HttpUtils.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package com.happy.Until;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.google.gson.Gson;
  4. import com.google.gson.reflect.TypeToken;
  5. import com.happy.Model.weixin.WeiXinUtil;
  6. import com.happy.common.http.MyX509TrustManager;
  7. import com.happy.common.wx.WxConfig;
  8. import org.apache.http.HttpEntity;
  9. import org.apache.http.NameValuePair;
  10. import org.apache.http.client.ClientProtocolException;
  11. import org.apache.http.client.config.RequestConfig;
  12. import org.apache.http.client.entity.UrlEncodedFormEntity;
  13. import org.apache.http.client.methods.CloseableHttpResponse;
  14. import org.apache.http.client.methods.HttpGet;
  15. import org.apache.http.client.methods.HttpPost;
  16. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  17. import org.apache.http.conn.ssl.SSLContexts;
  18. import org.apache.http.entity.StringEntity;
  19. import org.apache.http.impl.client.CloseableHttpClient;
  20. import org.apache.http.impl.client.HttpClients;
  21. import org.apache.http.message.BasicNameValuePair;
  22. import org.apache.http.util.EntityUtils;
  23. import javax.net.ssl.*;
  24. import java.io.*;
  25. import java.net.HttpURLConnection;
  26. import java.net.URL;
  27. import java.security.KeyStore;
  28. import java.text.SimpleDateFormat;
  29. import java.util.*;
  30. import java.util.concurrent.TimeUnit;
  31. /**
  32. * http post 提交 和 get请求
  33. *
  34. * @author QT-666
  35. *
  36. */
  37. public class HttpUtils {
  38. private static Integer READ_TIMEOUT = WxConfig.readTimeout;
  39. private static RequestConfig requestConfig = RequestConfig.custom()
  40. .setSocketTimeout(15000).setConnectTimeout(15000)
  41. .setConnectionRequestTimeout(15000).build();
  42. public static String get(String url, Map<String, String> params) {
  43. CloseableHttpClient httpClient = null;
  44. HttpGet httpGet = null;
  45. String re = "";
  46. try {
  47. httpClient = HttpClients.createDefault();
  48. RequestConfig requestConfig = RequestConfig.custom()
  49. .setSocketTimeout(20000).setConnectTimeout(20000).build();
  50. String ps = "";
  51. for (String pKey : params.keySet()) {
  52. if (!"".equals(ps)) {
  53. ps = ps + "&";
  54. }
  55. ps = pKey + "=" + params.get(pKey);
  56. }
  57. if (!"".equals(ps)) {
  58. url = url + "?" + ps;
  59. }
  60. httpGet = new HttpGet(url);
  61. httpGet.setConfig(requestConfig);
  62. CloseableHttpResponse response = httpClient.execute(httpGet);
  63. HttpEntity httpEntity = response.getEntity();
  64. re = EntityUtils.toString(httpEntity, "utf-8");
  65. } catch (ClientProtocolException e) {
  66. e.printStackTrace();
  67. } catch (IOException e) {
  68. e.printStackTrace();
  69. } finally {
  70. try {
  71. if (httpGet != null) {
  72. httpGet.releaseConnection();
  73. }
  74. if (httpClient != null) {
  75. httpClient.close();
  76. }
  77. } catch (IOException e) {
  78. e.printStackTrace();
  79. }
  80. }
  81. return re;
  82. }
  83. /**
  84. * 发送http post请求
  85. *
  86. * @param
  87. *
  88. * @param
  89. *
  90. */
  91. public static String post(String url, Map<String, String> params) throws IOException {
  92. CloseableHttpClient httpClient = null;
  93. HttpPost httpPost = null;
  94. String re = "";
  95. try {
  96. httpClient = HttpClients.createDefault();
  97. RequestConfig requestConfig = RequestConfig.custom()
  98. .setSocketTimeout(20000).setConnectTimeout(20000).build();
  99. httpPost = new HttpPost(url);
  100. httpPost.setConfig(requestConfig);
  101. List<NameValuePair> ps = new ArrayList<NameValuePair>();
  102. for (String pKey : params.keySet()) {
  103. ps.add(new BasicNameValuePair(pKey, params.get(pKey)));
  104. }
  105. httpPost.setEntity(new UrlEncodedFormEntity(ps));
  106. CloseableHttpResponse response = httpClient.execute(httpPost);
  107. HttpEntity httpEntity = response.getEntity();
  108. re = EntityUtils.toString(httpEntity, "utf-8");
  109. } catch (ClientProtocolException e) {
  110. e.printStackTrace();
  111. } catch (IOException e) {
  112. e.printStackTrace();
  113. } finally {
  114. try {
  115. if (httpPost != null) {
  116. httpPost.releaseConnection();
  117. }
  118. if (httpClient != null) {
  119. httpClient.close();
  120. }
  121. } catch (IOException e) {
  122. e.printStackTrace();
  123. }
  124. }
  125. return re;
  126. }
  127. /**
  128. * 发送post请求Https,参数是字符串
  129. *
  130. * @param
  131. * @return
  132. */
  133. public static String post(String url, String body) throws Exception {
  134. String str = "";
  135. CloseableHttpClient httpClient = null;
  136. HttpPost httpPost = null;
  137. try {
  138. httpClient = HttpClients.createDefault();
  139. RequestConfig requestConfig = RequestConfig.custom()
  140. .setSocketTimeout(20000).setConnectTimeout(20000).build();
  141. httpPost = new HttpPost(url);
  142. httpPost.setConfig(requestConfig);
  143. httpPost.setEntity(new StringEntity(body, "utf-8"));
  144. CloseableHttpResponse response = httpClient.execute(httpPost);
  145. HttpEntity httpEntity = response.getEntity();
  146. str = EntityUtils.toString(httpEntity, "utf-8");
  147. } catch (ClientProtocolException e) {
  148. e.printStackTrace();
  149. } catch (IOException e) {
  150. e.printStackTrace();
  151. } finally {
  152. try {
  153. if (httpPost != null) {
  154. httpPost.releaseConnection();
  155. }
  156. if (httpClient != null) {
  157. httpClient.close();
  158. }
  159. } catch (IOException e) {
  160. e.printStackTrace();
  161. }
  162. }
  163. return str;
  164. }
  165. public static String post2(String url, String body) throws Exception {
  166. //商户号
  167. //微信公众平台:“微信支付”--》“商户信息”--》“商户号”,将该值赋值给partner
  168. String partner = WeiXinUtil.account;
  169. //p12证书的位置
  170. //微信公众平台:“微信支付”--》“商户信息”--》“交易数据”--》“详情请登录微信支付商户平台查看”(登录)--》“API安全”--》“API证书”--》“下载证书”
  171. //下载证书后将apiclient_cert.p12放在src目录下面(出于安全考虑,请自行下载自己的证书)
  172. String apiclient_certLocation = "/usr/mbin2/apiclient_cert.p12";
  173. String str = "";
  174. CloseableHttpClient httpClient = null;
  175. HttpPost httpPost = null;
  176. KeyStore keyStore = KeyStore.getInstance("PKCS12");
  177. FileInputStream instream = new FileInputStream(new File(apiclient_certLocation));//P12文件目录
  178. try {
  179. keyStore.load(instream, partner.toCharArray());
  180. HttpsURLConnection httpsUrlConnection = null;
  181. //创建https请求证书
  182. SSLContext sslcontext = SSLContexts.custom()
  183. .loadKeyMaterial(keyStore, partner.toCharArray())//这里也是写密码的
  184. .build();
  185. SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
  186. sslcontext,
  187. new String[] { "TLSv1" },
  188. null,
  189. SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
  190. httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
  191. RequestConfig requestConfig = RequestConfig.custom()
  192. .setSocketTimeout(20000).setConnectTimeout(20000).build();
  193. httpPost = new HttpPost(url);
  194. httpPost.setConfig(requestConfig);
  195. httpPost.setEntity(new StringEntity(body, "utf-8"));
  196. CloseableHttpResponse response = httpClient.execute(httpPost);
  197. HttpEntity httpEntity = response.getEntity();
  198. str = EntityUtils.toString(httpEntity, "utf-8");
  199. } catch (ClientProtocolException e) {
  200. e.printStackTrace();
  201. } catch (IOException e) {
  202. System.out.println("退款异常;"+e.getMessage());
  203. e.printStackTrace();
  204. } finally {
  205. try {
  206. if (httpPost != null) {
  207. httpPost.releaseConnection();
  208. }
  209. if (httpClient != null) {
  210. httpClient.close();
  211. }
  212. instream.close();
  213. } catch (IOException e) {
  214. e.printStackTrace();
  215. }
  216. }
  217. return new String(str.getBytes("iso-8859-1"));
  218. }
  219. public static String get(String strURL) throws Exception {
  220. URL url = new URL(strURL);
  221. HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
  222. httpConn.setRequestMethod("GET");
  223. httpConn.connect();
  224. BufferedReader reader = new BufferedReader(new InputStreamReader(
  225. httpConn.getInputStream(), "utf-8"));
  226. String line;
  227. StringBuffer buffer = new StringBuffer();
  228. while ((line = reader.readLine()) != null) {
  229. buffer.append(line);
  230. }
  231. reader.close();
  232. httpConn.disconnect();
  233. return buffer.toString();
  234. }
  235. public static void main(String[] args) throws Exception {
  236. SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");// 设置日期格式
  237. String time = df.format(new Date());
  238. Map<String, String> req = new HashMap<>();
  239. req.put("operator_id", "000000002");
  240. req.put("operator_secret", "SBC88d0ItR3w6oCTY");
  241. req.put("data_secret", "Epc3hhs0imtVhPSu");
  242. req.put("data_secret_iv", "r57m1sr8Tg2E302L");
  243. req.put("sign_key", "ikAhygeGCRnEdhjgBKf");
  244. req.put("timeStamp", time);
  245. req.put("seq", "0001");
  246. String msg = post("http://183.129.139.222:10022/ems-share-api/queryToken", req);
  247. System.out.println("aaa: "+msg);
  248. }
  249. }