Spork Code

Code. Randomness. Sporks.

Posts Tagged ‘Apache

HTTP Posts in Apache’s new HttpClient 4.0 (with Lighttpd)

leave a comment »

Apache is developing a new HTTP library which is not based on the current Commons libraries. The documentation on the new libraries can be found here. I came across the new library while developing for Google’s Android platform, which uses HttpClient 4.0 for its HTTP protocol handling. The new libraries work well, but I spent a considerable amount of time trying to figure out why it was that I couldn’t get my server to accept the information I had POSTed to it. I finally discovered that the issue was with Lighttpd. Lighttpd was the web server used for the service I was POSTing to. It turns out that Lighttpd has had a long-standing issue with the Expect 100-Continue header. Fortunately, this issue has been resolved in Lighttpd 1.5, but for earlier versions it had never been patched. I had to turn, instead to a rather obscure function which disabled that header. Here is the code:

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);

//THIS IS THE LINE WE CARE ABOUT TO FIX LIHTTPD POSTS
HttpProtocolParams.setUseExpectContinue(params, false);
try {
   StringEntity se = new StringEntity( /* post params went here */ );
   post.setEntity(se);
} catch (UnsupportedEncodingException e1) {
   e1.printStackTrace();
}
try {
   HttpResponse resp =  client.execute(post);
} catch (ClientProtocolException e) {
   e.printStackTrace();
} catch (IOException e) {
   e.printStackTrace();
}

Written by John Gunderman

June 30, 2009 at 9:50 pm

Posted in Uncategorized

Tagged with , , ,