编码帖子中使用的参数

时间:2010-01-09 15:20:43

标签: java apache post urlencode params

我需要将params编码为ISOLatin,我打算将其发布到网站上。我正在使用org.apache.http。库。我的代码如下所示:

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);

谢谢!

1 个答案:

答案 0 :(得分:0)

您设置的参数错误。这是一个例子,

       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);

默认编码为Latin-1。