10 Replies Latest reply: Mar 25, 2013 3:20 AM by gimbal2 RSS

    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
        • 1. Re: send http request with some parameters via POST method
          gimbal2
          Who says you're doing anything wrong? Try looking up what that response actually means. Its a standard HTTP code after all, it is properly documented.
          • 2. Re: send http request with some parameters via POST method
            sofiane
            *405 Method Not Allowed*

            The method specified in the Request-Line is not allowed for the resource identified by the Request-URI. The response MUST include an Allow header containing a list of valid methods for the requested resource.

            that's what i found in Http/1.1 status code definitions
            • 3. Re: send http request with some parameters via POST method
              baftos
              sofiane wrote:
              *405 Method Not Allowed*

              The method specified in the Request-Line is not allowed for the resource identified by the Request-URI. The response MUST include an Allow header containing a list of valid methods for the requested resource.

              that's what i found in Http/1.1 status code definitions
              If you use connection.getInputStream(), you can see the full response, including the Allow header that lists the valid methods. POST is not among them, I presume.

              Edited by: baftos on Mar 21, 2013 5:27 PM
              • 4. Re: send http request with some parameters via POST method
                gimbal2
                sofiane wrote:
                that's what i found in Http/1.1 status code definitions
                Yes but do you understand what it means? If not: do more research. That's the essence of research and gaining understanding: take your time.

                I am 99% positive that you are doing a POST request to a resource that simply does not allow a POST request to be made to it. The only mistake you would then be making is assuming that you can.
                • 5. Re: send http request with some parameters via POST method
                  sofiane
                  hi again
                  i am sick and tired of trying to find a solution to my problem ,i tried to use the commons httpClient [http://hc.apache.org/downloads.cgi] ,i always get the same error 405 method not allowed
                  i also used the RESTClient in my browser (Mozila Firefox) to send a POST request all i get is this response header
                  Status Code: 405 M
                  Allow: OPTIONS, TRACE, GET, HEAD
                  Connection: close
                  Content-Length: 4003
                  Content-Type: text/html
                  Server: Microsoft-IIS/5.0

                  this result makes me sure that the POST method is not allowed by the server , after analyzing the form tag in the html source code of the page that's what i found

                  <form action="cherche.asp" method="post">

                  i just can't understand, how the browser can send the post request if it's not allowed by the server

                  Edited by: sofiane on Mar 22, 2013 6:38 AM
                  • 6. Re: send http request with some parameters via POST method
                    jtahlborn
                    sofiane wrote:
                    <form action="cherche.asp" method="post">

                    i just can't understand, how the browser can send the post request if it's not allowed by the server
                    you do realize that that is a different resource than the one you are using in your code: "cherche.asp" vs. "index.asp".
                    • 7. Re: send http request with some parameters via POST method
                      sofiane
                      oky let's be more clearly, there is no index.asp i just put it in the example to allude to the web site is developed in ASP and it's run on Microsoft IIS server actually the exact url is [nomenclature of medicines|http://emphis.ands.dz] ,

                      there is input text and list of items to specify the parameters of search and submit button ,all a want to do is to invoke the search function and get the response ,parse it to have the list of this medicines

                      i am very thankful for your interaction

                      Edited by: sofiane on Mar 22, 2013 9:28 AM
                      • 8. Re: send http request with some parameters via POST method
                        jtahlborn
                        did you capture the webbrowser's interaction via a proxy (like charles proxy) to verify that it is indeed sending a post (or use something like firebug)?
                        • 9. Re: send http request with some parameters via POST method
                          sofiane
                          thank's gays it was an encoding problem

                          String query = URLEncoder.encode("TypeSearch=","UTF-8")+ URLEncoder.encode("DCI","UTF-8" )+"&"+URLEncoder.encode("DaInBox=","UTF-8")+URLEncoder.encode("%","UTF-8") ;

                          firebug help me to know some invisible paremeters in the url
                          • 10. Re: send http request with some parameters via POST method
                            gimbal2
                            sofiane wrote:
                            thank's gays it was an encoding problem
                            Unless you have an alternative keyboard layout, that is a very odd typo to make since the A and U are nowhere near each other.