在用到HttpClient的基本請求和響應時候,發(fā)現默認的編碼是“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); //發(fā)送請求 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"; } } |
|