Reference to previous question: Getting an ajax response in java from a web method (java.io.FileNotFoundException)
I try to send a POST request containing json in the request body but the body is encoded differently when doing it from a web browser (chrome) and my java code.
Here is my code:
String params = "{\"prefixText\":\"aCity\",\"count\":10,\"contextKey\":\"he\"}";
conn = (HttpURLConnection) new URL("http://bus.gov.il/WebForms/wfrmMain.aspx/GetCompletionList").openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setFixedLengthStreamingMode(params.length());
conn.addRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.addRequestProperty("Referer", "http://bus.gov.il/WebForms/wfrmMain.aspx?width=1024&company=1&language=he&state=");
Writer out = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
out.write(params);
out.close();
String answer = readStream(conn.getInputStream(), "utf-8");
Here are the 2 requests sent as seen in Wireshark: link (first is the good one, second is the wrongly encoded java request).
I tried changing the charset in Content-Type but it did nothing.
Any help will be highly appreciated.
Here, you’re using the default character encoding for your system:
Instead, specify the encoding explicitly in the constructor call for
OutputStreamWriter:(You don’t need the
BufferedWriterhere – it’s not doing you any good.)EDIT: Okay, you’re now getting a problem because you’re setting the content length incorrectly. You should convert from text to binary once, like this: