HttpUtils.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package com.mes.util;
  2. import com.mes.ui.MesClient;
  3. import java.io.*;
  4. import java.net.HttpURLConnection;
  5. import java.net.URISyntaxException;
  6. import java.net.URL;
  7. public class HttpUtils {
  8. /**
  9. * HTTP请求,正确报文解析,并保存数据到生产记录数据
  10. * @return
  11. * @throws URISyntaxException
  12. */
  13. //http post请求
  14. public static String sendRequest(String urlParam) {
  15. urlParam = appendSid(urlParam);
  16. String requestType = "POST";
  17. //根据接收内容返回数据结果
  18. String ret = "";
  19. System.out.println(urlParam);
  20. HttpURLConnection con = null;
  21. BufferedReader buffer = null;
  22. StringBuffer resultBuffer = null;
  23. try {
  24. URL url = new URL(urlParam);
  25. //得到连接对象
  26. con = (HttpURLConnection) url.openConnection();
  27. //设置请求类型
  28. con.setRequestMethod(requestType);
  29. //设置请求需要返回的数据类型和字符集类型
  30. con.setRequestProperty("Content-Type", "application/json;charset=GBK");
  31. //允许写出
  32. con.setDoOutput(true);
  33. //允许读入
  34. con.setDoInput(true);
  35. //不使用缓存
  36. con.setUseCaches(false);
  37. //得到响应码
  38. int responseCode = con.getResponseCode();
  39. System.out.println("responseCode="+responseCode);
  40. if(responseCode == HttpURLConnection.HTTP_OK){
  41. //得到响应流
  42. InputStream inputStream = con.getInputStream();
  43. //将响应流转换成字符串
  44. resultBuffer = new StringBuffer();
  45. String line;
  46. buffer = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
  47. while ((line = buffer.readLine()) != null) {
  48. resultBuffer.append(line);
  49. }
  50. System.out.println(resultBuffer.toString());
  51. ret = resultBuffer.toString();
  52. }else {
  53. //ret = String.valueOf(responseCode);
  54. ret = "false";
  55. }
  56. con.disconnect();
  57. }catch(Exception e) {
  58. //ret = false;
  59. //System.out.println("e.toString()="+e.toString());
  60. //return e.toString();
  61. //e.printStackTrace();
  62. ret = "false";
  63. }
  64. return ret;
  65. }
  66. public static String sendPostRequest(String urlParam, String params) {
  67. urlParam = appendSid(urlParam);
  68. String requestType = "POST";
  69. //根据接收内容返回数据结果
  70. String ret = "";
  71. System.out.println(urlParam);
  72. HttpURLConnection con = null;
  73. BufferedReader buffer = null;
  74. StringBuffer resultBuffer = null;
  75. try {
  76. URL url = new URL(urlParam);
  77. //得到连接对象
  78. con = (HttpURLConnection) url.openConnection();
  79. //设置请求类型
  80. con.setRequestMethod(requestType);
  81. //设置请求需要返回的数据类型和字符集类型
  82. con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=GBK");
  83. //允许写出
  84. con.setDoOutput(true);
  85. //允许读入
  86. con.setDoInput(true);
  87. //不使用缓存
  88. con.setUseCaches(false);
  89. // 获取URLConnection对象对应的输出流
  90. OutputStreamWriter out = new OutputStreamWriter( con.getOutputStream(),"UTF-8");// utf-8编码
  91. // 发送请求参数
  92. out.write(params);
  93. //得到响应码
  94. int responseCode = con.getResponseCode();
  95. System.out.println("responseCode="+responseCode);
  96. if(responseCode == HttpURLConnection.HTTP_OK){
  97. //得到响应流
  98. InputStream inputStream = con.getInputStream();
  99. //将响应流转换成字符串
  100. resultBuffer = new StringBuffer();
  101. String line;
  102. buffer = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
  103. while ((line = buffer.readLine()) != null) {
  104. resultBuffer.append(line);
  105. }
  106. System.out.println(resultBuffer.toString());
  107. ret = resultBuffer.toString();
  108. }else {
  109. //ret = String.valueOf(responseCode);
  110. ret = "false";
  111. }
  112. con.disconnect();
  113. }catch(Exception e) {
  114. //ret = false;
  115. //System.out.println("e.toString()="+e.toString());
  116. //return e.toString();
  117. //e.printStackTrace();
  118. ret = "false";
  119. }
  120. return ret;
  121. }
  122. public static String sendPostRequestJson(String apiUrl, String jsonData) throws IOException {
  123. apiUrl = appendSid(apiUrl);
  124. URL url = new URL(apiUrl);
  125. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  126. conn.setRequestMethod("POST");
  127. conn.setRequestProperty("Content-Type", "application/json");
  128. conn.setDoOutput(true);
  129. // 发送请求数据
  130. try (OutputStream os = conn.getOutputStream()) {
  131. byte[] input = jsonData.getBytes("utf-8");
  132. os.write(input, 0, input.length);
  133. }
  134. // 获取响应数据
  135. StringBuilder response = new StringBuilder();
  136. int responseCode = conn.getResponseCode();
  137. // System.out.println("Sending POST request to URL: " + apiUrl);
  138. // System.out.println("Post parameters: " + jsonData);
  139. // System.out.println("Response Code: " + responseCode);
  140. try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"))) {
  141. String responseLine;
  142. while ((responseLine = br.readLine()) != null) {
  143. response.append(responseLine.trim());
  144. }
  145. }
  146. conn.disconnect();
  147. return response.toString();
  148. }
  149. public static String sendGetRequestWithHeader(String apiUrl, String headerKey, String headerValue) throws IOException {
  150. URL url = new URL(apiUrl);
  151. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  152. conn.setRequestMethod("GET");
  153. conn.setRequestProperty(headerKey, headerValue);
  154. conn.setDoInput(true);
  155. conn.setUseCaches(false);
  156. int responseCode = conn.getResponseCode();
  157. InputStream inputStream = responseCode >= 200 && responseCode < 300
  158. ? conn.getInputStream()
  159. : conn.getErrorStream();
  160. if (inputStream == null) {
  161. conn.disconnect();
  162. return "false";
  163. }
  164. StringBuilder response = new StringBuilder();
  165. try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "utf-8"))) {
  166. String responseLine;
  167. while ((responseLine = br.readLine()) != null) {
  168. response.append(responseLine.trim());
  169. }
  170. }
  171. conn.disconnect();
  172. if (responseCode < 200 || responseCode >= 300) {
  173. return "false";
  174. }
  175. return response.toString();
  176. }
  177. private static String appendSid(String url) {
  178. if (url == null || url.contains("__sid=") || url.contains("/login")) {
  179. return url;
  180. }
  181. try {
  182. String sid = MesClient.sessionid;
  183. if (sid == null || sid.length() == 0) {
  184. return url;
  185. }
  186. return url + (url.contains("?") ? "&" : "?") + "__sid=" + sid;
  187. } catch (Exception e) {
  188. return url;
  189. }
  190. }
  191. }