10 Replies Latest reply on Mar 25, 2013 8:20 AM by gimbal2

    send http request with some parameters via POST method

    sofiane
      hi
      i am trying to send http request with some parameters via POST method so i wrote the following code

      String query = "TypeSearch="+ URLEncoder.encode("DCI","UTF-8" )+"&"+"DaInBox="+URLEncoder.encode("%","UTF-8") ;
      URL url=new URL("http://example.dz/index.asp");


      HttpURLConnection connection=(HttpURLConnection)url.openConnection();

      connection.setDoOutput(true);


      connection.setRequestMethod("POST");
      connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
      connection.setRequestProperty("charset", "utf-8");
      connection.setRequestProperty("Content-Length", "" + Integer.toString(query.getBytes().length));
      connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0");

      DataOutputStream out=new DataOutputStream(connection.getOutputStream());

      out.writeBytes(query);

      out.close();
      System.out.println(connection.getResponseCode());
      System.out.println(connection.getResponseMessage());

      unfortunately all i get is that message :405 Method not allowed

      please i need help to understand what am i doing wrong