Note that there are some explanatory texts on larger screens.

plurals
  1. POHTTP post body lost spuriously (HttpURLConnection & Jetty)
    primarykey
    data
    text
    <p>I have a home grown protocol which uses HttpURLConnection (from Java 1.6) &amp; Jetty (6.1.26) to POST a block of xml as a request and receive a block of xml as a response. The amounts of xml are approx. 5KB.</p> <p>When running both sender and receiver on Linux EC2 instances in different parts of the world I'm finding that in about 0.04% of my requests the Jetty handler sees the xml request (the post body) as an empty string. I've checked and the client outputs that it's consistently trying to send the correct (> 0 length) xml request string. </p> <p>I have also reproduced this by looping my JUnit tests on my local (Win 8) box.</p> <p>I assume the error must be something like:</p> <ul> <li>Misuse of buffers</li> <li>An HttpURLConnection bug</li> <li>A network error</li> <li>A Jetty bug</li> <li>A random head slapping stupid thing I've done in the code</li> </ul> <p>The relevant code is below:</p> <p><strong>CLIENT</strong></p> <pre><code> connection = (HttpURLConnection) (new URL (url)).openConnection(); connection.setReadTimeout(readTimeoutMS); connection.setConnectTimeout(connectTimeoutMS); connection.setRequestMethod("POST"); connection.setAllowUserInteraction(false); connection.setDoOutput(true); // Send request byte[] postBytes = requestXML.getBytes("UTF-8"); connection.setRequestProperty("Content-length", "" + postBytes.length); OutputStream os = connection.getOutputStream(); os.write(postBytes); os.flush(); os.close(); // Read response InputStream is = connection.getInputStream(); StringWriter writer = new StringWriter(); IOUtils.copy(is, writer, "UTF-8"); is.close(); connection.disconnect(); return writer.toString(); </code></pre> <p><strong>SERVER (Jetty handler)</strong></p> <pre><code> public void handle(java.lang.String target, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, int dispatch) { InputStream is = request.getInputStream(); StringWriter writer = new StringWriter(); IOUtils.copy(is, writer, "UTF-8"); is.close(); String requestXML = writer.toString(); // requestXML is 0 length string about 0.04% of time </code></pre> <p>Can anyone think of why I'd randomly get the request as an empty string?</p> <p>Thanks!</p> <p><strong>EDIT</strong></p> <p>I introduced some more trace and getContentLength() returns -1 when the error occurs, but the client output still shows it's sending the right amount of bytes.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload