HttpURLConnection 处理乱码办法

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/windvally/article/details/38560375

对请求中含中文的部分进行utf-8的转换:

类似:

try {
              pgstatus02=URLEncoder.encode(pgstatus02,"utf-8");
             } catch (UnsupportedEncodingException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
             }

URL url = new URL(pgstatus02);   
   HttpURLConnection connection = (HttpURLConnection) url
     .openConnection();
   connection.setDoOutput(true); 
   connection.setRequestProperty("Content-type", "text/html");
   connection.setRequestProperty("Accept-Charset", "utf-8"); 
   connection.setRequestProperty("contentType", "utf-8"); 
   connection.setConnectTimeout(3000);
   connection.setRequestMethod("POST");
   connection.setDoInput(true);

 

服务器端:

 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  response.setContentType("text/html;charset=utf-8");
  request.setCharacterEncoding("utf-8");
  response.setCharacterEncoding("utf-8");
  PrintWriter out = response.getWriter();

}

 

 

 

 

展开阅读全文

没有更多推荐了,返回首页