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");  
}
相關文章
相關標籤/搜索