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


    h ttpClient HttpMethod NameValuePair setRequestBody 
今天开发时,遇到利用Java中HttpClient类以POST方式提交数据,目标收到后中文乱码问题。
请求端代码:
Java代码 复制代码  收藏代码
  1. /** 
  2.  * HttpClient提交参数 
  3.  * @author sunyunfang@126.com 
  4.  */  
  5. public static void main(String[] args) throws IOException {  
  6.     HttpClient client = new HttpClient();  
  7.     client.getHostConfiguration().setHost("127.0.0.1"8081"http");  
  8.     // 使用POST方式提交数据  
  9.     HttpMethod method = getPostMethod();  
  10.     client.executeMethod(method);  
  11.     // 打印服务器返回的状态  
  12.     System.out.println(method.getStatusLine());  
  13.     // 打印结果页面  
  14.     String response = new String(method.getResponseBodyAsString().getBytes("8859_1"));  
  15.     // 打印返回的信息  
  16.     System.out.println(response);  
  17.     method.releaseConnection();  
  18. }  
  19.   
  20. // 使用POST方式提交数据  
  21. private static HttpMethod getPostMethod() {  
  22.     String url = "/PushServer/notification.do?action=sendOneMsg";  
  23.     NameValuePair message = new NameValuePair("message""消息内容。");  
  24.     post.setRequestBody(new NameValuePair[]{message});  
  25.     return post;  
  26. }  
  27.   
  28. // 使用GET方式提交数据  
  29. private static HttpMethod getGetMethod() {  
  30.     return new GetMethod("/PushServer/notification.do?action=sendOneMsg&message=abcd");  
  31. }  
	/**
	 * 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代码 复制代码  收藏代码
  1. /** 
  2.  * 供MsgServer远程调用 
  3.  * @param request 
  4.  * @param response 
  5.  * @return 
  6.  * @throws Exception 
  7.  * @author SunYunfang@126.com 
  8.  */  
  9. public ModelAndView sendOneMsg(HttpServletRequest request,  
  10.         HttpServletResponse response) throws Exception {  
  11.     String message = ServletRequestUtils.getStringParameter(request, "message");  
  12. }  
    /**
     * 供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");
    }

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

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

修正后:
Java代码 复制代码  收藏代码
  1. /** 
  2.  * HttpClient提交参数 
  3.  * @author SunYunfang@126.com 
  4.  */  
  5. public static void main(String[] args) throws IOException {  
  6.     HttpClient client = new HttpClient();  
  7.     client.getHostConfiguration().setHost("127.0.0.1"8081"http");  
  8.     // 使用POST方式提交数据  
  9.     HttpMethod method = getPostMethod();  
  10.     client.executeMethod(method);  
  11.     // 打印服务器返回的状态  
  12.     System.out.println(method.getStatusLine());  
  13.     // 打印结果页面  
  14.     String response = new String(method.getResponseBodyAsString().getBytes("8859_1"));  
  15.     // 打印返回的信息  
  16.     System.out.println(response);  
  17.     method.releaseConnection();  
  18. }  
  19.   
  20. // 使用POST方式提交数据  
  21. private HttpMethod getPostMethod() {  
  22.     String url = "/PushServer/notification.do?action=sendOneMsg";  
  23.     PostMethod post = new UTF8PostMethod(url);  
  24.     NameValuePair message = new NameValuePair("message""消息内容。");  
  25.     post.setRequestBody(new NameValuePair[]{message});  
  26.     return post;  
  27. }  
  28.   
  29. //Inner class for UTF-8 support     
  30. public static class UTF8PostMethod extends PostMethod{     
  31.     public UTF8PostMethod(String url){     
  32.     super(url);     
  33.     }     
  34.     @Override     
  35.     public String getRequestCharSet() {     
  36.         //return super.getRequestCharSet();     
  37.         return "UTF-8";     
  38.     }  
  39. }  
  40.   
  41. // 使用GET方式提交数据  
  42. private static HttpMethod getGetMethod() {  
  43.     return new GetMethod("/PushServer/notification.do?action=sendOneMsg&message=abcd");  
  44. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值