在用到HttpClient的基本请求和响应时候,发现默认的编码是“ISO-8859-1”,这样就存在中文乱码问题了,解决办法如下,记录一下: http://hc./httpclient-3.x/charencodings.html#Request_Response_Body 三种形式: ![]() ![]() ![]() 评论:
# re: HttpClient POST的中文编码问题
2009-06-05 17:35 | okhaoba 我的方法:
HttpClient client = new HttpClient(); //设置超时时间 client.getHttpConnectionManager().getParams().setSoTimeout(timeOut); //使用post方式,参数长度不受限制 //postMethod = new PostMethod(url); postMethod = new UTF8PostMethod(url); //设置参数 NameValuePair[] nameValue = new NameValuePair[] { new NameValuePair("××param", xmlParamStr) }; postMethod.setRequestBody(nameValue); //发送请求 state = client.executeMethod(postMethod); private static class UTF8PostMethod extends PostMethod { public UTF8PostMethod(String url) { super(url); } @Override public String getRequestCharSet() { //return super.getRequestCharSet(); return "UTF-8"; } } |
|