package com.fr.plugin; import com.fr.log.FineLoggerFactory; import com.fr.third.org.apache.commons.collections4.MapUtils; import com.fr.third.org.apache.commons.lang3.StringUtils; import com.fr.third.org.apache.http.HttpEntity; import com.fr.third.org.apache.http.HttpResponse; import com.fr.third.org.apache.http.HttpStatus; import com.fr.third.org.apache.http.NameValuePair; import com.fr.third.org.apache.http.client.HttpClient; import com.fr.third.org.apache.http.client.entity.UrlEncodedFormEntity; import com.fr.third.org.apache.http.client.methods.HttpGet; import com.fr.third.org.apache.http.client.methods.HttpPost; import com.fr.third.org.apache.http.client.utils.URLEncodedUtils; import com.fr.third.org.apache.http.config.Registry; import com.fr.third.org.apache.http.config.RegistryBuilder; import com.fr.third.org.apache.http.conn.socket.ConnectionSocketFactory; import com.fr.third.org.apache.http.conn.socket.PlainConnectionSocketFactory; import com.fr.third.org.apache.http.conn.ssl.SSLConnectionSocketFactory; import com.fr.third.org.apache.http.entity.StringEntity; import com.fr.third.org.apache.http.impl.client.CloseableHttpClient; import com.fr.third.org.apache.http.impl.client.HttpClients; import com.fr.third.org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import com.fr.third.org.apache.http.message.BasicNameValuePair; import com.fr.third.org.apache.http.util.EntityUtils; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.security.Principal; import java.security.SecureRandom; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.List; import java.util.Map; public class HttpUtil { private static CloseableHttpClient wrapClient() { try { SSLContext ctx = SSLContext.getInstance("TLS"); X509TrustManager tm = new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { if ((chain == null) || (chain.length == 0)) { throw new IllegalArgumentException("null or zero-length certificate chain"); } if ((authType == null) || (authType.length() == 0)) { throw new IllegalArgumentException("null or zero-length authentication type"); } boolean br = false; Principal principal; for (X509Certificate x509Certificate : chain) { principal = x509Certificate.getSubjectX500Principal(); if (principal != null) { br = true; break; } } if (!(br)) { throw new CertificateException("服务端证书验证失败!"); } } @Override public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } }; ctx.init(null, new TrustManager[]{tm}, new SecureRandom()); SSLConnectionSocketFactory ssf = new SSLConnectionSocketFactory(ctx, SSLConnectionSocketFactory.getDefaultHostnameVerifier()); Registry registry = RegistryBuilder.create() .register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", ssf).build(); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry); return HttpClients.custom().setConnectionManager(cm).build(); } catch (Exception ex) { FineLoggerFactory.getLogger().error("httpclient初始化失败", ex); return null; } } public static String postEntity(String url, HttpEntity entity, Map headers) { HttpClient httpClient = createHttpClient(url); HttpPost requestBase = new HttpPost(url); requestBase.setEntity(entity); try { if (MapUtils.isNotEmpty(headers)) { for (Map.Entry entry : headers.entrySet()) { requestBase.setHeader(entry.getKey(), entry.getValue()); } } HttpResponse response = httpClient.execute(requestBase); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { return EntityUtils.toString(response.getEntity(), "UTF-8"); } } catch (IOException e) { FineLoggerFactory.getLogger().error("http接口调用异常:url is::" + url, e); } return ""; } public static String get(String url, Map headers) { CloseableHttpClient httpClient = createHttpClient(url); try { HttpGet httpGet = new HttpGet(url); httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0"); HttpResponse response; if (MapUtils.isNotEmpty(headers)) { for (Map.Entry entry : headers.entrySet()) { httpGet.setHeader(entry.getKey(), entry.getValue()); } } response = httpClient.execute(httpGet); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { return EntityUtils.toString(response.getEntity(), "UTF-8"); } } catch (IOException e) { FineLoggerFactory.getLogger().error("http接口调用异常:url is::" + url, e); } return ""; } public static String get(String url, Map params, Map headers) { StringBuilder sb = new StringBuilder(url); if (!url.contains("?")) { sb.append("?"); } else { sb.append("&"); } List nvps = new ArrayList<>(); if (params != null) { for (Map.Entry entry : params.entrySet()) { nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } } String uri = sb.append(URLEncodedUtils.format(nvps, "UTF-8")).toString(); return get(uri, headers); } public static String getNoHeader(String url, Map params) { return get(url, params, null); } public static String post(String url, Map params) { return post(url, params, null); } public static String post(String url, Map params, Map headers) { CloseableHttpClient httpClient = createHttpClient(url); List nvps = new ArrayList<>(); if (params != null) { for (Map.Entry entry : params.entrySet()) { nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } } final HttpPost post = new HttpPost(url); try { if (MapUtils.isNotEmpty(headers)) { for (Map.Entry entry : headers.entrySet()) { post.setHeader(entry.getKey(), entry.getValue()); } } post.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8")); HttpResponse response = httpClient.execute(post); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { return EntityUtils.toString(response.getEntity()); } } catch (IOException e) { e.printStackTrace(); } finally { try { httpClient.close(); } catch (IOException e) { FineLoggerFactory.getLogger().error("http接口调用异常:url is::" + url, e); } } return ""; } public static String post(String url, String postEntity) { CloseableHttpClient httpClient = createHttpClient(url); HttpPost post = new HttpPost(url); try { post.setEntity(new StringEntity(postEntity)); HttpResponse response = httpClient.execute(post); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { return EntityUtils.toString(response.getEntity()); } } catch (IOException e) { e.printStackTrace(); } finally { try { httpClient.close(); } catch (IOException e) { FineLoggerFactory.getLogger().error("http接口调用异常:url is::" + url, e); } } return ""; } private static CloseableHttpClient createHttpClient(String url) { CloseableHttpClient httpClient; if (url.startsWith("https")) { httpClient = wrapClient(); } else { httpClient = HttpClients.createDefault(); } return httpClient; } public static boolean inContainURL(HttpServletRequest request, String url) { boolean result = false; if (url != null && !"".equals(url.trim())) { String[] urlArr = url.split(";"); StringBuffer reqUrl = new StringBuffer(request.getRequestURL()); for (int i = 0; i < urlArr.length; ++i) { if (reqUrl.indexOf(urlArr[i]) > 1) { result = true; break; } } } String op = request.getParameter("op"); if (StringUtils.isNotBlank(op)) { result=true; } return result; } }