HttpUtils.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package com.happy.Until;
  2. import com.google.gson.Gson;
  3. import com.google.gson.reflect.TypeToken;
  4. import org.apache.http.HttpEntity;
  5. import org.apache.http.NameValuePair;
  6. import org.apache.http.client.ClientProtocolException;
  7. import org.apache.http.client.config.RequestConfig;
  8. import org.apache.http.client.entity.UrlEncodedFormEntity;
  9. import org.apache.http.client.methods.CloseableHttpResponse;
  10. import org.apache.http.client.methods.HttpGet;
  11. import org.apache.http.client.methods.HttpPost;
  12. import org.apache.http.entity.StringEntity;
  13. import org.apache.http.impl.client.CloseableHttpClient;
  14. import org.apache.http.impl.client.HttpClients;
  15. import org.apache.http.message.BasicNameValuePair;
  16. import org.apache.http.util.EntityUtils;
  17. import java.io.BufferedReader;
  18. import java.io.IOException;
  19. import java.io.InputStreamReader;
  20. import java.net.HttpURLConnection;
  21. import java.net.URL;
  22. import java.text.SimpleDateFormat;
  23. import java.util.*;
  24. /**
  25. * http post 提交 和 get请求
  26. *
  27. * @author QT-666
  28. *
  29. */
  30. public class HttpUtils {
  31. private static RequestConfig requestConfig = RequestConfig.custom()
  32. .setSocketTimeout(15000).setConnectTimeout(15000)
  33. .setConnectionRequestTimeout(15000).build();
  34. public static String get(String url, Map<String, String> params) {
  35. CloseableHttpClient httpClient = null;
  36. HttpGet httpGet = null;
  37. String re = "";
  38. try {
  39. httpClient = HttpClients.createDefault();
  40. RequestConfig requestConfig = RequestConfig.custom()
  41. .setSocketTimeout(20000).setConnectTimeout(20000).build();
  42. String ps = "";
  43. for (String pKey : params.keySet()) {
  44. if (!"".equals(ps)) {
  45. ps = ps + "&";
  46. }
  47. ps = pKey + "=" + params.get(pKey);
  48. }
  49. if (!"".equals(ps)) {
  50. url = url + "?" + ps;
  51. }
  52. httpGet = new HttpGet(url);
  53. httpGet.setConfig(requestConfig);
  54. CloseableHttpResponse response = httpClient.execute(httpGet);
  55. HttpEntity httpEntity = response.getEntity();
  56. re = EntityUtils.toString(httpEntity, "utf-8");
  57. } catch (ClientProtocolException e) {
  58. e.printStackTrace();
  59. } catch (IOException e) {
  60. e.printStackTrace();
  61. } finally {
  62. try {
  63. if (httpGet != null) {
  64. httpGet.releaseConnection();
  65. }
  66. if (httpClient != null) {
  67. httpClient.close();
  68. }
  69. } catch (IOException e) {
  70. e.printStackTrace();
  71. }
  72. }
  73. return re;
  74. }
  75. /**
  76. * 发送http post请求
  77. *
  78. * @param
  79. *
  80. * @param
  81. *
  82. */
  83. public static String post(String url, Map<String, String> params) throws IOException {
  84. CloseableHttpClient httpClient = null;
  85. HttpPost httpPost = null;
  86. String re = "";
  87. try {
  88. httpClient = HttpClients.createDefault();
  89. RequestConfig requestConfig = RequestConfig.custom()
  90. .setSocketTimeout(20000).setConnectTimeout(20000).build();
  91. httpPost = new HttpPost(url);
  92. httpPost.setConfig(requestConfig);
  93. List<NameValuePair> ps = new ArrayList<NameValuePair>();
  94. for (String pKey : params.keySet()) {
  95. ps.add(new BasicNameValuePair(pKey, params.get(pKey)));
  96. }
  97. httpPost.setEntity(new UrlEncodedFormEntity(ps));
  98. CloseableHttpResponse response = httpClient.execute(httpPost);
  99. HttpEntity httpEntity = response.getEntity();
  100. re = EntityUtils.toString(httpEntity, "utf-8");
  101. } catch (ClientProtocolException e) {
  102. e.printStackTrace();
  103. } catch (IOException e) {
  104. e.printStackTrace();
  105. } finally {
  106. try {
  107. if (httpPost != null) {
  108. httpPost.releaseConnection();
  109. }
  110. if (httpClient != null) {
  111. httpClient.close();
  112. }
  113. } catch (IOException e) {
  114. e.printStackTrace();
  115. }
  116. }
  117. return re;
  118. }
  119. /**
  120. * 发送post请求Https,参数是字符串
  121. *
  122. * @param httpPost
  123. * @return
  124. */
  125. public static String post(String url, String body) throws Exception {
  126. String str = "";
  127. CloseableHttpClient httpClient = null;
  128. HttpPost httpPost = null;
  129. try {
  130. httpClient = HttpClients.createDefault();
  131. RequestConfig requestConfig = RequestConfig.custom()
  132. .setSocketTimeout(20000).setConnectTimeout(20000).build();
  133. httpPost = new HttpPost(url);
  134. httpPost.setConfig(requestConfig);
  135. httpPost.setEntity(new StringEntity(body, "utf-8"));
  136. CloseableHttpResponse response = httpClient.execute(httpPost);
  137. HttpEntity httpEntity = response.getEntity();
  138. str = EntityUtils.toString(httpEntity, "utf-8");
  139. } catch (ClientProtocolException e) {
  140. e.printStackTrace();
  141. } catch (IOException e) {
  142. e.printStackTrace();
  143. } finally {
  144. try {
  145. if (httpPost != null) {
  146. httpPost.releaseConnection();
  147. }
  148. if (httpClient != null) {
  149. httpClient.close();
  150. }
  151. } catch (IOException e) {
  152. e.printStackTrace();
  153. }
  154. }
  155. return new String(str.getBytes("iso-8859-1"));
  156. }
  157. public static String get(String strURL) throws Exception {
  158. URL url = new URL(strURL);
  159. HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
  160. httpConn.setRequestMethod("GET");
  161. httpConn.connect();
  162. BufferedReader reader = new BufferedReader(new InputStreamReader(
  163. httpConn.getInputStream(), "utf-8"));
  164. String line;
  165. StringBuffer buffer = new StringBuffer();
  166. while ((line = reader.readLine()) != null) {
  167. buffer.append(line);
  168. }
  169. reader.close();
  170. httpConn.disconnect();
  171. return buffer.toString();
  172. }
  173. public static void main(String[] args) throws Exception {
  174. SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");// 设置日期格式
  175. String time = df.format(new Date());
  176. Map<String, String> req = new HashMap<>();
  177. req.put("operator_id", "000000002");
  178. req.put("operator_secret", "SBC88d0ItR3w6oCTY");
  179. req.put("data_secret", "Epc3hhs0imtVhPSu");
  180. req.put("data_secret_iv", "r57m1sr8Tg2E302L");
  181. req.put("sign_key", "ikAhygeGCRnEdhjgBKf");
  182. req.put("timeStamp", time);
  183. req.put("seq", "0001");
  184. String msg = post("http://183.129.139.222:10022/ems-share-api/queryToken", req);
  185. System.out.println("aaa: "+msg);
  186. }
  187. }