title: Upgrading POST request from HttpClient to HttpComponents. What's going wrong here?
body: <p>I inherited some old code that uses the now-deprecated Apache Commons HttpClient. I was tasked with upgrading it to use the newer Apache HttpComponents. However, I can't seem to get this POST request to function properly. The server keeps complaining that <code>Content-Length = 0</code>. I'm fairly certain that it's a problem with my conversion of how parameters are added.</p>
  
  <p>The old HttpClient code looks something like this:</p>
  
  <pre><code>PostMethod postMethod = null;
  int responseCode = 0;
  try{
      HttpClient httpClient = new HttpClient();
      postMethod = new PostMethod(getServiceUrl()); //The url, without a query.
      ...
      postMethod.addParameter(paramName, request);
  
      responseCode = httpClient.executeMethod(postMethod);
      ...
  }
  </code></pre>
  
  <p>And here are my HttpComponents replacements:</p>
  
  <pre><code>HttpPost postMethod = null;
  int responseCode = 0;
  HttpResponse httpResponse = null;
  try{
      HttpClient httpClient = new DefaultHttpClient();
      postMethod = new HttpPost(getServiceUrl()); //The url, without a query.
      ...
      BasicHttpParams params = new BasicHttpParams();
      params.setParameter(paramName, request);
      postMethod.setParams(params);
  
      httpResponse = httpClient.execute(postMethod);
      responseCode = httpResponse.getStatusLine().getStatusCode();
      ...
  }
  </code></pre>
  
  <p>The servlet my code it talking to is using Apache Commons FileUpload. Here is the code it catches on when it receives my request:</p>
  
  <pre><code>ServletRequestContext src = new ServletRequestContext(request);
  if (src.getContentLength() == 0)
      throw new IOException("Could not construct ServletRequestContext object");
  </code></pre>
  
  <p>It used to pass this test just fine. Now it doesn't. I've tried all kinds of alternatives, such as using the header, or passing <code>request</code> as a URLEncoded query. Have I made a mistake in my upgrade, somewhere?</p>
  
  <p>Note: I can't just change how the servlet receives my request, because then I'll have to change a number of other apps that talk to it, and that's too big a job.</p>