开发者

Encode params used within a post

开发者 https://www.devze.com 2022-12-15 18:30 出处:网络
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:

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开发者_Python百科 you!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消