将GBK编码形式的xml文件 数据请求转为utf-8

来源:互联网 发布:做网络监控用几类线 编辑:程序博客网 时间:2024/06/05 06:47

[java] view plain copy
  1. public String resposeString(String urlStr){  
  2.   
  3.         String result = "";  
  4.         try  
  5.         {  
  6.             URL url = new URL(urlStr);  
  7.   
  8.             HttpURLConnection httpURLConnection = null;  
  9.   
  10.             httpURLConnection = (HttpURLConnection)url.openConnection();  
  11.   
  12.             httpURLConnection.setRequestMethod("POST");  
  13.   
  14.             httpURLConnection.setDoOutput(true);//上传数据  
  15.             httpURLConnection.setConnectTimeout(15000);  
  16.             httpURLConnection.setReadTimeout(15000);  
  17.             InputStream inputStream = httpURLConnection.getInputStream();//获取返回的数据流  
  18.   
  19.             InputStreamReader isr = new InputStreamReader(inputStream, "gbk");//一定要在这个地方才不会乱码(utf-8,gb2312)  
  20.   
  21.             BufferedReader br = new BufferedReader(isr);//利用BufferedReader将流转为String  
  22.   
  23.             String temp;  
  24.   
  25.             while((temp = br.readLine()) != null)  
  26.             {  
  27.                 result = result + temp;  
  28.             }  
  29.         }  
  30.         catch (MalformedURLException e)  
  31.         {  
  32.             e.printStackTrace();  
  33.         }   
  34.         catch (IOException e)   
  35.         {  
  36.             e.printStackTrace();  
  37.         }   
  38.         return result;  
  39.     }  



转载网址:http://blog.csdn.net/qq_36226579/article/details/70171972





阅读全文
0 0
原创粉丝点击