b10cf09857ce846726c58a4f05468b7ada4ca60f.svn-base 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package com.kingdee.eas.custom.erp.util;
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. import java.net.HttpURLConnection;
  5. import java.net.MalformedURLException;
  6. import java.net.URL;
  7. import com.alibaba.fastjson.JSONObject;
  8. import org.apache.http.HttpEntity;
  9. import org.apache.http.HttpResponse;
  10. import org.apache.http.client.methods.HttpPost;
  11. import org.apache.http.entity.StringEntity;
  12. import org.apache.http.impl.client.DefaultHttpClient;
  13. import org.apache.http.util.EntityUtils;
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import org.apache.http.impl.client.HttpClients;
  17. import org.apache.http.impl.client.CloseableHttpClient;
  18. import org.apache.http.HttpStatus;
  19. import java.io.OutputStream;
  20. public class HttpsReqUtil {
  21. private static Logger log = LoggerFactory.getLogger(HttpsReqUtil.class);
  22. private static CloseableHttpClient httpClient;
  23. public static String postByHttps(String sendUrl, String param) throws Exception {
  24. URL url = new URL(sendUrl);
  25. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  26. conn.setRequestMethod("POST");
  27. conn.setDoOutput(true);
  28. // 设置请求头,如Content-Type
  29. conn.setRequestProperty("Content-Type", "application/json");
  30. // 发送请求参数
  31. //String postParams = "{\"appId\":\"gtiit_sq\",\"appSecret\":\"Kingdee@2024Kingdee@2024\",\"tenantid\":\"ierp-client\",\"accountId\":\"1917417602120941568\"}";
  32. byte[] outputInBytes = param.getBytes("UTF-8");
  33. OutputStream os = conn.getOutputStream();
  34. os.write(outputInBytes);
  35. os.close();
  36. // 获取响应码
  37. int responseCode = conn.getResponseCode();
  38. log.info("postByHttps.POST Response Code :: " + responseCode);
  39. if (responseCode == HttpURLConnection.HTTP_OK) {
  40. // 处理响应
  41. log.info("POST request succeeded");
  42. } else {
  43. log.info("POST request failed");
  44. }
  45. StringBuilder response = new StringBuilder();
  46. // 接收响应内容
  47. if (responseCode == HttpURLConnection.HTTP_OK) {
  48. BufferedReader in = new BufferedReader(new InputStreamReader(
  49. conn.getInputStream()));
  50. String inputLine;
  51. while ((inputLine = in.readLine()) != null) {
  52. response.append(inputLine);
  53. }
  54. in.close();
  55. // 打印结果
  56. log.info("postByHttps.POST Response:" + response.toString());
  57. } else {
  58. log.info("POST NOT Worked");
  59. }
  60. // 关闭连接
  61. conn.disconnect();
  62. return response.toString();
  63. }
  64. public static String getByHttps(String httpUrl, String requestMethod) {
  65. StringBuilder response = new StringBuilder();
  66. try {
  67. URL url = new URL(httpUrl);
  68. HttpURLConnection con = (HttpURLConnection) url.openConnection();
  69. // 设置请求方法
  70. con.setRequestMethod("GET");
  71. // 发送请求并获取响应码
  72. int responseCode = con.getResponseCode();
  73. // 检查响应码是否为200
  74. if (responseCode == HttpURLConnection.HTTP_OK) {
  75. // 获取输入流
  76. BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
  77. String inputLine;
  78. // 读取响应数据,直到没有更多数据
  79. while ((inputLine = in.readLine()) != null) {
  80. response.append(inputLine);
  81. }
  82. in.close();
  83. } else {
  84. // 响应码不是200,打印错误信息
  85. System.out.println("GET request not worked");
  86. }
  87. // 关闭连接
  88. con.disconnect();
  89. } catch (Exception e) {
  90. log.error("getByHttps.e:" + e.getMessage());
  91. }
  92. // 返回响应字符串
  93. return response.toString();
  94. }
  95. /**
  96. * 数据保存接口
  97. * @param sendUrl
  98. * @param param
  99. * @param accessToken
  100. * @return
  101. * @throws Exception
  102. */
  103. public static String postByHttps(String sendUrl, String param,String accessToken) throws Exception {
  104. DefaultHttpClient httpClient = new DefaultHttpClient();
  105. HttpPost httppost = new HttpPost(sendUrl);
  106. String responseMsg = null;
  107. log.info("postByHttps.sendUrl:" + sendUrl);
  108. log.info("postByHttps.param:" + param);
  109. log.info("postByHttps.accessToken:" + accessToken);
  110. try {
  111. StringEntity re = new StringEntity(param, "utf-8");
  112. re.setContentType("application/json");
  113. httppost.setHeader("accessToken", accessToken);
  114. httppost.setEntity(re);
  115. HttpResponse response = httpClient.execute(httppost);
  116. HttpEntity entity = response.getEntity();
  117. responseMsg = EntityUtils.toString(entity, "utf-8");
  118. log.info("postByHttps.responseMsg:" + responseMsg);
  119. } catch (Exception e) {
  120. throw new Exception("请求星瀚接口抛异常:"+e.getMessage());
  121. } finally {
  122. httpClient.getConnectionManager().shutdown();
  123. }
  124. return responseMsg;
  125. }
  126. public static String doPost(String url, JSONObject json) {
  127. try {
  128. httpClient = HttpClients.createDefault();
  129. HttpPost post = new HttpPost(url);
  130. post.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
  131. StringEntity s = new StringEntity(json.toString(), "UTF-8");
  132. s.setContentType("application/json");
  133. //设置请求参数
  134. post.setEntity(s);
  135. HttpResponse response = httpClient.execute(post);
  136. response.setHeader("Content-type", "text/html;charset=UTF-8");
  137. if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
  138. //返回json格式
  139. String res = EntityUtils.toString(response.getEntity(), "utf-8");
  140. return res;
  141. }
  142. } catch (Exception e) {
  143. e.printStackTrace();
  144. } finally {
  145. if (httpClient != null) {
  146. try {
  147. httpClient.close();
  148. } catch (Exception e) {
  149. e.printStackTrace();
  150. }
  151. }
  152. }
  153. return null;
  154. }
  155. }