Http && Https(绕过证书) 请求工具类 (Java)
public class HttpRequestUtil { private static final String HTTP = "http"; private static final String HTTPS = "https"; private static SSLConnectionSocketFactory sslsf = null; private static PoolingHttpClientConnectionManager cm = null; private static SSLContextBuilder builder = null; static { try { builder = new SSLContextBuilder(); // 全部信任 不做身份鉴定 builder.loadTrustMaterial(null, new TrustStrategy() { @Override public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { return true; } }); sslsf = new SSLConnectionSocketFactory(builder.build(), new String[]{"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2"}, null, NoopHostnameVerifier.INSTANCE); Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() .register(HTTP, new PlainConnectionSocketFactory()) .register(HTTPS, sslsf) .build(); cm = new PoolingHttpClientConnectionManager(registry); cm.setMaxTotal(200);//max connection 设置最大连接数 } catch (Exception e) { e.printStackTrace(); } } /** * httpClient post请求 * @param url 请求url * @param params 请求参数 form提交适用 * @param encoding 请求实体 json/xml提交适用 * @return 可能为空 需要处理 * @throws Exception * */ public static String post(String url, String params, String encoding) throws Exception { String result = ""; CloseableHttpClient httpClient = null; try { httpClient = getHttpClient(); HttpPost httpPost = new HttpPost(url); // 设置头信息 httpPost.addHeader("Content-Type", "application/json;charset=" + encoding); // 消息实体 httpPost.setEntity(new StringEntity(params, encoding)); //发起请求 HttpResponse httpResponse = httpClient.execute(httpPost); //解析返回消息实体 HttpEntity resEntity = httpResponse.getEntity(); if (resEntity != null) { result= EntityUtils.toString(resEntity); log.info("result==="+result); } } catch (Exception e) { throw e; } finally { if (httpClient != null) { httpClient.close(); } } return result; } public static CloseableHttpClient getHttpClient() throws Exception { CloseableHttpClient httpClient = HttpClients.custom() .setSSLSocketFactory(sslsf) .setConnectionManager(cm) .setConnectionManagerShared(true) .build(); return httpClient; } }