I am using HttpURLConnection to send data to the server via POST. I set the headers, then get the output stream and write 5 bytes of data ("M = 005"), then closes the output stream.
On the server, I get all the headers, the correct length of the content, but then I get the line with zero length and the server hangs on readLine.
It seems that the client never closes, so all the data is not written, so the server never receives it.
I read many examples and tried various changes to see if I could in any way affect this with less desired results. For example, turning off keep alives, forcing the CRLF to end my data (which causes my data to go to the server, but the connection still doesn’t close (this was only for testing), trying a writer.
Since many examples do what I am, I assume it is simple, I skip, but I just do not see it. Any help would be appreciated.
StringBuilder postDataBuilder.append("M=").append(URLEncoder.encode("005", UTF8));
byte[] postData = null;
postData = postDataBuilder.toString().getBytes();
url = new URL("http://" + serverAddress + ":" + String.valueOf(serverPort));
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Length", Integer.toString(postData.length));
conn.setUseCaches(false);
OutputStream out = conn.getOutputStream();
out.write(postData);
out.close();
int responseCode = conn.getResponseCode();
source
share