Uploaded image for project: 'HttpComponents HttpClient'
  1. HttpComponents HttpClient
  2. HTTPCLIENT-795

If there is more than 15 seconds between HttpClient.execute() calls using a MultipartEntity, a ProtocolException is thrown complaining about the Content-Length header already being present.

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 4.0 Alpha 4
    • 4.0 Beta 1
    • HttpClient (classic)
    • None
    • Linux, java version "1.6.0_06" Java(TM) SE Runtime Environment (build 1.6.0_06-b02) Java HotSpot(TM) Client VM (build 10.0-b22, mixed mode). Also using HttpCore-4.0-beta2

    Description

      I am not sure if this time-related behaviour is intentional or not (I have only been using this library for a few weeks) , but even if a timeout is to be expected, the exception thrown ought to indicate that there is a time component involved. "org.apache.http.ProtocolException: Content-Length header already present" is incredibly misleading.

      A simple-ish compileable program to reproduce the bug is as follows:

      import java.nio.charset.Charset;
      import org.apache.http.HttpResponse;
      import org.apache.http.client.methods.HttpPost;
      import org.apache.http.client.params.ClientPNames;
      import org.apache.http.client.params.CookiePolicy;
      import org.apache.http.entity.mime.MultipartEntity;
      import org.apache.http.entity.mime.content.StringBody;
      import org.apache.http.impl.client.DefaultHttpClient;
      public class Simple {
      static public void main(String [] args)
      {
      try

      { DefaultHttpClient client = new DefaultHttpClient(); client.getParams().setParameter( ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); MultipartEntity entity; StringBody stringBody; HttpPost post; HttpResponse response; entity = new MultipartEntity(); stringBody = new StringBody("field contents",Charset.forName("ISO-8859-1")); entity.addPart("field", stringBody); post = new HttpPost("http://localhost/simple.php"); post.setEntity(entity); response = client.execute(post); //The exception does not occur if the content is not consumed response.getEntity().consumeContent(); System.out.println("First post done"); //The exception does not occur if the time interval between the requests is too short Thread.sleep(15000); //The exception naturally doesn't occur if a new HttpClient is created //client = new DefaultHttpClient(); entity = new MultipartEntity(); stringBody = new StringBody("field contents",Charset.forName("ISO-8859-1")); entity.addPart("field", stringBody); post = new HttpPost("http://localhost/simple.php"); post.setEntity(entity); response = client.execute(post); //Will throw the following: /* org.apache.http.ProtocolException: Content-Length header already present at org.apache.http.protocol.RequestContent.process(RequestContent.java:70) at org.apache.http.protocol.BasicHttpProcessor.process(BasicHttpProcessor.java:290) at org.apache.http.protocol.HttpRequestExecutor.preProcess(HttpRequestExecutor.java:160) at org.apache.http.impl.client.DefaultClientRequestDirector.execute(DefaultClientRequestDirector.java:356) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:501) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:456) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:422) at test.Simple.main(Simple.java:57) */ System.out.println("Second post done"); }

      catch(Exception e)

      { System.out.println(e); e.printStackTrace(); }

      }
      }

      Attachments

        Activity

          People

            Unassigned Unassigned
            immesys Michael Andersen
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: