首页 > 编程语言 > 详细

java中post时中文乱码

时间:2015-06-23 15:05:16      阅读:245      评论:0      收藏:0      [点我收藏+]

http://blog.chinaunix.net/uid-12348673-id-3335300.html

 

设置流的编码,就避免了乱码

public static String post(String path,String params) throws Exception{
 HttpURLConnection httpConn=null;
 BufferedReader in=null;
 //PrintWriter out=null;
 try {
  URL url=new URL(path);
  httpConn=(HttpURLConnection)url.openConnection();
  httpConn.setRequestMethod("POST");
  httpConn.setDoInput(true);
  httpConn.setDoOutput(true);
  System.out.print("params="+params);
 //utf-8
  PrintWriter ot = new PrintWriter(new OutputStreamWriter(httpConn.getOutputStream(),"utf-8")); 
  ot.println(params);
  ot.flush();
  //out=new PrintWriter(httpConn.getOutputStream());
  //out.println(params);
  
  //out.flush();

  //读取响应
  if(httpConn.getResponseCode()==HttpURLConnection.HTTP_OK){
   StringBuffer content=new StringBuffer();
   String tempStr="";
   //utf-8
   in=new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"utf-8"));
   while((tempStr=in.readLine())!=null){
    content.append(tempStr);
   }
   return content.toString();
  }else{
	  
   throw new Exception("请求出现了问题!");
   
  }
 } catch (IOException e) {
  e.printStackTrace();
 }finally{
  in.close();
 // out.close();
  httpConn.disconnect();
 }
 return null;
}

  

java中post时中文乱码

原文:http://www.cnblogs.com/zjn0zjn/p/4595234.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!