两类问题:
第一:post乱码解决
第二:get乱码解决
post乱码解决方案如下(省去若干理论和源码分析)
凡是用到postMethod的时候PostMethod使用如下的重载函数:
public static class UTF8PostMethod extends PostMethod{
public UTF8PostMethod(String url){
super(url);
}
@Override
public String getRequestCharSet() {
//return super.getRequestCharSet();
return "UTF-8";
}
}
即可解决问题
get乱码解决方案如下:
1. 对中文参数使用URLEncoder.encode(src);来编码;
2. 设置GetMethod编码格式为utf-8:get_method.addRequestHeader("Content-type" , "text/html; charset=utf-8");
3. 返回响应消息使用utf-8或gb2312编码:String response=new String(get_method.getResponseBodyAsString().getBytes("gb2312"));
事实上,get方式只要第三种即可比较完美的解决问题。