/**
  * Sets the timeouts of the HTTP client.
  *
  * @param connectionTimeout timeout until connection established in milliseconds. Zero means no
  *     timeout.
  * @param socketTimeout timeout for waiting for data in milliseconds. Zero means no timeout.
  * @param maxRequests maximum number of connections to a particuar host
  */
 public static void setParams(int connectionTimeout, int socketTimeout, int maxRequests) {
   PrudentHttpEntityResolver.maxRequests = maxRequests;
   PoolingHttpClientConnectionManager phcConnMgr;
   Registry<ConnectionSocketFactory> registry = //
       RegistryBuilder.<ConnectionSocketFactory>create() //
           .register("http", PlainConnectionSocketFactory.getSocketFactory()) //
           .register("https", SSLConnectionSocketFactory.getSocketFactory()) //
           .build();
   HttpClientBuilder builder = HttpClients.custom();
   builder.setRedirectStrategy(new LaxRedirectStrategy());
   builder.setMaxConnPerRoute(maxRequests);
   builder.setMaxConnTotal(200);
   if ("true".equals(System.getProperty("nu.validator.xml.promiscuous-ssl", "false"))) { //
     try {
       SSLContext promiscuousSSLContext =
           new SSLContextBuilder() //
               .loadTrustMaterial(
                   null,
                   new TrustStrategy() {
                     @Override
                     public boolean isTrusted(X509Certificate[] arg0, String arg1)
                         throws CertificateException {
                       return true;
                     }
                   })
               .build();
       builder.setSslcontext(promiscuousSSLContext);
       HostnameVerifier verifier = //
           SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
       SSLConnectionSocketFactory promiscuousSSLConnSocketFactory = //
           new SSLConnectionSocketFactory(promiscuousSSLContext, verifier);
       registry =
           RegistryBuilder.<ConnectionSocketFactory>create() //
               .register("https", promiscuousSSLConnSocketFactory) //
               .register("http", PlainConnectionSocketFactory.getSocketFactory()) //
               .build();
     } catch (KeyManagementException
         | KeyStoreException
         | NoSuchAlgorithmException
         | NumberFormatException e) {
       e.printStackTrace();
     }
   }
   phcConnMgr = new PoolingHttpClientConnectionManager(registry);
   phcConnMgr.setDefaultMaxPerRoute(maxRequests);
   phcConnMgr.setMaxTotal(200);
   builder.setConnectionManager(phcConnMgr);
   RequestConfig.Builder config = RequestConfig.custom();
   config.setCircularRedirectsAllowed(true);
   config.setMaxRedirects(20); // Gecko default
   config.setConnectTimeout(connectionTimeout);
   config.setCookieSpec(CookieSpecs.BEST_MATCH);
   config.setSocketTimeout(socketTimeout);
   client = builder.setDefaultRequestConfig(config.build()).build();
 }
Пример #2
0
  public Session(
      URL url, String login, String password, boolean trustSelfSigned, int maxConnections) {

    _executorService = Executors.newFixedThreadPool(maxConnections);

    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();

    credentialsProvider.setCredentials(
        new AuthScope(url.getHost(), url.getPort()),
        new UsernamePasswordCredentials(login, password));

    httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);

    httpClientBuilder.setMaxConnPerRoute(maxConnections);
    httpClientBuilder.setMaxConnTotal(maxConnections);
    httpClientBuilder.setRoutePlanner(_getHttpRoutePlanner());

    if (trustSelfSigned) {
      try {
        SSLContextBuilder sslContextBuilder = new SSLContextBuilder();

        sslContextBuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());

        SSLConnectionSocketFactory sslConnectionSocketFactory =
            new SSLConnectionSocketFactory(
                sslContextBuilder.build(), SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        httpClientBuilder.setSSLSocketFactory(sslConnectionSocketFactory);
      } catch (Exception e) {
        _logger.error(e.getMessage(), e);
      }
    }

    _httpClient = httpClientBuilder.build();

    _httpHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
  }