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

时间:2023-03-09 14:28:32
Http请求之基于HttpUrlConnection,支持Header,Body传值,支持Multipart上传文件:

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

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