`

apache httpclien访问乱码,返回信息乱码问题

 
阅读更多

     最近在项目中遇到一个问题,客户端使用apache httpclient向服务端发送消息,如发送内容全是英文,没有问题。但是如发送中文则在服务端会接受到乱码,解决如下:

     在HttpClient httpClient = new HttpClient() ;前加入以下语句之一

  • postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
  • postMethod.addRequestHeader("Content-Type","text/html;charset=UTF-8");
  • postMethod.setRequestHeader("Content-Type","text/html;charset=UTF-8")

     在服务端返回消息是,带有中文也是乱码,需要在服务端设置

  • response.setCharacterEncoding("UTF-8");
  • PrintWriter out = response.getWriter();
设置编码必须在getWriter()的前面。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics