Matt Williams
Matt Williams

Reputation: 79

Java Http POST not working as expected?

Im trying to send some data to a url with some post data. Iv tried the following code:-

try {
        // Construct data
        String data = URLEncoder.encode("appid", "UTF-8") + "=" + URLEncoder.encode("2", "UTF-8");
        data += "&" + URLEncoder.encode("secret", "UTF-8") + "=" + URLEncoder.encode("APPSECRET", "UTF-8");
        data += "&" + URLEncoder.encode("usermcaccount", "UTF-8") + "=" + URLEncoder.encode("NAME", "UTF-8");
        data += "&" + URLEncoder.encode("actiontype", "UTF-8") + "=" + URLEncoder.encode("ACTION", "UTF-8");
        data += "&" + URLEncoder.encode("serverip", "UTF-8") + "=" + URLEncoder.encode("IP", "UTF-8");


        // Send data
        URL url = new URL("http://maiacraft.com/api/");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true);
        conn.setRequestMethod("POST");
        DataOutputStream outStream;


        OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

        //write parameters
        writer.write(data);
        writer.flush();
        writer.close();




    } catch (Exception e) {
    }

Yet it doesnt seem to post it to the server. All the server side code is working as it allows a post request from another page but not from this java code...

Any ideas?

Upvotes: 0

Views: 753

Answers (1)

Stephen C
Stephen C

Reputation: 719279

Any ideas?

  1. You are not checking the HTTP response status. That will give you some clues as to what is going wrong. The response status can indicate various kinds of things; e.g. that your request parameters are wrong, the URL is wrong, you don't have permission, you need to login, the server is down, etcetera.

  2. If you get a non-2xx response code, there is likely to be some more information in the response body. You can access it by opening and reading the connection object's error stream.

  3. Check the server logs.

  4. Try submitting the same request using a web browser, or a utility like curl or wget. (Put the POST arguments into a file ... or attach them to the URL.)

  5. Capture and carefully examine the encoded POST data that your Java program is actually sending. You could modify the program to print data before it is sent, or you could use Wireshark (or equivalent) to read the request off the wire. Once you have captured the data, carefully compare it with what the HTML specification says about encoding form data; see http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4

  6. Finally, you are not setting a content type header in the request. It could well be that this is causing the server to ignore the POST data. Read the HTTP specification for a description of the headers that should be set. (In this case, you should use a content type consisting of the mime-type "application/x-www-form-urlencoded" and the content encoding you are using - "UTF-8"; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17)


I won't mention the exception squashing abomination at the end of the code sample ....

Ooops. I just did :-)

Upvotes: 3

Related Questions