Monday, August 4, 2008

Setting headers for HttpClient request operations

I was searching through the internet for a while and though the answer finally seemed pretty simple i could not get a solid code snippet and hence i am jotting it down here for some future reference.

Say suppose, i want to set a request header for an HttpClient request that i am sending to a URL (it could also be a REST based service), here is how i would go about doing it.

HttpClient client = new HttpClient();
PutMethod method = new PutMethod();
Header head1 = new Header("foo", "f");
method.addRequestHeader(head1);
int statusCode = client.executeMethod(method);


Now, there could be other ways to do this same thing too.. But hey i got a start!

No comments: