HttpUtils.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package com.mes.util;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import java.io.*;
  5. import java.net.HttpURLConnection;
  6. import java.net.URISyntaxException;
  7. import java.net.URL;
  8. public class HttpUtils {
  9. public static final Logger log = LoggerFactory.getLogger(HttpUtils.class);
  10. /**
  11. * HTTP请求,正确报文解析,并保存数据到生产记录数据
  12. * @return
  13. * @throws URISyntaxException
  14. */
  15. //http post请求
  16. public static String sendRequest(String urlParam) {
  17. String requestType = "POST";
  18. //根据接收内容返回数据结果
  19. String ret = "";
  20. log.info(urlParam);
  21. HttpURLConnection con = null;
  22. BufferedReader buffer = null;
  23. StringBuffer resultBuffer = null;
  24. try {
  25. URL url = new URL(urlParam);
  26. //得到连接对象
  27. con = (HttpURLConnection) url.openConnection();
  28. //设置请求类型
  29. con.setRequestMethod(requestType);
  30. //设置请求需要返回的数据类型和字符集类型
  31. con.setRequestProperty("Content-Type", "application/json;charset=GBK");
  32. //允许写出
  33. con.setDoOutput(true);
  34. //允许读入
  35. con.setDoInput(true);
  36. //不使用缓存
  37. con.setUseCaches(false);
  38. //得到响应码
  39. int responseCode = con.getResponseCode();
  40. log.info("responseCode="+responseCode);
  41. if(responseCode == HttpURLConnection.HTTP_OK){
  42. //得到响应流
  43. InputStream inputStream = con.getInputStream();
  44. //将响应流转换成字符串
  45. resultBuffer = new StringBuffer();
  46. String line;
  47. buffer = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
  48. while ((line = buffer.readLine()) != null) {
  49. resultBuffer.append(line);
  50. }
  51. log.info(resultBuffer.toString());
  52. ret = resultBuffer.toString();
  53. }else {
  54. //ret = String.valueOf(responseCode);
  55. ret = "false";
  56. }
  57. con.disconnect();
  58. }catch(Exception e) {
  59. //ret = false;
  60. //log.info("e.toString()="+e.toString());
  61. //return e.toString();
  62. //e.printStackTrace();
  63. ret = "false";
  64. }
  65. return ret;
  66. }
  67. public static String sendPostRequest(String urlParam, String params) {
  68. String requestType = "POST";
  69. //根据接收内容返回数据结果
  70. String ret = "";
  71. log.info(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. log.info("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. log.info(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. //log.info("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. URL url = new URL(apiUrl);
  124. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  125. conn.setRequestMethod("POST");
  126. conn.setRequestProperty("Content-Type", "application/json");
  127. conn.setDoOutput(true);
  128. // 发送请求数据
  129. try (OutputStream os = conn.getOutputStream()) {
  130. byte[] input = jsonData.getBytes("utf-8");
  131. os.write(input, 0, input.length);
  132. }
  133. // 获取响应数据
  134. StringBuilder response = new StringBuilder();
  135. int responseCode = conn.getResponseCode();
  136. // System.out.println("Sending POST request to URL: " + apiUrl);
  137. // System.out.println("Post parameters: " + jsonData);
  138. // System.out.println("Response Code: " + responseCode);
  139. try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"))) {
  140. String responseLine;
  141. while ((responseLine = br.readLine()) != null) {
  142. response.append(responseLine.trim());
  143. }
  144. }
  145. conn.disconnect();
  146. return response.toString();
  147. }
  148. }