/**
  * Create SSL context and initialize it using specific trust manager.
  *
  * @param trustManager trust manager
  * @return initialized SSL context
  * @throws GeneralSecurityException on security error
  */
 public static SSLContext createSslContext(TrustManager trustManager)
     throws GeneralSecurityException {
   EwsX509TrustManager x509TrustManager = new EwsX509TrustManager(null, trustManager);
   SSLContext sslContext = SSLContexts.createDefault();
   sslContext.init(null, new TrustManager[] {x509TrustManager}, null);
   return sslContext;
 }
  public EasyHttpClient() {

    CookieSpecProvider easySpecProvider =
        new CookieSpecProvider() {
          public CookieSpec create(HttpContext context) {

            return new BrowserCompatSpec() {
              public void validate(Cookie cookie, CookieOrigin origin)
                  throws MalformedCookieException {}
            };
          }
        };

    SSLContext sslContext = SSLContexts.createDefault(); // 忽略证书主机名验证
    SSLConnectionSocketFactory sslsf =
        new SSLConnectionSocketFactory(
            sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

    Registry<ConnectionSocketFactory> reg =
        RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.INSTANCE)
            .register("https", sslsf)
            .build();
    connectionManager = new PoolingHttpClientConnectionManager(reg);
    connectionManager.setDefaultMaxPerRoute(100); // 同一个路由允许最大连接数
    // connectionManager.setMaxTotal(poolSize);
    HttpHost localhost = new HttpHost("locahost", 80); // 如果是多网卡,这里选择出口IP?
    connectionManager.setMaxPerRoute(new HttpRoute(localhost), 50);

    Registry<CookieSpecProvider> r =
        RegistryBuilder.<CookieSpecProvider>create()
            .register(CookieSpecs.BEST_MATCH, new BestMatchSpecFactory())
            .register(CookieSpecs.BROWSER_COMPATIBILITY, new BrowserCompatSpecFactory())
            .register("easy", easySpecProvider)
            .build();

    RequestConfig requestConfig =
        RequestConfig.custom()
            .setCookieSpec("easy")
            .setSocketTimeout(10000)
            .setConnectTimeout(10000)
            .build();
    ConnectionConfig connectioncfg = ConnectionConfig.custom().setCharset(Charsets.UTF_8).build();
    SocketConfig socketConfig =
        SocketConfig.custom().setSoKeepAlive(true).setTcpNoDelay(true).build();

    HttpRequestRetryHandler myRetryHandler =
        new HttpRequestRetryHandler() {
          public boolean retryRequest(
              IOException exception, int executionCount, HttpContext context) {
            if (executionCount >= 3) {
              return false;
            }
            if (exception instanceof SSLHandshakeException) {
              return false;
            } else {
              return true;
            }
          }
        };

    HttpClientBuilder builder =
        HttpClients.custom() // 默认
            .setConnectionManager(connectionManager) // 链接管理器
            .setDefaultSocketConfig(socketConfig) // socket管理器
            .setRetryHandler(myRetryHandler) // 重试3次
            .setDefaultConnectionConfig(connectioncfg) // 链接配置,如默认字符编码
            .setDefaultCookieSpecRegistry(r) // cookie策略
            .setUserAgent(Chorme_User_Agent) // 浏览器请求头
            .setDefaultRequestConfig(requestConfig) // 链接配置,超时等
            .setDefaultCookieStore(cookieStore); // cookie

    builder.addInterceptorFirst(
        new HttpRequestInterceptor() {

          public void process(final HttpRequest request, final HttpContext context)
              throws HttpException, IOException {
            if (!request.containsHeader("Accept-Encoding")) {
              request.addHeader("Accept-Encoding", "gzip");
            }
          }
        });

    httpclient = builder.build();
  }