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. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Key: HTTPCLIENT-795 URL: https://issues.apache.org/jira/browse/HTTPCLIENT-795 Project: HttpComponents HttpClient Issue Type: Bug Components: HttpClient Affects Versions: 4.0 Alpha 4 Environment: 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 Reporter: Michael Andersen 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(); } } } -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]