Java Code Examples for org.apache.commons.httpclient.params.HttpConnectionManagerParams#setMaxTotalConnections()

The following examples show how to use org.apache.commons.httpclient.params.HttpConnectionManagerParams#setMaxTotalConnections() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpClientFactory.java    From alfresco-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected HttpClient constructHttpClient()
{
    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    HttpClient httpClient = new HttpClient(connectionManager);
    HttpClientParams params = httpClient.getParams();
    params.setBooleanParameter(HttpConnectionParams.TCP_NODELAY, true);
    params.setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, true);
    if (socketTimeout != null) 
    {
        params.setSoTimeout(socketTimeout);
    }
    HttpConnectionManagerParams connectionManagerParams = httpClient.getHttpConnectionManager().getParams();
    connectionManagerParams.setMaxTotalConnections(maxTotalConnections);
    connectionManagerParams.setDefaultMaxConnectionsPerHost(maxHostConnections);
    connectionManagerParams.setConnectionTimeout(connectionTimeout);

    return httpClient;
}
 
Example 2
Source File: DefaultDiamondSubscriber.java    From diamond with Apache License 2.0 6 votes vote down vote up
protected void initHttpClient() {
    if (MockServer.isTestMode()) {
        return;
    }
    HostConfiguration hostConfiguration = new HostConfiguration();
    hostConfiguration.setHost(diamondConfigure.getDomainNameList().get(this.domainNamePos.get()),
        diamondConfigure.getPort());

    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.closeIdleConnections(diamondConfigure.getPollingIntervalTime() * 4000);

    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setStaleCheckingEnabled(diamondConfigure.isConnectionStaleCheckingEnabled());
    params.setMaxConnectionsPerHost(hostConfiguration, diamondConfigure.getMaxHostConnections());
    params.setMaxTotalConnections(diamondConfigure.getMaxTotalConnections());
    params.setConnectionTimeout(diamondConfigure.getConnectionTimeout());
    // 设置读超时为1分钟,
    // [email protected]
    params.setSoTimeout(60 * 1000);

    connectionManager.setParams(params);
    httpClient = new HttpClient(connectionManager);
    httpClient.setHostConfiguration(hostConfiguration);
}
 
Example 3
Source File: DefaultDiamondSubscriber.java    From diamond with Apache License 2.0 6 votes vote down vote up
protected void initHttpClient() {
    if (MockServer.isTestMode()) {
        return;
    }
    HostConfiguration hostConfiguration = new HostConfiguration();
    hostConfiguration.setHost(diamondConfigure.getDomainNameList().get(this.domainNamePos.get()),
        diamondConfigure.getPort());

    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.closeIdleConnections(diamondConfigure.getPollingIntervalTime() * 4000);

    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setStaleCheckingEnabled(diamondConfigure.isConnectionStaleCheckingEnabled());
    params.setMaxConnectionsPerHost(hostConfiguration, diamondConfigure.getMaxHostConnections());
    params.setMaxTotalConnections(diamondConfigure.getMaxTotalConnections());
    params.setConnectionTimeout(diamondConfigure.getConnectionTimeout());
    // 设置读超时为1分钟,
    // [email protected]
    params.setSoTimeout(60 * 1000);

    connectionManager.setParams(params);
    httpClient = new HttpClient(connectionManager);
    httpClient.setHostConfiguration(hostConfiguration);
}
 
Example 4
Source File: CoreServerInitializer.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
private ConfigurationContext getClientConfigurationContext() throws AxisFault {
    String clientRepositoryLocation = serverConfigurationService.getFirstProperty(CLIENT_REPOSITORY_LOCATION);
    String clientAxis2XmlLocationn = serverConfigurationService.getFirstProperty(CLIENT_AXIS2_XML_LOCATION);
    ConfigurationContext clientConfigContextToReturn = ConfigurationContextFactory
            .createConfigurationContextFromFileSystem(clientRepositoryLocation, clientAxis2XmlLocationn);
    MultiThreadedHttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();

    // Set the default max connections per host
    int defaultMaxConnPerHost = 500;
    Parameter defaultMaxConnPerHostParam = clientConfigContextToReturn.getAxisConfiguration()
            .getParameter("defaultMaxConnPerHost");
    if (defaultMaxConnPerHostParam != null) {
        defaultMaxConnPerHost = Integer.parseInt((String) defaultMaxConnPerHostParam.getValue());
    }
    params.setDefaultMaxConnectionsPerHost(defaultMaxConnPerHost);

    // Set the max total connections
    int maxTotalConnections = 15000;
    Parameter maxTotalConnectionsParam = clientConfigContextToReturn.getAxisConfiguration()
            .getParameter("maxTotalConnections");
    if (maxTotalConnectionsParam != null) {
        maxTotalConnections = Integer.parseInt((String) maxTotalConnectionsParam.getValue());
    }
    params.setMaxTotalConnections(maxTotalConnections);

    params.setSoTimeout(600000);
    params.setConnectionTimeout(600000);

    httpConnectionManager.setParams(params);
    clientConfigContextToReturn
            .setProperty(HTTPConstants.MULTITHREAD_HTTP_CONNECTION_MANAGER, httpConnectionManager);

    clientConfigContextToReturn.setProperty(MicroIntegratorBaseConstants.WORK_DIR, serverWorkDir);
    return clientConfigContextToReturn;
}
 
Example 5
Source File: CFActivator.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
private HttpClient createHttpClient() {
	//see http://hc.apache.org/httpclient-3.x/threading.html
	MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
	HttpConnectionManagerParams params = connectionManager.getParams();
	params.setMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION, PreferenceHelper.getInt(ServerConstants.HTTP_MAX_CONN_HOST_CONF_KEY, 50));
	params.setMaxTotalConnections(PreferenceHelper.getInt(ServerConstants.HTTP_MAX_CONN_TOTAL_CONF_KEY, 150));
	params.setConnectionTimeout(PreferenceHelper.getInt(ServerConstants.HTTP_CONN_TIMEOUT_CONF_KEY, 15000)); //15s
	params.setSoTimeout(PreferenceHelper.getInt(ServerConstants.HTTP_SO_TIMEOUT_CONF_KEY, 30000)); //30s
	connectionManager.setParams(params);

	HttpClientParams clientParams = new HttpClientParams();
	clientParams.setConnectionManagerTimeout(PreferenceHelper.getInt(ServerConstants.HTTP_CONN_MGR_TIMEOUT_CONF_KEY, 300000)); // 5 minutes

	return new HttpClient(clientParams, connectionManager);
}
 
Example 6
Source File: HttpClientBuilder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Builds an HTTP client with the given settings. Settings are NOT reset to their default values after a client has
 * been created.
 * 
 * @return the created client.
 */
public HttpClient buildClient() {
    if (httpsProtocolSocketFactory != null) {
        Protocol.registerProtocol("https", new Protocol("https", httpsProtocolSocketFactory, 443));
    }

    HttpClientParams clientParams = new HttpClientParams();
    clientParams.setAuthenticationPreemptive(isPreemptiveAuthentication());
    clientParams.setContentCharset(getContentCharSet());
    clientParams.setParameter(HttpClientParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(
            connectionRetryAttempts, false));

    HttpConnectionManagerParams connMgrParams = new HttpConnectionManagerParams();
    connMgrParams.setConnectionTimeout(getConnectionTimeout());
    connMgrParams.setDefaultMaxConnectionsPerHost(getMaxConnectionsPerHost());
    connMgrParams.setMaxTotalConnections(getMaxTotalConnections());
    connMgrParams.setReceiveBufferSize(getReceiveBufferSize());
    connMgrParams.setSendBufferSize(getSendBufferSize());
    connMgrParams.setTcpNoDelay(isTcpNoDelay());

    MultiThreadedHttpConnectionManager connMgr = new MultiThreadedHttpConnectionManager();
    connMgr.setParams(connMgrParams);

    HttpClient httpClient = new HttpClient(clientParams, connMgr);

    if (proxyHost != null) {
        HostConfiguration hostConfig = new HostConfiguration();
        hostConfig.setProxy(proxyHost, proxyPort);
        httpClient.setHostConfiguration(hostConfig);

        if (proxyUsername != null) {
            AuthScope proxyAuthScope = new AuthScope(proxyHost, proxyPort);
            UsernamePasswordCredentials proxyCredentials = new UsernamePasswordCredentials(proxyUsername,
                    proxyPassword);
            httpClient.getState().setProxyCredentials(proxyAuthScope, proxyCredentials);
        }
    }

    return httpClient;
}
 
Example 7
Source File: SharedHttpClientProvider.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
public SharedHttpClientProvider(String alfrescoUrl, int maxNumberOfConnections)
{
    setAlfrescoUrl(alfrescoUrl);
    
    // Initialize manager
    MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setMaxTotalConnections(maxNumberOfConnections);
    params.setMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION, maxNumberOfConnections);

    // Create the client
    client = new HttpClient(manager);
    client.getParams().setAuthenticationPreemptive(true);
}
 
Example 8
Source File: OAuthTokenValidationStubFactory.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an instance of MultiThreadedHttpConnectionManager using HttpClient 3.x APIs
 *
 * @param properties Properties to configure MultiThreadedHttpConnectionManager
 * @return An instance of properly configured MultiThreadedHttpConnectionManager
 */
private HttpConnectionManager createConnectionManager(Properties properties) {
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    if (properties == null || properties.isEmpty()) {
        throw new IllegalArgumentException("Parameters required to initialize HttpClient instances " +
                "associated with OAuth token validation service stub are not provided");
    }
    String maxConnectionsPerHostParam = properties.getProperty("MaxConnectionsPerHost");
    if (maxConnectionsPerHostParam == null || maxConnectionsPerHostParam.isEmpty()) {
        if (log.isDebugEnabled()) {
            log.debug("MaxConnectionsPerHost parameter is not explicitly defined. Therefore, the default, " +
                    "which is 2, will be used");
        }
    } else {
        params.setDefaultMaxConnectionsPerHost(Integer.parseInt(maxConnectionsPerHostParam));
    }

    String maxTotalConnectionsParam = properties.getProperty("MaxTotalConnections");
    if (maxTotalConnectionsParam == null || maxTotalConnectionsParam.isEmpty()) {
        if (log.isDebugEnabled()) {
            log.debug("MaxTotalConnections parameter is not explicitly defined. Therefore, the default, " +
                    "which is 10, will be used");
        }
    } else {
        params.setMaxTotalConnections(Integer.parseInt(maxTotalConnectionsParam));
    }
    HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.setParams(params);
    return connectionManager;
}
 
Example 9
Source File: ProxyFilter.java    From development with Apache License 2.0 5 votes vote down vote up
/**
 * Init the parameter settings for the http client
 */
private HttpConnectionManagerParams connectionParams() {
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setDefaultMaxConnectionsPerHost(readMaxHostConnectionSetting());
    params.setMaxTotalConnections(readMaxTotalConnectionSetting());
    return params;

}
 
Example 10
Source File: AutoscalerCloudControllerClient.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
private AutoscalerCloudControllerClient() {
    MultiThreadedHttpConnectionManager multiThreadedHttpConnectionManager = new
            MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setDefaultMaxConnectionsPerHost(AS_CC_CLIENT_MAX_CONNECTIONS_PER_HOST);
    params.setMaxTotalConnections(AS_CC_CLIENT_MAX_TOTAL_CONNECTIONS);
    multiThreadedHttpConnectionManager.setParams(params);
    HttpClient httpClient = new HttpClient(multiThreadedHttpConnectionManager);

    try {
        ConfigurationContext ctx = ConfigurationContextFactory.createConfigurationContextFromFileSystem(null, null);
        ctx.setProperty(HTTPConstants.CACHED_HTTP_CLIENT, httpClient);
        XMLConfiguration conf = ConfUtil.getInstance(null).getConfiguration();
        int port = conf.getInt("autoscaler.cloudController.port", AutoscalerConstants
                .CLOUD_CONTROLLER_DEFAULT_PORT);
        String hostname = conf.getString("autoscaler.cloudController.hostname", "localhost");
        String epr = "https://" + hostname + ":" + port + "/" + AutoscalerConstants.CLOUD_CONTROLLER_SERVICE_SFX;
        int cloudControllerClientTimeout = conf.getInt("autoscaler.cloudController.clientTimeout", 180000);

        stub = new CloudControllerServiceStub(ctx, epr);
        stub._getServiceClient().getOptions().setProperty(HTTPConstants.SO_TIMEOUT, cloudControllerClientTimeout);
        stub._getServiceClient().getOptions().setProperty(HTTPConstants.CONNECTION_TIMEOUT,
                cloudControllerClientTimeout);
        stub._getServiceClient().getOptions().setProperty(HTTPConstants.CHUNKED, Constants.VALUE_FALSE);
        stub._getServiceClient().getOptions().setProperty(Constants.Configuration.DISABLE_SOAP_ACTION, Boolean
                .TRUE);
    } catch (Exception e) {
        log.error("Could not initialize cloud controller client", e);
    }
}
 
Example 11
Source File: CloudControllerServiceClient.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
private CloudControllerServiceClient(String epr) throws AxisFault {
    MultiThreadedHttpConnectionManager multiThreadedHttpConnectionManager = new
            MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setDefaultMaxConnectionsPerHost(StratosConstants.CLOUD_CONTROLLER_CLIENT_MAX_CONNECTIONS_PER_HOST);
    params.setMaxTotalConnections(StratosConstants.CLOUD_CONTROLLER_CLIENT_MAX_TOTAL_CONNECTIONS);
    multiThreadedHttpConnectionManager.setParams(params);
    HttpClient httpClient = new HttpClient(multiThreadedHttpConnectionManager);
    ConfigurationContext ctx = ConfigurationContextFactory.createConfigurationContextFromFileSystem(null, null);
    ctx.setProperty(HTTPConstants.CACHED_HTTP_CLIENT, httpClient);

    String ccSocketTimeout = System.getProperty(StratosConstants.CLOUD_CONTROLLER_CLIENT_SOCKET_TIMEOUT) == null ?
            StratosConstants.DEFAULT_CLIENT_SOCKET_TIMEOUT :
            System.getProperty(StratosConstants.CLOUD_CONTROLLER_CLIENT_SOCKET_TIMEOUT);

    String ccConnectionTimeout =
            System.getProperty(StratosConstants.CLOUD_CONTROLLER_CLIENT_CONNECTION_TIMEOUT) == null ?
                    StratosConstants.DEFAULT_CLIENT_CONNECTION_TIMEOUT :
                    System.getProperty(StratosConstants.CLOUD_CONTROLLER_CLIENT_CONNECTION_TIMEOUT);
    try {
        stub = new CloudControllerServiceStub(ctx, epr);
        stub._getServiceClient().getOptions()
                .setProperty(HTTPConstants.SO_TIMEOUT, Integer.valueOf(ccSocketTimeout));
        stub._getServiceClient().getOptions()
                .setProperty(HTTPConstants.CONNECTION_TIMEOUT, new Integer(ccConnectionTimeout));
        stub._getServiceClient().getOptions().setProperty(HTTPConstants.CHUNKED, Constants.VALUE_FALSE);
        stub._getServiceClient().getOptions().setProperty(Constants.Configuration.DISABLE_SOAP_ACTION, Boolean
                .TRUE);
    } catch (AxisFault axisFault) {
        String msg = "Could not initialize cloud controller service client";
        log.error(msg, axisFault);
        throw new AxisFault(msg, axisFault);
    }
}
 
Example 12
Source File: AutoscalerServiceClient.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
private AutoscalerServiceClient(String epr) throws AxisFault {
    MultiThreadedHttpConnectionManager multiThreadedHttpConnectionManager = new
            MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setDefaultMaxConnectionsPerHost(StratosConstants.AUTOSCALER_CLIENT_MAX_CONNECTIONS_PER_HOST);
    params.setMaxTotalConnections(StratosConstants.AUTOSCALER_CLIENT_MAX_TOTAL_CONNECTIONS);
    multiThreadedHttpConnectionManager.setParams(params);
    HttpClient httpClient = new HttpClient(multiThreadedHttpConnectionManager);
    ConfigurationContext ctx = ConfigurationContextFactory.createConfigurationContextFromFileSystem(null, null);
    ctx.setProperty(HTTPConstants.CACHED_HTTP_CLIENT, httpClient);

    String autosclaerSocketTimeout = System.getProperty(StratosConstants.AUTOSCALER_CLIENT_SOCKET_TIMEOUT) == null ?
            StratosConstants.DEFAULT_CLIENT_SOCKET_TIMEOUT :
            System.getProperty(StratosConstants.AUTOSCALER_CLIENT_SOCKET_TIMEOUT);

    String autosclaerConnectionTimeout = System.getProperty(StratosConstants.AUTOSCALER_CLIENT_CONNECTION_TIMEOUT)
            == null ? StratosConstants.DEFAULT_CLIENT_CONNECTION_TIMEOUT :
            System.getProperty(StratosConstants.AUTOSCALER_CLIENT_CONNECTION_TIMEOUT);
    try {
        stub = new AutoscalerServiceStub(ctx, epr);
        stub._getServiceClient().getOptions().setProperty(HTTPConstants.SO_TIMEOUT,
                Integer.valueOf(autosclaerSocketTimeout));
        stub._getServiceClient().getOptions().setProperty(HTTPConstants.CONNECTION_TIMEOUT,
                Integer.valueOf(autosclaerConnectionTimeout));
        stub._getServiceClient().getOptions().setProperty(HTTPConstants.CHUNKED, Constants.VALUE_FALSE);
        stub._getServiceClient().getOptions().setProperty(Constants.Configuration.DISABLE_SOAP_ACTION, Boolean
                .TRUE);
    } catch (AxisFault axisFault) {
        String msg = "Could not initialize autoscaler service client";
        log.error(msg, axisFault);
        throw new AxisFault(msg, axisFault);
    }
}
 
Example 13
Source File: StratosManagerServiceClient.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
private StratosManagerServiceClient(String epr) throws AxisFault {
    MultiThreadedHttpConnectionManager multiThreadedHttpConnectionManager = new
            MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setDefaultMaxConnectionsPerHost(StratosConstants.STRATOS_MANAGER_CLIENT_MAX_CONNECTIONS_PER_HOST);
    params.setMaxTotalConnections(StratosConstants.STRATOS_MANAGER_CLIENT_MAX_TOTAL_CONNECTIONS);
    multiThreadedHttpConnectionManager.setParams(params);
    HttpClient httpClient = new HttpClient(multiThreadedHttpConnectionManager);
    ConfigurationContext ctx = ConfigurationContextFactory.createConfigurationContextFromFileSystem(null, null);
    ctx.setProperty(HTTPConstants.CACHED_HTTP_CLIENT, httpClient);

    String ccSocketTimeout = System.getProperty(StratosConstants.STRATOS_MANAGER_CLIENT_SOCKET_TIMEOUT) == null ?
            StratosConstants.DEFAULT_CLIENT_SOCKET_TIMEOUT :
            System.getProperty(StratosConstants.STRATOS_MANAGER_CLIENT_SOCKET_TIMEOUT);

    String ccConnectionTimeout = System.getProperty(StratosConstants.STRATOS_MANAGER_CLIENT_CONNECTION_TIMEOUT)
            == null ?
            StratosConstants.DEFAULT_CLIENT_CONNECTION_TIMEOUT :
            System.getProperty(StratosConstants.STRATOS_MANAGER_CLIENT_CONNECTION_TIMEOUT);
    try {
        stub = new StratosManagerServiceStub(ctx, epr);
        stub._getServiceClient().getOptions().setProperty(HTTPConstants.SO_TIMEOUT, Integer.valueOf
                (ccSocketTimeout));
        stub._getServiceClient().getOptions().setProperty(HTTPConstants.CONNECTION_TIMEOUT, Integer.valueOf
                (ccConnectionTimeout));
        stub._getServiceClient().getOptions().setProperty(HTTPConstants.CHUNKED, Constants.VALUE_FALSE);
        stub._getServiceClient().getOptions().setProperty(Constants.Configuration.DISABLE_SOAP_ACTION, Boolean
                .TRUE);
    } catch (AxisFault axisFault) {
        String msg = "Could not initialize stratos manager service client";
        log.error(msg, axisFault);
        throw new AxisFault(msg, axisFault);
    }
}
 
Example 14
Source File: Http.java    From anthelion with Apache License 2.0 4 votes vote down vote up
/**
 * Configures the HTTP client
 */
private void configureClient() {

  // Set up an HTTPS socket factory that accepts self-signed certs.
  Protocol https = new Protocol("https",
      new DummySSLProtocolSocketFactory(), 443);
  Protocol.registerProtocol("https", https);

  HttpConnectionManagerParams params = connectionManager.getParams();
  params.setConnectionTimeout(timeout);
  params.setSoTimeout(timeout);
  params.setSendBufferSize(BUFFER_SIZE);
  params.setReceiveBufferSize(BUFFER_SIZE);
  params.setMaxTotalConnections(maxThreadsTotal);

  // executeMethod(HttpMethod) seems to ignore the connection timeout on the connection manager.
  // set it explicitly on the HttpClient.
  client.getParams().setConnectionManagerTimeout(timeout);

  HostConfiguration hostConf = client.getHostConfiguration();
  ArrayList headers = new ArrayList();
  // Set the User Agent in the header
  headers.add(new Header("User-Agent", userAgent));
  // prefer English
  headers.add(new Header("Accept-Language", acceptLanguage));
  // prefer UTF-8
  headers.add(new Header("Accept-Charset", "utf-8,ISO-8859-1;q=0.7,*;q=0.7"));
  // prefer understandable formats
  headers.add(new Header("Accept",
          "text/html,application/xml;q=0.9,application/xhtml+xml,text/xml;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"));
  // accept gzipped content
  headers.add(new Header("Accept-Encoding", "x-gzip, gzip, deflate"));
  hostConf.getParams().setParameter("http.default-headers", headers);

  // HTTP proxy server details
  if (useProxy) {
    hostConf.setProxy(proxyHost, proxyPort);

    if (proxyUsername.length() > 0) {

      AuthScope proxyAuthScope = getAuthScope(
          this.proxyHost, this.proxyPort, this.proxyRealm);

      NTCredentials proxyCredentials = new NTCredentials(
          this.proxyUsername, this.proxyPassword,
          this.agentHost, this.proxyRealm);

      client.getState().setProxyCredentials(
          proxyAuthScope, proxyCredentials);
    }
  }

}
 
Example 15
Source File: Http.java    From nutch-htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Configures the HTTP client
 */
private void configureClient() {

  // Set up an HTTPS socket factory that accepts self-signed certs.
  ProtocolSocketFactory factory = new SSLProtocolSocketFactory();
  Protocol https = new Protocol("https", factory, 443);
  Protocol.registerProtocol("https", https);

  HttpConnectionManagerParams params = connectionManager.getParams();
  params.setConnectionTimeout(timeout);
  params.setSoTimeout(timeout);
  params.setSendBufferSize(BUFFER_SIZE);
  params.setReceiveBufferSize(BUFFER_SIZE);
  params.setMaxTotalConnections(maxThreadsTotal);

  // executeMethod(HttpMethod) seems to ignore the connection timeout on the connection manager.
  // set it explicitly on the HttpClient.
  client.getParams().setConnectionManagerTimeout(timeout);

  HostConfiguration hostConf = client.getHostConfiguration();
  ArrayList<Header> headers = new ArrayList<Header>();
  // Set the User Agent in the header
  headers.add(new Header("User-Agent", userAgent));
  // prefer English
  headers.add(new Header("Accept-Language", acceptLanguage));
  // prefer UTF-8
  headers.add(new Header("Accept-Charset", "utf-8,ISO-8859-1;q=0.7,*;q=0.7"));
  // prefer understandable formats
  headers.add(new Header("Accept",
          "text/html,application/xml;q=0.9,application/xhtml+xml,text/xml;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"));
  // accept gzipped content
  headers.add(new Header("Accept-Encoding", "x-gzip, gzip, deflate"));
  hostConf.getParams().setParameter("http.default-headers", headers);

  // HTTP proxy server details
  if (useProxy) {
    hostConf.setProxy(proxyHost, proxyPort);

    if (proxyUsername.length() > 0) {

      AuthScope proxyAuthScope = getAuthScope(
          this.proxyHost, this.proxyPort, this.proxyRealm);

      NTCredentials proxyCredentials = new NTCredentials(
          this.proxyUsername, this.proxyPassword,
          Http.agentHost, this.proxyRealm);

      client.getState().setProxyCredentials(
          proxyAuthScope, proxyCredentials);
    }
  }

}