Java的HttpClient类以POST方式提交数据,目标端收到后中文乱码

今天开发时,遇到利用Java中HttpClient类以POST方式提交数据,目标收到后中文乱码问题。 请求端代码:服务器

Java代码  收藏代码
/** 
 * HttpClient提交参数 
 * @author sunyunfang@126.com 
 */  
public static void main(String[] args) throws IOException {  
    HttpClient client = new HttpClient();  
    client.getHostConfiguration().setHost("127.0.0.1", 8081, "http");  
    // 使用POST方式提交数据  
    HttpMethod method = getPostMethod();  
    client.executeMethod(method);  
    // 打印服务器返回的状态  
    System.out.println(method.getStatusLine());  
    // 打印结果页面  
    String response = new String(method.getResponseBodyAsString().getBytes("8859_1"));  
    // 打印返回的信息  
    System.out.println(response);  
    method.releaseConnection();  
}  
  
// 使用POST方式提交数据  
private static HttpMethod getPostMethod() {  
    String url = "/PushServer/notification.do?action=sendOneMsg";  
    NameValuePair message = new NameValuePair("message", "消息内容。");  
    post.setRequestBody(new NameValuePair[]{message});  
    return post;  
}  
  
// 使用GET方式提交数据  
private static HttpMethod getGetMethod() {  
    return new GetMethod("/PushServer/notification.do?action=sendOneMsg&message=abcd");  
}  

目标端代码: 
Java代码  收藏代码
/** 
 * 供MsgServer远程调用 
 * @param request 
 * @param response 
 * @return 
 * @throws Exception 
 * @author SunYunfang@126.com 
 */  
public ModelAndView sendOneMsg(HttpServletRequest request,  
        HttpServletResponse response) throws Exception {  
    String message = ServletRequestUtils.getStringParameter(request, "message");  
}

这段代码执行后,目标能收到信息,可是中文乱码,也没有找到转码的方法。ide

经分析,原来使用 NameValuePair 加入的HTTP请求的参数最终都会转化为 RequestEntity 提交到HTTP服务器。接着在PostMethod的父类 EntityEnclosingMethod 中发现,只要重载getRequestCharSet()方法就能设置提交的编码(字符集)。post

修正后: 
Java代码  收藏代码
/** 
 * HttpClient提交参数 
 * @author SunYunfang@126.com 
 */  
public static void main(String[] args) throws IOException {  
    HttpClient client = new HttpClient();  
    client.getHostConfiguration().setHost("127.0.0.1", 8081, "http");  
    // 使用POST方式提交数据  
    HttpMethod method = getPostMethod();  
    client.executeMethod(method);  
    // 打印服务器返回的状态  
    System.out.println(method.getStatusLine());  
    // 打印结果页面  
    String response = new String(method.getResponseBodyAsString().getBytes("8859_1"));  
    // 打印返回的信息  
    System.out.println(response);  
    method.releaseConnection();  
}  
  
// 使用POST方式提交数据  
private HttpMethod getPostMethod() {  
    String url = "/PushServer/notification.do?action=sendOneMsg";  
    PostMethod post = new UTF8PostMethod(url);  
    NameValuePair message = new NameValuePair("message", "消息内容。");  
    post.setRequestBody(new NameValuePair[]{message});  
    return post;  
}  
  
//Inner class for UTF-8 support     
public static class UTF8PostMethod extends PostMethod{     
    public UTF8PostMethod(String url){     
    super(url);     
    }     
    @Override     
    public String getRequestCharSet() {     
        //return super.getRequestCharSet();     
        return "UTF-8";     
    }  
}  
  
// 使用GET方式提交数据  
private static HttpMethod getGetMethod() {  
    return new GetMethod("/PushServer/notification.do?action=sendOneMsg&message=abcd");  
}
相关文章
相关标签/搜索