POSTing with commons-httpclient

POSTing with commons-httpclient

So, commons-httpclient is quite useful and easy for programming http operations, e.t.c. As their tutorial explains, you just create an HttpClient, then a PostMethod or a GetMethod and finally you have the client execute the method.
In the particular use case i was working on, i needed to post some data to a MS ASP 6.0 Greek Server – both the request and the response should be in windows-1253. After testing a simple get request and seeing it working, I thought it wouldn’t get any easier than that… and it didn’t!
When doing post (which is a requirement due to the size of the data), i kept getting all those weird chars, e.t.c. I tried
setRequestHeader("Content-Type", "charset=windows-1253");

but somehow, the server wouldn’t get my data this time.

So, anyway, to cut a long story sort (and probably save you the hours I lost), the key is to remember you’re doing a post (!), and do

 setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=windows-1253");
 

BTW, addRequestHeader("Content-Type", "charset=windows-1253"); wouldn’t work either.