zoukankan      html  css  js  c++  java
  • 通过绕过验证的方式,请求rpc

    package com.lassenInterface.common.httpRpcPost;

    import java.security.KeyManagementException;
    import java.security.KeyStoreException;
    import java.security.NoSuchAlgorithmException;
    import java.security.cert.CertificateException;
    import java.security.cert.X509Certificate;

    import javax.net.ssl.HostnameVerifier;
    import javax.net.ssl.SSLContext;

    import org.apache.http.config.Registry;
    import org.apache.http.config.RegistryBuilder;
    import org.apache.http.conn.socket.ConnectionSocketFactory;
    import org.apache.http.conn.socket.PlainConnectionSocketFactory;
    import org.apache.http.conn.ssl.NoopHostnameVerifier;
    import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
    import org.apache.http.ssl.SSLContextBuilder;
    import org.apache.http.ssl.TrustStrategy;

    public class SSLUtils {


    /**
    * 绕过验证
    *
    * @return
    * @throws NoSuchAlgorithmException
    * @throws KeyManagementException
    * @throws KeyStoreException
    */
    public static CloseableHttpClient createIgnoreVerifySSL() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
    //创建httpClient对象
    HttpClientBuilder b = HttpClientBuilder.create();
    //通过X509绕过验证的方式处理https请求
    SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
    public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
    return true;
    }
    }).build();
    //httpClient的SSL环境搭建
    b.setSSLContext(sslContext);
    //验证主机名与服务器匹配
    HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
    //创建SSLConnectionSocketFactory
    SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
    //设置协议http和https对应的处理socket链接工厂的对象,通过Registry将SSLConnectionSocketFactory注册到我们的HttpClient上
    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
    .register("http", PlainConnectionSocketFactory.getSocketFactory())
    .register("https", sslSocketFactory)
    .build();
    //http请求连接池
    PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
    //将最大连接数增加到200
    connMgr.setMaxTotal(200);
    //将每个路由基础的连接增加到100
    connMgr.setDefaultMaxPerRoute(100);
    //httpClient的连接池搭建
    b.setConnectionManager(connMgr);

    CloseableHttpClient client = b.build();
    return client;
    }
    }

  • 相关阅读:
    git下载指定的版本
    QT中定时器
    makefile 中添加依赖的库文件
    Qt 出现“undefined reference to `vtable for”
    qt程序启动播放动画
    常用正则表达式
    当你纠结时,请打开这31个锦…
    android mk详解
    C++日志系统log4cxx使用总结
    qt 坐标变换
  • 原文地址:https://www.cnblogs.com/TestMa/p/9506400.html
Copyright © 2011-2022 走看看