Http请求之基于HttpUrlConnection,支持Header,Body传值,支持Multipart上传文件:

 Http请求之基于HttpUrlConnection,支持Header,Body传值,支持Multipart上传文件:

[java] view plain copy 在CODE上查看代码片派生到我的代码片
  1. public static String post(String actionUrl, Map<String, String> headParams,  
  2.             Map<String, String> params,  
  3.             Map<String, File> files) throws IOException {  
  4.   
  5.         String BOUNDARY = java.util.UUID.randomUUID().toString();  
  6.         String PREFIX = "--", LINEND = "\r\n";  
  7.         String MULTIPART_FROM_DATA = "multipart/form-data";  
  8.         String CHARSET = "UTF-8";  
  9.   
  10.         URL uri = new URL(actionUrl);  
  11.         HttpURLConnection conn = (HttpURLConnection) uri.openConnection();  
  12.         conn.setReadTimeout(30 * 1000); // 缓存的最长时间  
  13.         conn.setDoInput(true);// 允许输入  
  14.         conn.setDoOutput(true);// 允许输出  
  15.         conn.setUseCaches(false); // 不允许使用缓存  
  16.         conn.setRequestMethod("POST");  
  17.         conn.setRequestProperty("connection", "keep-alive");  
  18.         conn.setRequestProperty("Charsert", "UTF-8");  
  19.         conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA  
  20.                 + ";boundary=" + BOUNDARY);  
  21.         if(headParams!=null){  
  22.             for(String key : headParams.keySet()){  
  23.                 conn.setRequestProperty(key, headParams.get(key));  
  24.             }  
  25.         }  
  26.         StringBuilder sb = new StringBuilder();  
  27.   
  28.         if (params!=null) {  
  29.             // 首先组拼文本类型的参数  
  30.             for (Map.Entry<String, String> entry : params.entrySet()) {  
  31.                 sb.append(PREFIX);  
  32.                 sb.append(BOUNDARY);  
  33.                 sb.append(LINEND);  
  34.                 sb.append("Content-Disposition: form-data; name=\""  
  35.                         + entry.getKey() + "\"" + LINEND);  
  36.                 sb.append("Content-Type: text/plain; charset=" + CHARSET + LINEND);  
  37.                 sb.append("Content-Transfer-Encoding: 8bit" + LINEND);  
  38.                 sb.append(LINEND);  
  39.                 sb.append(entry.getValue());  
  40.                 sb.append(LINEND);  
  41.             }  
  42.               
  43.         }  
  44.           
  45.         DataOutputStream outStream = new DataOutputStream(  
  46.                 conn.getOutputStream());  
  47.         if (!TextUtils.isEmpty(sb.toString())) {  
  48.             outStream.write(sb.toString().getBytes());  
  49.         }  
  50.           
  51.   
  52.         // 发送文件数据  
  53.         if (files != null)  
  54.             for (Map.Entry<String, File> file : files.entrySet()) {  
  55.                 StringBuilder sb1 = new StringBuilder();  
  56.                 sb1.append(PREFIX);  
  57.                 sb1.append(BOUNDARY);  
  58.                 sb1.append(LINEND);  
  59.                 sb1.append("Content-Disposition: form-data; name=\"file\"; filename=\""  
  60.                         + file.getKey() + "\"" + LINEND);  
  61.                 sb1.append("Content-Type: application/octet-stream; charset="  
  62.                         + CHARSET + LINEND);  
  63.                 sb1.append(LINEND);  
  64.                 outStream.write(sb1.toString().getBytes());  
  65.   
  66.                 InputStream is = new FileInputStream(file.getValue());  
  67.                 byte[] buffer = new byte[1024];  
  68.                 int len = 0;  
  69.                 while ((len = is.read(buffer)) != -1) {  
  70.                     outStream.write(buffer, 0, len);  
  71.                     Log.i("HttpUtil", "写入中...");  
  72.                 }  
  73.   
  74.                 is.close();  
  75.                 outStream.write(LINEND.getBytes());  
  76.             }  
  77.   
  78.         // 请求结束标志  
  79.         byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();  
  80.         outStream.write(end_data);  
  81.         outStream.flush();  
  82.         Log.i("HttpUtil", "conn.getContentLength():"+conn.getContentLength());  
  83.   
  84.         // 得到响应码  
  85.         int res = conn.getResponseCode();  
  86.         InputStream in = conn.getInputStream();  
  87.         if (res == 200) {  
  88.              BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in, "UTF-8"));    
  89.                    StringBuffer buffer = new StringBuffer();    
  90.                  String line = "";    
  91.              while ((line = bufferedReader.readLine()) != null){    
  92.                    buffer.append(line);    
  93.              }    
  94.   
  95. //          int ch;  
  96. //          StringBuilder sb2 = new StringBuilder();  
  97. //          while ((ch = in.read()) != -1) {  
  98. //              sb2.append((char) ch);  
  99. //          }  
  100.             return buffer.toString();  
  101.         }  
  102.         outStream.close();  
  103.         conn.disconnect();  
  104.         return in.toString();  
  105.   
  106.     }  

posted on 2017-02-27 15:07  Sun‘刺眼的博客  阅读(15628)  评论(1编辑  收藏  举报

导航