views:

106

answers:

1

Hello Experts!

I need to encode the params to ISOLatin which i intend to post to the site. I'm using org.apache.http. libraries. My code looks like follows:

HttpClient client = new DefaultHttpClient();

HttpPost post = new HttpPost("www.foobar.bar");
post.setHeader("Content-Type", "application/x-www-form-urlencoded");           
HttpParams params = new BasicHttpParams();

params.setParameter("action", "find");
params.setParameter("what", "somebody");

post.setParams(params);

HttpResponse response2 = httpClient.execute(post);

Thank you!

A: 

You are setting parameters wrong. Here is an example,

       PostMethod method = new PostMethod(url);
       method.addParameters("action", "find");
       method.addParameters("what", "somebody");

       int status = httpClient.executeMethod(method);
       byte[] bytes = method.getResponseBody();
       response = new String(bytes, "iso-8859-1");
       if (status != HttpStatus.SC_OK)
             throw new IOException("Status code: " + status + " Message: "
                                        + response);

The default encoding will be Latin-1.

ZZ Coder
I'm not sure I follow you, Doesn't this mean that you encode the response. I need to encode the request params.
jakob
You just add parameters to the method. The executeMethod() will take care of the rest, including encoding parameters properly.
ZZ Coder