Community
Participate
Working Groups
Build Identifier: I have following code that creates an HTTP request: HttpClient httpclient = new HttpClient(); httpclient.start(); ContentExchange exchange = new ContentExchange(); exchange.setMethod("GET"); exchange.setURL("http://localhost:8080/MyServlet"); String trial = "Hello World"; ByteArrayInputStream byteStream = new ByteArrayInputStream(trial.getBytes()); exchange.setRequestContentSource( byteStream ); byteStream.close(); httpclient.send(exchange); exchange.waitForDone(); With this code, on the servlet side, I do not get anything in the request content. Reproducible: Always Steps to Reproduce: 1. Use the above client code 2. Use the following servlet code: import javax.servlet.http.*; import javax.servlet.ServletInputStream; import java.io.*; public class MyServlet extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse resp) { try{ byte[] input = new byte[req.getContentLength()]; ServletInputStream sin = req.getInputStream(); sin.readLine(input,0,input.length); sin.close(); System.out.println("Request content : " + new String(input)); resp.setContentLength(input.length); resp.setStatus(HttpServletResponse.SC_OK); resp.getOutputStream().write(input); } catch (IOException e) { } } public void doGet(HttpServletRequest req, HttpServletResponse resp) { doPost(req, resp); } }
This is not a bug in Jetty. When the request content is set using an input stream, the content length does not get set (which seems obvious). I was not aware of this and the lack of content lenght on the servlet end was making me think that the Jetty client is not doing the right thing. I had to change the servlet code to take into account this and now everything works fine.