HttpUtils.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. package cn.com.ctop.common.utils;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.google.gson.Gson;
  4. import org.apache.fontbox.ttf.CmapSubtable;
  5. import org.apache.fontbox.ttf.GlyphData;
  6. import org.apache.fontbox.ttf.TTFParser;
  7. import org.apache.fontbox.ttf.TrueTypeFont;
  8. import org.apache.http.HttpEntity;
  9. import org.apache.http.HttpResponse;
  10. import org.apache.http.HttpStatus;
  11. import org.apache.http.NameValuePair;
  12. import org.apache.http.client.CookieStore;
  13. import org.apache.http.client.HttpClient;
  14. import org.apache.http.client.config.CookieSpecs;
  15. import org.apache.http.client.config.RequestConfig;
  16. import org.apache.http.client.entity.UrlEncodedFormEntity;
  17. import org.apache.http.client.methods.HttpGet;
  18. import org.apache.http.client.methods.HttpPost;
  19. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  20. import org.apache.http.conn.ssl.TrustStrategy;
  21. import org.apache.http.cookie.Cookie;
  22. import org.apache.http.entity.StringEntity;
  23. import org.apache.http.impl.client.BasicCookieStore;
  24. import org.apache.http.impl.client.CloseableHttpClient;
  25. import org.apache.http.impl.client.HttpClients;
  26. import org.apache.http.message.BasicNameValuePair;
  27. import org.apache.http.ssl.SSLContextBuilder;
  28. import org.apache.http.util.EntityUtils;
  29. import org.eclipse.jetty.util.UrlEncoded;
  30. import org.slf4j.Logger;
  31. import org.slf4j.LoggerFactory;
  32. import javax.net.ssl.SSLContext;
  33. import java.io.BufferedReader;
  34. import java.io.InputStreamReader;
  35. import java.net.URL;
  36. import java.net.URLConnection;
  37. import java.net.URLEncoder;
  38. import java.nio.charset.Charset;
  39. import java.security.cert.CertificateException;
  40. import java.security.cert.X509Certificate;
  41. import java.util.*;
  42. public class HttpUtils {
  43. private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);
  44. public static CookieStore cookieStore = new BasicCookieStore();
  45. //.setProxy(new HttpHost("106.125.239.179", 4245))
  46. public static Map<String, Map<String, String>> fontsMap = new HashMap<String, Map<String, String>>();
  47. public static String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36";
  48. public static CloseableHttpClient createSSLClientDefault() {
  49. try {
  50. SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
  51. //信任所有证书
  52. public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
  53. return true;
  54. }
  55. }).build();
  56. RequestConfig globalConfig = RequestConfig.custom().setConnectTimeout(30000).setSocketTimeout(30000).setCookieSpec(CookieSpecs.STANDARD).build();
  57. SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(sslContext);
  58. return HttpClients.custom().setDefaultCookieStore(cookieStore).setDefaultRequestConfig(globalConfig).setConnectionReuseStrategy((response, context) -> false).setSSLSocketFactory(sslFactory).build();
  59. } catch (Exception e) {
  60. logger.error("处理Https证书异常", e);
  61. }
  62. return HttpClients.createDefault();
  63. }
  64. public static String httpPostParamRequest(String url, Map<String, Object> param, Map<String, String> headers) {
  65. HttpClient httpClient = createSSLClientDefault();
  66. String strReturn = "";
  67. try {
  68. HttpPost httppost = new HttpPost(url);
  69. httppost.setHeader("User-Agent", USER_AGENT);
  70. List<Cookie> list = cookieStore.getCookies();
  71. for (Cookie ck : list) {
  72. System.out.println(ck.getName() + "=" + ck.getValue() + "," + ck.getDomain());
  73. }
  74. System.out.println("-------------------------------------");
  75. for (String key : headers.keySet()) {
  76. httppost.setHeader(key, headers.get(key));
  77. }
  78. List<NameValuePair> paramList = new ArrayList<NameValuePair>();
  79. for (String key : param.keySet()) {
  80. BasicNameValuePair basicNameValuePair = new BasicNameValuePair(key, String.valueOf(param.get(key)));
  81. paramList.add(basicNameValuePair);
  82. }
  83. // 第二步:我们发现Entity是一个接口,所以只能找实现类,发现实现类又需要一个集合,集合的泛型是NameValuePair类型
  84. UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(paramList);
  85. // 第一步:通过setEntity 将我们的entity对象传递过去
  86. httppost.setEntity(formEntity);
  87. //httppost.setHeader("Content-Type", "application/json");
  88. // httppost.setEntity(new StringEntity(new Gson().toJson(param), Charset.forName("UTF-8")));
  89. HttpEntity respentity;
  90. HttpResponse response = httpClient.execute(httppost);
  91. int statusCode = response.getStatusLine().getStatusCode();
  92. if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
  93. String newUrl = response.getFirstHeader("Location").getValue();
  94. return httpPostParamRequest(newUrl, param, headers);
  95. } else if (statusCode == HttpStatus.SC_OK) {
  96. respentity = response.getEntity();
  97. strReturn = EntityUtils.toString(respentity);
  98. return strReturn;
  99. }
  100. } catch (Exception e) {
  101. e.printStackTrace();
  102. logger.error(e.getMessage());
  103. }
  104. return strReturn;
  105. }
  106. public static String httpPostRequest(String url, Map<String, Object> param, Map<String, String> headers) {
  107. // HttpClient httpclient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();
  108. HttpClient httpClient = createSSLClientDefault();
  109. String strReturn = "";
  110. try {
  111. HttpPost httppost = new HttpPost(url);
  112. httppost.setHeader("User-Agent", USER_AGENT);
  113. httppost.addHeader("Content-Type", "application/json");
  114. List<Cookie> list = cookieStore.getCookies();
  115. for (Cookie ck : list) {
  116. System.out.println(ck.getName() + "=" + ck.getValue() + "," + ck.getDomain());
  117. }
  118. for (String key : headers.keySet()) {
  119. httppost.setHeader(key, headers.get(key));
  120. }
  121. //httppost.setHeader("Content-Type", "application/json");
  122. httppost.setEntity(new StringEntity(new Gson().toJson(param), Charset.forName("UTF-8")));
  123. HttpEntity respentity;
  124. HttpResponse response = httpClient.execute(httppost);
  125. int statusCode = response.getStatusLine().getStatusCode();
  126. if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
  127. String newUrl = response.getFirstHeader("Location").getValue();
  128. return httpPostRequest(newUrl, param, headers);
  129. } else if (statusCode == HttpStatus.SC_OK) {
  130. respentity = response.getEntity();
  131. strReturn = EntityUtils.toString(respentity);
  132. return strReturn;
  133. }
  134. } catch (Exception e) {
  135. e.printStackTrace();
  136. logger.error(e.getMessage());
  137. }
  138. return strReturn;
  139. }
  140. public static String httpPostRequestTest(String url, String body, Map<String, String> headers) {
  141. // HttpClient httpclient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();
  142. HttpClient httpClient = createSSLClientDefault();
  143. String strReturn = "";
  144. try {
  145. HttpPost httppost = new HttpPost(url);
  146. httppost.setHeader("User-Agent", USER_AGENT);
  147. httppost.addHeader("Content-Type", "application/json");
  148. List<Cookie> list = cookieStore.getCookies();
  149. for (Cookie ck : list) {
  150. System.out.println(ck.getName() + "=" + ck.getValue() + "," + ck.getDomain());
  151. }
  152. for (String key : headers.keySet()) {
  153. httppost.setHeader(key, headers.get(key));
  154. }
  155. //httppost.setHeader("Content-Type", "application/json");
  156. httppost.setEntity(new StringEntity(body, Charset.forName("UTF-8")));
  157. HttpEntity respentity;
  158. HttpResponse response = httpClient.execute(httppost);
  159. int statusCode = response.getStatusLine().getStatusCode();
  160. if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
  161. String newUrl = response.getFirstHeader("Location").getValue();
  162. return httpPostRequestTest(newUrl, body, headers);
  163. } else if (statusCode == HttpStatus.SC_OK) {
  164. respentity = response.getEntity();
  165. strReturn = EntityUtils.toString(respentity);
  166. return strReturn;
  167. }
  168. } catch (Exception e) {
  169. e.printStackTrace();
  170. logger.error(e.getMessage());
  171. }
  172. return strReturn;
  173. }
  174. public static String kuaiShouhttpPostRequest(String url, String body, Map<String, String> headers) {
  175. HttpClient httpClient = createSSLClientDefault();
  176. String strReturn = "";
  177. try {
  178. HttpPost httppost = new HttpPost(url);
  179. if (!Check.isNullMap(headers)) {
  180. for (String key : headers.keySet()) {
  181. httppost.setHeader(key, headers.get(key));
  182. }
  183. }
  184. httppost.setHeader("User-Agent", USER_AGENT);
  185. if (!Check.isNull(body)) {
  186. httppost.setEntity(new StringEntity(body, Charset.forName("UTF-8")));
  187. }
  188. HttpEntity respentity;
  189. System.err.println(httppost);
  190. HttpResponse response = httpClient.execute(httppost);
  191. int statusCode = response.getStatusLine().getStatusCode();
  192. if (statusCode == HttpStatus.SC_OK) {
  193. respentity = response.getEntity();
  194. strReturn = EntityUtils.toString(respentity);
  195. return strReturn;
  196. }
  197. } catch (Exception e) {
  198. e.printStackTrace();
  199. logger.error(e.getMessage());
  200. }
  201. return strReturn;
  202. }
  203. public static String httpPostRequest(String url, JSONObject params, Map<String, String> headers) {
  204. // HttpClient httpclient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();
  205. HttpClient httpClient = createSSLClientDefault();
  206. String strReturn = "";
  207. try {
  208. HttpPost httppost = new HttpPost(url);
  209. httppost.setHeader("User-Agent", USER_AGENT);
  210. httppost.addHeader("Content-Type", "application/json");
  211. List<Cookie> list = cookieStore.getCookies();
  212. for (Cookie ck : list) {
  213. System.out.println(ck.getName() + "=" + ck.getValue() + "," + ck.getDomain());
  214. }
  215. for (String key : headers.keySet()) {
  216. httppost.setHeader(key, headers.get(key));
  217. }
  218. //httppost.setHeader("Content-Type", "application/json");
  219. httppost.setEntity(new StringEntity(params.toJSONString(), Charset.forName("UTF-8")));
  220. HttpEntity respentity;
  221. HttpResponse response = httpClient.execute(httppost);
  222. int statusCode = response.getStatusLine().getStatusCode();
  223. if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
  224. String newUrl = response.getFirstHeader("Location").getValue();
  225. return httpPostRequest(newUrl, params, headers);
  226. } else if (statusCode == HttpStatus.SC_OK) {
  227. respentity = response.getEntity();
  228. strReturn = EntityUtils.toString(respentity);
  229. return strReturn;
  230. }
  231. } catch (Exception e) {
  232. e.printStackTrace();
  233. logger.error(e.getMessage());
  234. }
  235. return strReturn;
  236. }
  237. public static String httpGetRequest(String url) {
  238. HttpClient httpClient = createSSLClientDefault();
  239. HttpResponse response = null;
  240. HttpGet httpGet = new HttpGet(url);
  241. // httpPost.setHeader("Content-Type", "application/json");
  242. String result = null;
  243. try {
  244. httpGet.setHeader("User-Agent", USER_AGENT);
  245. List<Cookie> list2 = cookieStore.getCookies();
  246. for (Cookie ck : list2) {
  247. System.out.println(ck.getName() + "=" + ck.getValue() + "," + ck.getDomain());
  248. }
  249. response = httpClient.execute(httpGet);
  250. List<Cookie> list = cookieStore.getCookies();
  251. for (Cookie ck : list) {
  252. System.out.println(ck.getName() + "=" + ck.getValue() + "," + ck.getDomain());
  253. }
  254. int statusCode = response.getStatusLine().getStatusCode();
  255. if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
  256. String newUrl = response.getFirstHeader("Location").getValue();
  257. return httpGetRequest(newUrl);
  258. } else if (statusCode == HttpStatus.SC_OK) {
  259. BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  260. String line = null;
  261. StringBuilder builder = new StringBuilder();
  262. while ((line = reader.readLine()) != null) {
  263. builder.append(line);
  264. }
  265. result = builder.toString();
  266. }
  267. } catch (Exception e) {
  268. e.printStackTrace();
  269. }
  270. return result;
  271. }
  272. public static String httpRequest(String url, String strParams) throws Exception {
  273. // HttpClient httpclient = HttpClientBuilder.create().build();
  274. HttpClient httpClient = createSSLClientDefault();
  275. String strReturn = "";
  276. try {
  277. HttpPost httppost = new HttpPost(url);
  278. httppost.addHeader("Content-Type", "application/json");
  279. httppost.setEntity(new StringEntity(strParams, Charset.forName("UTF-8")));
  280. HttpEntity respentity;
  281. HttpResponse response = httpClient.execute(httppost);
  282. int code = response.getStatusLine().getStatusCode();
  283. respentity = response.getEntity();
  284. strReturn = EntityUtils.toString(respentity);
  285. } catch (Exception e) {
  286. e.printStackTrace();
  287. logger.error(e.getMessage());
  288. }
  289. return strReturn;
  290. }
  291. public static String httpRequest(String url, CookieStore cookies, Map<String, String> parameterMap) throws Exception {
  292. // HttpClient httpclient = getHttpclient();
  293. HttpClient httpClient = createSSLClientDefault();
  294. String strReturn = "";
  295. try {
  296. HttpPost httppost = new HttpPost(url);
  297. httppost.setHeader("Content-Type", "application/json");
  298. UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(
  299. getParam(parameterMap), "UTF-8");
  300. httppost.setEntity(postEntity);
  301. HttpEntity respentity;
  302. HttpResponse response = httpClient.execute(httppost);
  303. respentity = response.getEntity();
  304. strReturn = EntityUtils.toString(respentity);
  305. } catch (Exception e) {
  306. e.printStackTrace();
  307. logger.error(e.getMessage());
  308. }
  309. return strReturn;
  310. }
  311. public static List<NameValuePair> getParam(Map parameterMap) {
  312. List<NameValuePair> param = new ArrayList<NameValuePair>();
  313. Iterator it = parameterMap.entrySet().iterator();
  314. while (it.hasNext()) {
  315. Map.Entry parmEntry = (Map.Entry) it.next();
  316. param.add(new BasicNameValuePair((String) parmEntry.getKey(),
  317. (String) parmEntry.getValue()));
  318. }
  319. return param;
  320. }
  321. public static HttpClient getHttpclient() {
  322. RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(180000).setConnectTimeout(30000)
  323. .setConnectionRequestTimeout(30000).setStaleConnectionCheckEnabled(true).build();
  324. CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
  325. return httpclient;
  326. }
  327. public static String callingGraph(String url, String json) {
  328. HttpClient httpClient = createSSLClientDefault();
  329. HttpResponse response = null;
  330. HttpPost httpPost = new HttpPost(url);
  331. List<Cookie> list = cookieStore.getCookies();
  332. // cookieStore.clear();
  333. for (Cookie ck : list) {
  334. System.out.println(ck.getName() + "=" + ck.getValue() + "," + ck.getDomain());
  335. }
  336. System.out.println("-------------------------------------");
  337. httpPost.setHeader("Content-Type", "application/json");
  338. String result = null;
  339. try {
  340. StringEntity entity = new StringEntity(json, "utf-8");
  341. httpPost.setHeader("User-Agent", USER_AGENT);
  342. httpPost.setEntity(entity);
  343. response = httpClient.execute(httpPost);
  344. BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  345. String line = null;
  346. StringBuilder builder = new StringBuilder();
  347. while ((line = reader.readLine()) != null) {
  348. builder.append(line);
  349. }
  350. for (Cookie ck : list) {
  351. System.out.println(ck.getName() + "=" + ck.getValue() + "," + ck.getDomain());
  352. }
  353. result = builder.toString();
  354. } catch (Exception e) {
  355. e.printStackTrace();
  356. }
  357. return result;
  358. }
  359. public static Map<String, Integer> kuaishouNumberMap;
  360. public static Integer getKuaishouNumber(String key) {
  361. if (kuaishouNumberMap == null) {
  362. kuaishouNumberMap = new HashMap<>();
  363. // kuaishouNumberMap.put("32.0#-6.0#526.0#729.0",0);//735
  364. // kuaishouNumberMap.put("98.0#13.0#363.0#726.0",1);//713
  365. // kuaishouNumberMap.put("32.0#13.0#527.0#732.0",2);//719
  366. // kuaishouNumberMap.put("25.0#-6.0#525.0#730.0",3);//736
  367. // kuaishouNumberMap.put("26.0#13.0#536.0#731.0",4);//718
  368. // kuaishouNumberMap.put("33.0#-5.0#526.0#717.0",5);//722
  369. // kuaishouNumberMap.put("39.0#-5.0#530.0#732.0",6);//737
  370. // kuaishouNumberMap.put("38.0#13.0#536.0#717.0",7);//704
  371. // kuaishouNumberMap.put("33.0#-7.0#525.0#731.0",8);//738
  372. // kuaishouNumberMap.put("37.0#-7.0#521.0#730.0",9);//737
  373. kuaishouNumberMap.put("494.0#735.0", 0);
  374. kuaishouNumberMap.put("265.0#713.0", 1);
  375. kuaishouNumberMap.put("495.0#719.0", 2);
  376. kuaishouNumberMap.put("500.0#736.0", 3);
  377. kuaishouNumberMap.put("510.0#718.0", 4);
  378. kuaishouNumberMap.put("493.0#722.0", 5);
  379. kuaishouNumberMap.put("491.0#737.0", 6);
  380. kuaishouNumberMap.put("498.0#704.0", 7);
  381. kuaishouNumberMap.put("492.0#738.0", 8);
  382. kuaishouNumberMap.put("484.0#737.0", 9);
  383. }
  384. return kuaishouNumberMap.get(key);
  385. }
  386. public static void kuaishouTtf(String ttfName) {
  387. try {
  388. URL url = new URL("https://static.yximgs.com/udata/pkg/kuaishou-front-end-live/" + ttfName); // 创建URL
  389. URLConnection urlconn = url.openConnection();
  390. TTFParser ttfParser = new TTFParser();
  391. TrueTypeFont ttf = ttfParser.parse(urlconn.getInputStream());
  392. System.out.println(new Gson().toJson(ttf.getGlyph().getGlyphs()));
  393. GlyphData[] datas = ttf.getGlyph().getGlyphs();
  394. CmapSubtable[] tables = ttf.getCmap().getCmaps();
  395. Gson gson = new Gson();
  396. System.out.println(gson.toJson(tables));
  397. CmapSubtable table = tables[0];
  398. Map<String, Integer> numberMap = new HashMap<String, Integer>();
  399. Map<String, String> fontMap = new HashMap<String, String>();
  400. for (int i = 0; i <= 13; i++) {
  401. GlyphData data = datas[i];
  402. if (data != null) {
  403. System.out.println("getBoundingBox" + data.getBoundingBox());
  404. float fx = data.getBoundingBox().getLowerLeftX();
  405. float fy = data.getBoundingBox().getLowerLeftY();
  406. float rx = data.getBoundingBox().getUpperRightX();
  407. float ry = data.getBoundingBox().getUpperRightY();
  408. Integer num = getKuaishouNumber(String.valueOf(rx - fx) + "#" + String.valueOf(ry - fy));
  409. if (num != null) {
  410. System.out.println(num);
  411. }
  412. fontMap.put(String.valueOf((char) table.getCharCodes(i).get(0).intValue()), String.valueOf(num));
  413. }
  414. }
  415. System.out.println(new Gson().toJson(fontMap));
  416. fontsMap.put(ttfName, fontMap);
  417. for (GlyphData data : datas) {
  418. if (data != null) {
  419. System.out.println("getBoundingBox" + data.getBoundingBox());
  420. float fx = data.getBoundingBox().getLowerLeftX();
  421. float fy = data.getBoundingBox().getLowerLeftY();
  422. float rx = data.getBoundingBox().getUpperRightX();
  423. float ry = data.getBoundingBox().getUpperRightY();
  424. Integer num = getKuaishouNumber(String.valueOf(fx) + "#" + String.valueOf(fy) + "#" + String.valueOf(rx) + "#" + String.valueOf(ry));
  425. System.out.println(num);
  426. }
  427. }
  428. System.out.println(new Gson().toJson(numberMap));
  429. } catch (Exception e) {
  430. e.printStackTrace();
  431. }
  432. }
  433. public static String mapParamsSortToStringBySeperator(TreeMap<String, Object> treeMap, String seperator) {
  434. String result = "";
  435. if (null != treeMap && !treeMap.isEmpty()) {
  436. for (Map.Entry<String, Object> entry : treeMap.entrySet()) {
  437. String key = entry.getKey();
  438. String value = UrlEncoded.encodeString((String) entry.getValue());
  439. result += (key + "=" + value + seperator);
  440. }
  441. if (result.length() > 0) {
  442. result = result.substring(0, result.length() - seperator.length());
  443. }
  444. }
  445. return result;
  446. }
  447. public static String httpGetRequest(String url, Map<String, String> headers, TreeMap<String, Object> params) {
  448. HttpClient httpClient = createSSLClientDefault();
  449. String strReturn = "";
  450. try {
  451. String uri = url + "?" + mapParamsSortToStringBySeperator(params, "&");
  452. HttpGet httpGet = new HttpGet(uri);
  453. if (headers != null) {
  454. Iterator<String> keyIter = headers.keySet().iterator();
  455. while (keyIter.hasNext()) {
  456. String curKey = keyIter.next();
  457. if (curKey != null && headers.get(curKey) != null) {
  458. httpGet.addHeader(curKey, headers.get(curKey));
  459. }
  460. }
  461. }
  462. HttpEntity respentity;
  463. HttpResponse response = httpClient.execute(httpGet);
  464. respentity = response.getEntity();
  465. strReturn = EntityUtils.toString(respentity);
  466. } catch (Exception e) {
  467. e.printStackTrace();
  468. logger.error(e.getMessage());
  469. }
  470. return strReturn;
  471. }
  472. public static String httpGetRequest(String url, Map<String, String> headers, JSONObject params) {
  473. HttpClient httpClient = createSSLClientDefault();
  474. String strReturn = "";
  475. try {
  476. HttpGetWithBodyEntity httpGet = new HttpGetWithBodyEntity(url);
  477. if (headers != null) {
  478. Iterator<String> keyIter = headers.keySet().iterator();
  479. while (keyIter.hasNext()) {
  480. String curKey = keyIter.next();
  481. if (curKey != null && headers.get(curKey) != null) {
  482. httpGet.addHeader(curKey, headers.get(curKey));
  483. }
  484. }
  485. }
  486. httpGet.setEntity(new StringEntity(params.toJSONString(), Charset.forName("UTF-8")));
  487. HttpEntity respentity;
  488. HttpResponse response = httpClient.execute(httpGet);
  489. respentity = response.getEntity();
  490. strReturn = EntityUtils.toString(respentity);
  491. } catch (Exception e) {
  492. e.printStackTrace();
  493. logger.error(e.getMessage());
  494. }
  495. return strReturn;
  496. }
  497. public static String httpGet(String url, Map<String, Object> paramsMap, Map<String, String> headers) {
  498. String result = null;
  499. try {
  500. // CloseableHttpClient httpClient = HttpClients.createDefault();
  501. //创建参数列表
  502. HttpClient httpclient = getHttpclient();
  503. StringBuilder postBody = null;
  504. if (!Check.isNull(paramsMap)) {
  505. postBody = new StringBuilder();
  506. for (Map.Entry<String, Object> entry : paramsMap.entrySet()) {
  507. if (entry.getValue() == null) {
  508. continue;
  509. }
  510. postBody.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue().toString(),
  511. "utf-8")).append("&");
  512. }
  513. if (!paramsMap.isEmpty()) {
  514. postBody.deleteCharAt(postBody.length() - 1);
  515. }
  516. }
  517. String urlStr;
  518. if (!Check.isNull(postBody)) {
  519. urlStr = url + "?" + postBody;
  520. } else {
  521. urlStr = url;
  522. }
  523. HttpGet httpget = new HttpGet(urlStr);
  524. httpget.setHeader("User-Agent", USER_AGENT);
  525. if (!Check.isNullMap(headers)) {
  526. for (String key : headers.keySet()) {
  527. httpget.setHeader(key, headers.get(key));
  528. }
  529. }
  530. HttpEntity respEntity;
  531. System.err.println(httpget);
  532. HttpResponse response = httpclient.execute(httpget);
  533. respEntity = response.getEntity();
  534. result = EntityUtils.toString(respEntity);
  535. } catch (Exception e) {
  536. logger.error("http get请求异常");
  537. e.printStackTrace();
  538. }
  539. return result;
  540. }
  541. }