Reputation: 500
I am using HttpURLConnection to send data to a server via POST. I set the headers then get the output stream and write 5 bytes of data ("M=005") then close the output stream.
On the server I receive all the headers,the correct content length but then I get a zero length line and the server hangs on readLine.
What appears to happen is the client close never actually happens so the entire data is not written hence the server never gets it.
I have read numerous examples and tried various changes to see if I can effect this in any way all with less than desirable results. For example turning off keep alives, forcing a CRLF at then end of my data (which forces my data out at the server but the connection still doesn't close. (This was just for testing), trying a print writer.
Since many examples are doing what I am I assume it's something simple I am overlooking but I just can't 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();
// After executing the above line the server starts to successfully readLines
// until it gets to the post data when the server hangs. If I restart the
// client side then the data finally gets through but the connection on the
// server side never ends.
Upvotes: 3
Views: 12500
Reputation: 500
Ooops. Bug on our server side that was doing a readLine instead of character read. Since there is no CRLF it will hang.
Upvotes: 3