package cn.com.ctop.common.utils; import com.alibaba.fastjson.JSONObject; import com.google.gson.Gson; import org.apache.fontbox.ttf.CmapSubtable; import org.apache.fontbox.ttf.GlyphData; import org.apache.fontbox.ttf.TTFParser; import org.apache.fontbox.ttf.TrueTypeFont; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.client.CookieStore; import org.apache.http.client.HttpClient; import org.apache.http.client.config.CookieSpecs; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.TrustStrategy; import org.apache.http.cookie.Cookie; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.BasicCookieStore; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.ssl.SSLContextBuilder; import org.apache.http.util.EntityUtils; import org.eclipse.jetty.util.UrlEncoded; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.net.ssl.SSLContext; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import java.nio.charset.Charset; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.*; public class HttpUtils { private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class); public static CookieStore cookieStore = new BasicCookieStore(); //.setProxy(new HttpHost("106.125.239.179", 4245)) public static Map> fontsMap = new HashMap>(); 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"; public static CloseableHttpClient createSSLClientDefault() { try { SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { //信任所有证书 public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; } }).build(); RequestConfig globalConfig = RequestConfig.custom().setConnectTimeout(30000).setSocketTimeout(30000).setCookieSpec(CookieSpecs.STANDARD).build(); SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(sslContext); return HttpClients.custom().setDefaultCookieStore(cookieStore).setDefaultRequestConfig(globalConfig).setConnectionReuseStrategy((response, context) -> false).setSSLSocketFactory(sslFactory).build(); } catch (Exception e) { logger.error("处理Https证书异常", e); } return HttpClients.createDefault(); } public static String httpPostParamRequest(String url, Map param, Map headers) { HttpClient httpClient = createSSLClientDefault(); String strReturn = ""; try { HttpPost httppost = new HttpPost(url); httppost.setHeader("User-Agent", USER_AGENT); List list = cookieStore.getCookies(); // for (Cookie ck : list) { // System.out.println(ck.getName() + "=" + ck.getValue() + "," + ck.getDomain()); // } // System.out.println("-------------------------------------"); for (String key : headers.keySet()) { httppost.setHeader(key, headers.get(key)); } List paramList = new ArrayList(); for (String key : param.keySet()) { BasicNameValuePair basicNameValuePair = new BasicNameValuePair(key, String.valueOf(param.get(key))); paramList.add(basicNameValuePair); } // 第二步:我们发现Entity是一个接口,所以只能找实现类,发现实现类又需要一个集合,集合的泛型是NameValuePair类型 UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(paramList); // 第一步:通过setEntity 将我们的entity对象传递过去 httppost.setEntity(formEntity); //httppost.setHeader("Content-Type", "application/json"); // httppost.setEntity(new StringEntity(new Gson().toJson(param), Charset.forName("UTF-8"))); HttpEntity respentity; HttpResponse response = httpClient.execute(httppost); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) { String newUrl = response.getFirstHeader("Location").getValue(); return httpPostParamRequest(newUrl, param, headers); } else if (statusCode == HttpStatus.SC_OK) { respentity = response.getEntity(); strReturn = EntityUtils.toString(respentity); return strReturn; } } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); } return strReturn; } public static String httpPostRequest(String url, Map param, Map headers) { // HttpClient httpclient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build(); HttpClient httpClient = createSSLClientDefault(); String strReturn = ""; try { HttpPost httppost = new HttpPost(url); httppost.setHeader("User-Agent", USER_AGENT); httppost.addHeader("Content-Type", "application/json"); List list = cookieStore.getCookies(); // for (Cookie ck : list) { // System.out.println(ck.getName() + "=" + ck.getValue() + "," + ck.getDomain()); // } for (String key : headers.keySet()) { httppost.setHeader(key, headers.get(key)); } //httppost.setHeader("Content-Type", "application/json"); httppost.setEntity(new StringEntity(new Gson().toJson(param), Charset.forName("UTF-8"))); HttpEntity respentity; HttpResponse response = httpClient.execute(httppost); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) { String newUrl = response.getFirstHeader("Location").getValue(); return httpPostRequest(newUrl, param, headers); } else if (statusCode == HttpStatus.SC_OK) { respentity = response.getEntity(); strReturn = EntityUtils.toString(respentity); return strReturn; } } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); } return strReturn; } public static String httpPostRequestTest(String url, String body, Map headers) { // HttpClient httpclient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build(); HttpClient httpClient = createSSLClientDefault(); String strReturn = ""; try { HttpPost httppost = new HttpPost(url); httppost.setHeader("User-Agent", USER_AGENT); httppost.addHeader("Content-Type", "application/json"); List list = cookieStore.getCookies(); for (Cookie ck : list) { System.out.println(ck.getName() + "=" + ck.getValue() + "," + ck.getDomain()); } for (String key : headers.keySet()) { httppost.setHeader(key, headers.get(key)); } //httppost.setHeader("Content-Type", "application/json"); httppost.setEntity(new StringEntity(body, Charset.forName("UTF-8"))); HttpEntity respentity; HttpResponse response = httpClient.execute(httppost); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) { String newUrl = response.getFirstHeader("Location").getValue(); return httpPostRequestTest(newUrl, body, headers); } else if (statusCode == HttpStatus.SC_OK) { respentity = response.getEntity(); strReturn = EntityUtils.toString(respentity); return strReturn; } } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); } return strReturn; } public static String kuaiShouhttpPostRequest(String url, String body, Map headers) { HttpClient httpClient = createSSLClientDefault(); String strReturn = ""; try { HttpPost httppost = new HttpPost(url); if (!Check.isNullMap(headers)) { for (String key : headers.keySet()) { httppost.setHeader(key, headers.get(key)); } } httppost.setHeader("User-Agent", USER_AGENT); if (!Check.isNull(body)) { httppost.setEntity(new StringEntity(body, Charset.forName("UTF-8"))); } HttpEntity respentity; System.err.println(httppost); HttpResponse response = httpClient.execute(httppost); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { respentity = response.getEntity(); strReturn = EntityUtils.toString(respentity); return strReturn; } } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); } return strReturn; } public static String httpPostRequest(String url, JSONObject params, Map headers) { // HttpClient httpclient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build(); HttpClient httpClient = createSSLClientDefault(); String strReturn = ""; try { HttpPost httppost = new HttpPost(url); httppost.setHeader("User-Agent", USER_AGENT); httppost.addHeader("Content-Type", "application/json"); List list = cookieStore.getCookies(); // for (Cookie ck : list) { // System.out.println(ck.getName() + "=" + ck.getValue() + "," + ck.getDomain()); // } for (String key : headers.keySet()) { httppost.setHeader(key, headers.get(key)); } //httppost.setHeader("Content-Type", "application/json"); httppost.setEntity(new StringEntity(params.toJSONString(), Charset.forName("UTF-8"))); HttpEntity respentity; HttpResponse response = httpClient.execute(httppost); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) { String newUrl = response.getFirstHeader("Location").getValue(); return httpPostRequest(newUrl, params, headers); } else if (statusCode == HttpStatus.SC_OK) { respentity = response.getEntity(); strReturn = EntityUtils.toString(respentity); return strReturn; } } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); } return strReturn; } public static String httpGetRequest(String url) { HttpClient httpClient = createSSLClientDefault(); HttpResponse response = null; HttpGet httpGet = new HttpGet(url); // httpPost.setHeader("Content-Type", "application/json"); String result = null; try { httpGet.setHeader("User-Agent", USER_AGENT); List list2 = cookieStore.getCookies(); // for (Cookie ck : list2) { // System.out.println(ck.getName() + "=" + ck.getValue() + "," + ck.getDomain()); // } response = httpClient.execute(httpGet); List list = cookieStore.getCookies(); // for (Cookie ck : list) { // System.out.println(ck.getName() + "=" + ck.getValue() + "," + ck.getDomain()); // } int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) { String newUrl = response.getFirstHeader("Location").getValue(); return httpGetRequest(newUrl); } else if (statusCode == HttpStatus.SC_OK) { BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line = null; StringBuilder builder = new StringBuilder(); while ((line = reader.readLine()) != null) { builder.append(line); } result = builder.toString(); } } catch (Exception e) { e.printStackTrace(); } return result; } public static String httpRequest(String url, String strParams) throws Exception { // HttpClient httpclient = HttpClientBuilder.create().build(); HttpClient httpClient = createSSLClientDefault(); String strReturn = ""; try { HttpPost httppost = new HttpPost(url); httppost.addHeader("Content-Type", "application/json"); httppost.setEntity(new StringEntity(strParams, Charset.forName("UTF-8"))); HttpEntity respentity; HttpResponse response = httpClient.execute(httppost); int code = response.getStatusLine().getStatusCode(); respentity = response.getEntity(); strReturn = EntityUtils.toString(respentity); } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); } return strReturn; } public static String httpRequest(String url, CookieStore cookies, Map parameterMap) throws Exception { // HttpClient httpclient = getHttpclient(); HttpClient httpClient = createSSLClientDefault(); String strReturn = ""; try { HttpPost httppost = new HttpPost(url); httppost.setHeader("Content-Type", "application/json"); UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity( getParam(parameterMap), "UTF-8"); httppost.setEntity(postEntity); HttpEntity respentity; HttpResponse response = httpClient.execute(httppost); respentity = response.getEntity(); strReturn = EntityUtils.toString(respentity); } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); } return strReturn; } public static List getParam(Map parameterMap) { List param = new ArrayList(); Iterator it = parameterMap.entrySet().iterator(); while (it.hasNext()) { Map.Entry parmEntry = (Map.Entry) it.next(); param.add(new BasicNameValuePair((String) parmEntry.getKey(), (String) parmEntry.getValue())); } return param; } public static HttpClient getHttpclient() { RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(180000).setConnectTimeout(30000) .setConnectionRequestTimeout(30000).setStaleConnectionCheckEnabled(true).build(); CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build(); return httpclient; } public static String callingGraph(String url, String json) { HttpClient httpClient = createSSLClientDefault(); HttpResponse response = null; HttpPost httpPost = new HttpPost(url); List list = cookieStore.getCookies(); // cookieStore.clear(); // for (Cookie ck : list) { // // System.out.println(ck.getName() + "=" + ck.getValue() + "," + ck.getDomain()); // } // System.out.println("-------------------------------------"); httpPost.setHeader("Content-Type", "application/json"); String result = null; try { StringEntity entity = new StringEntity(json, "utf-8"); httpPost.setHeader("User-Agent", USER_AGENT); httpPost.setEntity(entity); response = httpClient.execute(httpPost); BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line = null; StringBuilder builder = new StringBuilder(); while ((line = reader.readLine()) != null) { builder.append(line); } // for (Cookie ck : list) { // // System.out.println(ck.getName() + "=" + ck.getValue() + "," + ck.getDomain()); // } result = builder.toString(); } catch (Exception e) { e.printStackTrace(); } return result; } public static Map kuaishouNumberMap; public static Integer getKuaishouNumber(String key) { if (kuaishouNumberMap == null) { kuaishouNumberMap = new HashMap<>(); // kuaishouNumberMap.put("32.0#-6.0#526.0#729.0",0);//735 // kuaishouNumberMap.put("98.0#13.0#363.0#726.0",1);//713 // kuaishouNumberMap.put("32.0#13.0#527.0#732.0",2);//719 // kuaishouNumberMap.put("25.0#-6.0#525.0#730.0",3);//736 // kuaishouNumberMap.put("26.0#13.0#536.0#731.0",4);//718 // kuaishouNumberMap.put("33.0#-5.0#526.0#717.0",5);//722 // kuaishouNumberMap.put("39.0#-5.0#530.0#732.0",6);//737 // kuaishouNumberMap.put("38.0#13.0#536.0#717.0",7);//704 // kuaishouNumberMap.put("33.0#-7.0#525.0#731.0",8);//738 // kuaishouNumberMap.put("37.0#-7.0#521.0#730.0",9);//737 kuaishouNumberMap.put("494.0#735.0", 0); kuaishouNumberMap.put("265.0#713.0", 1); kuaishouNumberMap.put("495.0#719.0", 2); kuaishouNumberMap.put("500.0#736.0", 3); kuaishouNumberMap.put("510.0#718.0", 4); kuaishouNumberMap.put("493.0#722.0", 5); kuaishouNumberMap.put("491.0#737.0", 6); kuaishouNumberMap.put("498.0#704.0", 7); kuaishouNumberMap.put("492.0#738.0", 8); kuaishouNumberMap.put("484.0#737.0", 9); } return kuaishouNumberMap.get(key); } public static void kuaishouTtf(String ttfName) { try { URL url = new URL("https://static.yximgs.com/udata/pkg/kuaishou-front-end-live/" + ttfName); // 创建URL URLConnection urlconn = url.openConnection(); TTFParser ttfParser = new TTFParser(); TrueTypeFont ttf = ttfParser.parse(urlconn.getInputStream()); // System.out.println(new Gson().toJson(ttf.getGlyph().getGlyphs())); GlyphData[] datas = ttf.getGlyph().getGlyphs(); CmapSubtable[] tables = ttf.getCmap().getCmaps(); Gson gson = new Gson(); // System.out.println(gson.toJson(tables)); CmapSubtable table = tables[0]; Map numberMap = new HashMap(); Map fontMap = new HashMap(); for (int i = 0; i <= 13; i++) { GlyphData data = datas[i]; if (data != null) { // System.out.println("getBoundingBox" + data.getBoundingBox()); float fx = data.getBoundingBox().getLowerLeftX(); float fy = data.getBoundingBox().getLowerLeftY(); float rx = data.getBoundingBox().getUpperRightX(); float ry = data.getBoundingBox().getUpperRightY(); Integer num = getKuaishouNumber(String.valueOf(rx - fx) + "#" + String.valueOf(ry - fy)); // if (num != null) { // System.out.println(num); // } fontMap.put(String.valueOf((char) table.getCharCodes(i).get(0).intValue()), String.valueOf(num)); } } // System.out.println(new Gson().toJson(fontMap)); fontsMap.put(ttfName, fontMap); for (GlyphData data : datas) { if (data != null) { // System.out.println("getBoundingBox" + data.getBoundingBox()); float fx = data.getBoundingBox().getLowerLeftX(); float fy = data.getBoundingBox().getLowerLeftY(); float rx = data.getBoundingBox().getUpperRightX(); float ry = data.getBoundingBox().getUpperRightY(); Integer num = getKuaishouNumber(String.valueOf(fx) + "#" + String.valueOf(fy) + "#" + String.valueOf(rx) + "#" + String.valueOf(ry)); // System.out.println(num); } } // System.out.println(new Gson().toJson(numberMap)); } catch (Exception e) { e.printStackTrace(); } } public static String mapParamsSortToStringBySeperator(TreeMap treeMap, String seperator) { String result = ""; if (null != treeMap && !treeMap.isEmpty()) { for (Map.Entry entry : treeMap.entrySet()) { String key = entry.getKey(); String value = UrlEncoded.encodeString((String) entry.getValue()); result += (key + "=" + value + seperator); } if (result.length() > 0) { result = result.substring(0, result.length() - seperator.length()); } } return result; } public static String httpGetRequest(String url, Map headers, TreeMap params) { HttpClient httpClient = createSSLClientDefault(); String strReturn = ""; try { String uri = url + "?" + mapParamsSortToStringBySeperator(params, "&"); HttpGet httpGet = new HttpGet(uri); if (headers != null) { Iterator keyIter = headers.keySet().iterator(); while (keyIter.hasNext()) { String curKey = keyIter.next(); if (curKey != null && headers.get(curKey) != null) { httpGet.addHeader(curKey, headers.get(curKey)); } } } HttpEntity respentity; HttpResponse response = httpClient.execute(httpGet); respentity = response.getEntity(); strReturn = EntityUtils.toString(respentity); } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); } return strReturn; } public static String httpGetRequest(String url, Map headers, JSONObject params) { HttpClient httpClient = createSSLClientDefault(); String strReturn = ""; try { HttpGetWithBodyEntity httpGet = new HttpGetWithBodyEntity(url); if (headers != null) { Iterator keyIter = headers.keySet().iterator(); while (keyIter.hasNext()) { String curKey = keyIter.next(); if (curKey != null && headers.get(curKey) != null) { httpGet.addHeader(curKey, headers.get(curKey)); } } } httpGet.setEntity(new StringEntity(params.toJSONString(), Charset.forName("UTF-8"))); HttpEntity respentity; HttpResponse response = httpClient.execute(httpGet); respentity = response.getEntity(); strReturn = EntityUtils.toString(respentity); } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); } return strReturn; } public static String httpGet(String url, Map paramsMap, Map headers) { String result = null; try { // CloseableHttpClient httpClient = HttpClients.createDefault(); //创建参数列表 HttpClient httpclient = getHttpclient(); StringBuilder postBody = null; if (!Check.isNull(paramsMap)) { postBody = new StringBuilder(); for (Map.Entry entry : paramsMap.entrySet()) { if (entry.getValue() == null) { continue; } postBody.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue().toString(), "utf-8")).append("&"); } if (!paramsMap.isEmpty()) { postBody.deleteCharAt(postBody.length() - 1); } } String urlStr; if (!Check.isNull(postBody)) { urlStr = url + "?" + postBody; } else { urlStr = url; } HttpGet httpget = new HttpGet(urlStr); httpget.setHeader("User-Agent", USER_AGENT); if (!Check.isNullMap(headers)) { for (String key : headers.keySet()) { httpget.setHeader(key, headers.get(key)); } } HttpEntity respEntity; System.err.println(httpget); HttpResponse response = httpclient.execute(httpget); respEntity = response.getEntity(); result = EntityUtils.toString(respEntity); } catch (Exception e) { logger.error("http get请求异常"); e.printStackTrace(); } return result; } }