Java Code Examples for org.apache.http.params.BasicHttpParams#setParameter()

The following examples show how to use org.apache.http.params.BasicHttpParams#setParameter() . 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: GoogleAnalytics.java    From h2o-2 with Apache License 2.0 6 votes vote down vote up
protected HttpClient createHttpClient(GoogleAnalyticsConfig config) {
  ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager();
  connManager.setDefaultMaxPerRoute(getDefaultMaxPerRoute(config));

  BasicHttpParams params = new BasicHttpParams();

  if (isNotEmpty(config.getUserAgent())) {
    params.setParameter(CoreProtocolPNames.USER_AGENT, config.getUserAgent());
  }

  if (isNotEmpty(config.getProxyHost())) {
    params.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(config.getProxyHost(), config.getProxyPort()));
  }

  DefaultHttpClient client = new DefaultHttpClient(connManager, params);

  if (isNotEmpty(config.getProxyUserName())) {
    BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(config.getProxyHost(), config.getProxyPort()),
            new UsernamePasswordCredentials(config.getProxyUserName(), config.getProxyPassword()));
    client.setCredentialsProvider(credentialsProvider);
  }

  return client;
}
 
Example 2
Source File: HttpComponentsHelper.java    From openhab1-addons with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * prepare for the https connection
 * call this in the constructor of the class that does the connection if
 * it's used multiple times
 */
private void setup() {
    SchemeRegistry schemeRegistry = new SchemeRegistry();

    // http scheme
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    // https scheme
    schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));

    params = new BasicHttpParams();
    params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 1);
    params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(1));
    params.setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "utf8");

    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    // set the user credentials for our site "example.com"
    credentialsProvider.setCredentials(new AuthScope("example.com", AuthScope.ANY_PORT),
            new UsernamePasswordCredentials("UserNameHere", "UserPasswordHere"));
    clientConnectionManager = new ThreadSafeClientConnManager(params, schemeRegistry);

    context = new BasicHttpContext();
    context.setAttribute("http.auth.credentials-provider", credentialsProvider);
}
 
Example 3
Source File: MwsConnection.java    From amazon-mws-orders with Apache License 2.0 4 votes vote down vote up
/**
 * Initialize the connection.
 */
synchronized void freeze() {
    if (frozen) {
        return;
    }
    if (userAgent == null) {
        setDefaultUserAgent();
    }
    serviceMap = new HashMap<String, ServiceEndpoint>();
    if (log.isDebugEnabled()) {
        StringBuilder buf = new StringBuilder();
        buf.append("Creating MwsConnection {");
        buf.append("applicationName:");
        buf.append(applicationName);
        buf.append(",applicationVersion:");
        buf.append(applicationVersion);
        buf.append(",awsAccessKeyId:");
        buf.append(awsAccessKeyId);
        buf.append(",uri:");
        buf.append(endpoint.toString());
        buf.append(",userAgent:");
        buf.append(userAgent);
        buf.append(",connectionTimeout:");
        buf.append(connectionTimeout);
        if (proxyHost != null && proxyPort != 0) {
            buf.append(",proxyUsername:");
            buf.append(proxyUsername);
            buf.append(",proxyHost:");
            buf.append(proxyHost);
            buf.append(",proxyPort:");
            buf.append(proxyPort);
        }
        buf.append("}");
        log.debug(buf);
    }

    BasicHttpParams httpParams = new BasicHttpParams();
    httpParams.setParameter(CoreProtocolPNames.USER_AGENT, userAgent);
    HttpConnectionParams
            .setConnectionTimeout(httpParams, connectionTimeout);
    HttpConnectionParams.setSoTimeout(httpParams, socketTimeout);
    HttpConnectionParams.setStaleCheckingEnabled(httpParams, true);
    HttpConnectionParams.setTcpNoDelay(httpParams, true);

    connectionManager = getConnectionManager();
    httpClient = new DefaultHttpClient(connectionManager, httpParams);
    httpContext = new BasicHttpContext();
    if (proxyHost != null && proxyPort != 0) {
        String scheme = MwsUtl.usesHttps(endpoint) ? "https" : "http";
        HttpHost hostConfiguration = new HttpHost(proxyHost, proxyPort, scheme);
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, hostConfiguration);

        if (proxyUsername != null && proxyPassword != null) {
            Credentials credentials = new UsernamePasswordCredentials(proxyUsername, proxyPassword);
            CredentialsProvider cprovider = new BasicCredentialsProvider();
            cprovider.setCredentials(new AuthScope(proxyHost, proxyPort), credentials);
            httpContext.setAttribute(ClientContext.CREDS_PROVIDER, cprovider);
        }
    }
    headers = Collections.unmodifiableMap(headers);
    frozen = true;
}