zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    C#调用JS
    C#对象序列化(2)
    C#委托和事件(2)
    C#委托和事件(1)
    Windows Mobile Ping 命令实现
    操作 SQL Server Mobile 2005 数据库的常用 C# 代码
    Pocket PC 2003数据库操作
    C#委托和事件(3)
    C#中RSA加密解密和签名与验证的实现
    使用SqlBulkCopy数据导入和复制
  • 原文地址:https://www.cnblogs.com/yang1314/p/10515368.html
Copyright © 2011-2022 走看看