org.apache.http.impl.client.HttpClientBuilder#setProxy()源码实例Demo

下面列出了org.apache.http.impl.client.HttpClientBuilder#setProxy() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: markdown-image-kit   文件: DefaultCosHttpClient.java
private void initHttpClient() {
    this.connectionManager.setMaxTotal(this.clientConfig.getMaxConnectionsCount());
    this.connectionManager.setDefaultMaxPerRoute(this.clientConfig.getMaxConnectionsCount());
    this.connectionManager.setValidateAfterInactivity(1);
    HttpClientBuilder httpClientBuilder =
            HttpClients.custom().setConnectionManager(connectionManager);
    if (this.clientConfig.getHttpProxyIp() != null
            && this.clientConfig.getHttpProxyPort() != 0) {
        HttpHost proxy = new HttpHost(this.clientConfig.getHttpProxyIp(),
                this.clientConfig.getHttpProxyPort());
        httpClientBuilder.setProxy(proxy);
    }
    this.httpClient = httpClientBuilder.build();
    this.requestConfig =
        RequestConfig.custom()
            .setContentCompressionEnabled(false)
            .setConnectionRequestTimeout(
                this.clientConfig.getConnectionRequestTimeout())
            .setConnectTimeout(this.clientConfig.getConnectionTimeout())
            .setSocketTimeout(this.clientConfig.getSocketTimeout()).build();
    this.idleConnectionMonitor = new IdleConnectionMonitorThread(this.connectionManager);
    this.idleConnectionMonitor.setDaemon(true);
    this.idleConnectionMonitor.start();
}
 
源代码2 项目: slack-api   文件: RestUtils.java
public static CloseableHttpClient createHttpClient(int timeout, ProxyServerInfo proxyServerInfo) {
	RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout).build();
	PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
	HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig);
	if (proxyServerInfo != null) {
	    HttpHost proxy = new HttpHost(proxyServerInfo.getHost(), proxyServerInfo.getPort(), proxyServerInfo.getProtocol());
		httpClientBuilder = httpClientBuilder.setProxy(proxy);
		if (proxyServerInfo.getPrincipal() != null && proxyServerInfo.getPassword() != null) {
			Credentials credentials = new UsernamePasswordCredentials(proxyServerInfo.getPrincipal(), proxyServerInfo.getPassword());
			AuthScope authScope = new AuthScope(proxyServerInfo.getHost(), proxyServerInfo.getPort());
			CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
			credentialsProvider.setCredentials(authScope, credentials);
			httpClientBuilder = httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
		}
	}
	return httpClientBuilder.build();
}
 
源代码3 项目: cos-java-sdk-v5   文件: DefaultCosHttpClient.java
private void initHttpClient() {
    this.connectionManager.setMaxTotal(this.clientConfig.getMaxConnectionsCount());
    this.connectionManager.setDefaultMaxPerRoute(this.clientConfig.getMaxConnectionsCount());
    this.connectionManager.setValidateAfterInactivity(1);
    HttpClientBuilder httpClientBuilder =
            HttpClients.custom().setConnectionManager(connectionManager);
    if (this.clientConfig.getHttpProxyIp() != null
            && this.clientConfig.getHttpProxyPort() != 0) {
        HttpHost proxy = new HttpHost(this.clientConfig.getHttpProxyIp(),
                this.clientConfig.getHttpProxyPort());
        httpClientBuilder.setProxy(proxy);
    }
    this.httpClient = httpClientBuilder.build();
    this.requestConfig =
            RequestConfig.custom()
                    .setContentCompressionEnabled(false)
                    .setConnectionRequestTimeout(
                            this.clientConfig.getConnectionRequestTimeout())
            .setConnectTimeout(this.clientConfig.getConnectionTimeout())
            .setSocketTimeout(this.clientConfig.getSocketTimeout()).build();
    this.idleConnectionMonitor = new IdleConnectionMonitorThread(this.connectionManager);
    this.idleConnectionMonitor.setDaemon(true);
    this.idleConnectionMonitor.start();
}
 
源代码4 项目: Asqatasun   文件: HttpRequestHandler.java
private CloseableHttpClient getHttpClient(String url) {
     RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(socketTimeout)
.setConnectTimeout(connectionTimeout)
.build();
     HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
     httpClientBuilder.setDefaultRequestConfig(requestConfig);
     httpClientBuilder.setConnectionManager(new PoolingHttpClientConnectionManager());
     httpClientBuilder.setUserAgent(ASQATASUN_USER_AGENT);
     if (isProxySet(url)) {
         LOGGER.debug(("Set proxy with " + proxyHost + " and " + proxyPort));
         httpClientBuilder.setProxy(new HttpHost(proxyHost, Integer.valueOf(proxyPort)));
         if (isProxyCredentialSet()) {
             CredentialsProvider credsProvider = new BasicCredentialsProvider();
             credsProvider.setCredentials(
                     new AuthScope(proxyHost, Integer.valueOf(proxyPort)),
                     new UsernamePasswordCredentials(proxyUser, proxyPassword));
             httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
             LOGGER.debug(("Set proxy credentials " + proxyHost + " and " + proxyPort + " and " + proxyUser + " and " + proxyPassword));
         }
     }
     return httpClientBuilder.build();
 }
 
源代码5 项目: cos-java-sdk-v4   文件: AbstractCosHttpClient.java
public AbstractCosHttpClient(ClientConfig config) {
    super();
    this.config = config;
    this.connectionManager = new PoolingHttpClientConnectionManager();
    this.connectionManager.setMaxTotal(config.getMaxConnectionsCount());
    this.connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsCount());
    this.connectionManager.setValidateAfterInactivity(1);
    HttpClientBuilder httpClientBuilder =
            HttpClients.custom().setConnectionManager(connectionManager);
    if (config.getHttpProxyIp() != null && config.getHttpProxyPort() != 0) {
        HttpHost proxy = new HttpHost(config.getHttpProxyIp(), config.getHttpProxyPort());
        httpClientBuilder.setProxy(proxy);
    }
    this.httpClient = httpClientBuilder.build();
    this.requestConfig = RequestConfig.custom()
            .setConnectionRequestTimeout(this.config.getConnectionRequestTimeout())
            .setConnectTimeout(this.config.getConnectionTimeout())
            .setSocketTimeout(this.config.getSocketTimeout()).build();
    this.idleConnectionMonitor = new IdleConnectionMonitorThread(this.connectionManager);
    this.idleConnectionMonitor.setDaemon(true);
    this.idleConnectionMonitor.start();
}
 
源代码6 项目: cf-java-client-sap   文件: RestUtil.java
public ClientHttpRequestFactory createRequestFactory(HttpProxyConfiguration httpProxyConfiguration, boolean trustSelfSignedCerts) {
    HttpClientBuilder httpClientBuilder = HttpClients.custom()
                                                     .useSystemProperties();

    if (trustSelfSignedCerts) {
        httpClientBuilder.setSslcontext(buildSslContext());
        httpClientBuilder.setHostnameVerifier(BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    }

    if (httpProxyConfiguration != null) {
        HttpHost proxy = new HttpHost(httpProxyConfiguration.getProxyHost(), httpProxyConfiguration.getProxyPort());
        httpClientBuilder.setProxy(proxy);

        if (httpProxyConfiguration.isAuthRequired()) {
            BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(new AuthScope(httpProxyConfiguration.getProxyHost(),
                                                             httpProxyConfiguration.getProxyPort()),
                                               new UsernamePasswordCredentials(httpProxyConfiguration.getUsername(),
                                                                               httpProxyConfiguration.getPassword()));
            httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
        }

        HttpRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
        httpClientBuilder.setRoutePlanner(routePlanner);
    }

    HttpClient httpClient = httpClientBuilder.build();

    return new HttpComponentsClientHttpRequestFactory(httpClient);
}
 
源代码7 项目: rug-cli   文件: HttpClientFactory.java
private static void configureProxy(HttpClientBuilder builder, String url) {
    List<Proxy> proxies = ProxySelector.getDefault().select(URI.create(url));
    if (!proxies.isEmpty()) {
        Optional<Proxy> proxy = proxies.stream().filter(p -> p.type().equals(Proxy.Type.HTTP))
                .findFirst();
        if (proxy.isPresent()) {
            InetSocketAddress address = (InetSocketAddress) proxy.get().address();
            builder.setProxy(new HttpHost(address.getHostName(), address.getPort()));

            try {
                PasswordAuthentication auth = Authenticator.requestPasswordAuthentication(
                        address.getHostName(), null, address.getPort(),
                        (url.startsWith("https://") ? "https" : "http"),
                        "Credentials for proxy " + proxy, null, new URL(url),
                        Authenticator.RequestorType.PROXY);
                if (auth != null) {
                    CredentialsProvider credsProvider = new BasicCredentialsProvider();
                    credsProvider.setCredentials(
                            new AuthScope(address.getHostName(), address.getPort()),
                            new UsernamePasswordCredentials(auth.getUserName(),
                                    String.valueOf(auth.getPassword())));
                    builder.setDefaultCredentialsProvider(credsProvider);
                }
            }
            catch (MalformedURLException e) {
            }
        }
    }
}
 
源代码8 项目: hesperides   文件: IntegTestHttpConfig.java
private CloseableHttpClient buildHttpClient() throws Exception {
    SSLContext sslContext = new SSLContextBuilder()
            .loadTrustMaterial(null, (certificate, authType) -> true).build();
    HttpClientBuilder httpClientBuilder = HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);
    if (proxyHost != null) {
        httpClientBuilder.setProxy(new HttpHost(proxyHost, proxyPort));
    }
    return httpClientBuilder.build();
}
 
源代码9 项目: beam-client-java   文件: MixerHttpClient.java
protected HttpClient buildHttpClient() {
    String proxyHost = System.getProperty(PROXY_HOST_PROP);
    String proxyPort = System.getProperty(PROXY_PORT_PROP);

    HttpClientBuilder builder = HttpClientBuilder.create().setDefaultCookieStore(this.cookieStore);

    if (proxyHost != null && proxyPort != null) {
        builder.setProxy(new HttpHost(proxyHost, Integer.parseInt(proxyPort)));
    }

    return builder.build();
}
 
源代码10 项目: find   文件: HodConfiguration.java
@Bean
public HttpClient httpClient() {
    final HttpClientBuilder builder = HttpClientBuilder.create();

    final String proxyHost = environment.getProperty("find.https.proxyHost");

    if (proxyHost != null) {
        final Integer proxyPort = Integer.valueOf(environment.getProperty("find.https.proxyPort", "8080"));
        builder.setProxy(new HttpHost(proxyHost, proxyPort));
    }

    builder.disableCookieManagement();

    return builder.build();
}
 
private static void configureProxy(final HttpClientBuilder builder, final Proxy proxy) {
    if (proxy != null && !Proxy.NO_PROXY.equals(proxy)) {
        final SocketAddress socketAddress = proxy.address();
        if (socketAddress instanceof InetSocketAddress) {
            final InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
            final String proxyHost = inetSocketAddress.getHostName();
            final int proxyPort = inetSocketAddress.getPort();
            final HttpHost httpHost = new HttpHost(proxyHost, proxyPort);
            builder.setProxy(httpHost);
        }
    }
}
 
源代码12 项目: glowroot   文件: SyntheticMonitorService.java
private static SyncHttpClientHolder createSyncHttpClientHolder(String shortVersion,
        HttpProxyConfig httpProxyConfig) {
    HttpClientBuilder builder = HttpClients.custom()
            .setUserAgent("GlowrootCentral" + shortVersion)
            .setDefaultHeaders(
                    Arrays.asList(new BasicHeader("Glowroot-Transaction-Type", "Synthetic")));
    if (!httpProxyConfig.host().isEmpty()) {
        int proxyPort = MoreObjects.firstNonNull(httpProxyConfig.port(), 80);
        builder.setProxy(new HttpHost(httpProxyConfig.host(), proxyPort));
    }
    CloseableHttpClient httpClient = builder.setMaxConnPerRoute(10) // increasing from default 2
            .setMaxConnTotal(1000) // increasing from default 20
            .build();
    return ImmutableSyncHttpClientHolder.of(httpClient, httpProxyConfig);
}
 
源代码13 项目: JVoiceXML   文件: LUISGrammarEvaluator.java
/**
 * {@inheritDoc}
 */
@Override
public Object getSemanticInterpretation(final DataModel model,
        String utterance) {
    final HttpClientBuilder builder = HttpClientBuilder.create();
    if (PROXY_HOST != null) {
        HttpHost proxy = new HttpHost(PROXY_HOST, PROXY_PORT);
        builder.setProxy(proxy);
    }
    try (CloseableHttpClient client = builder.build()){
        final URIBuilder uribuilder = new URIBuilder(grammarUri);
        uribuilder.addParameter("subscription-key", subscriptionKey);
        uribuilder.addParameter("q", utterance);
        final URI uri = uribuilder.build();
        final HttpGet request = new HttpGet(uri);
        final HttpResponse response = client.execute(request);
        final StatusLine statusLine = response.getStatusLine();
        final int status = statusLine.getStatusCode();
        if (status != HttpStatus.SC_OK) {
            final String reasonPhrase = statusLine.getReasonPhrase();
            LOGGER.error("error accessing '" + uri +"': " +
                    reasonPhrase + " (HTTP error code "
                    + status + ")");
            return null;
        }
        final HttpEntity entity = response.getEntity();
        final InputStream input = entity.getContent();
        final Object interpretation = parseLUISResponse(model, input);
        return interpretation;
    } catch (IOException | URISyntaxException | ParseException | SemanticError e) {
        LOGGER.error(e.getMessage(), e);
        return null;
    }
}
 
源代码14 项目: davmail   文件: TestHttpClient4.java
public void testTimeoutsWithProxy() throws IOException, InterruptedException {
    Settings.setLoggingLevel("org.apache.http", Level.DEBUG);
    Settings.setLoggingLevel("org.apache.http.impl.conn", Level.DEBUG);

    RegistryBuilder<ConnectionSocketFactory> schemeRegistry = RegistryBuilder.create();
    schemeRegistry.register("http", new PlainConnectionSocketFactory());
    schemeRegistry.register("https", new SSLConnectionSocketFactory(new DavGatewaySSLSocketFactory(),
            SSLConnectionSocketFactory.getDefaultHostnameVerifier()));

    Registry<ConnectionSocketFactory> registry = schemeRegistry.build();

    RequestConfig config = RequestConfig.custom()
            // time to get request from the pool
            .setConnectionRequestTimeout(5000)
            // socket connect timeout
            .setConnectTimeout(5000)
            // inactivity timeout
            .setSocketTimeout(5000)
            // disable redirect
            .setRedirectsEnabled(false)
            .build();
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
    HttpClientBuilder clientBuilder = HttpClientBuilder.create()
            .disableRedirectHandling()
            .setDefaultRequestConfig(config)
            .setConnectionManager(connectionManager);
    String proxyHost = Settings.getProperty("davmail.proxyHost");
    int proxyPort = Settings.getIntProperty("davmail.proxyPort");
    HttpHost proxy = new HttpHost(proxyHost, proxyPort);
    clientBuilder.setProxy(proxy);

    clientBuilder.setDefaultCredentialsProvider(getProxyCredentialProvider());

    IdleConnectionEvictor evictor = new IdleConnectionEvictor(connectionManager, 1, TimeUnit.MINUTES);
    evictor.start();

    try (CloseableHttpClient httpClient = clientBuilder.build()) {

        HttpGet httpget = new HttpGet("http://davmail.sourceforge.net/version.txt");

        try (CloseableHttpResponse response = httpClient.execute(httpget)) {
            assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
            String responseString = new BasicResponseHandler().handleResponse(response);
            System.out.println(responseString);
        }
        while (connectionManager.getTotalStats().getAvailable() > 0) {
            Thread.sleep(5000);
            System.out.println("Pool: " + connectionManager.getTotalStats());
        }
    } finally {
        evictor.shutdown();
    }

}
 
源代码15 项目: ais-sdk   文件: HttpClientUtils.java
public static CloseableHttpClient acceptsUntrustedCertsHttpClient(boolean withProxy, ProxyHostInfo hostInfo, int connectionTimeout, int connectionRequestTimeout, int socketTimeout)
		throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
	HttpClientBuilder b = HttpClientBuilder.create();
	
	/**
	 * set http proxy
	 */
	
	b.setDefaultRequestConfig( 
			RequestConfig.custom().setConnectTimeout(connectionTimeout).setConnectionRequestTimeout(connectionRequestTimeout).setSocketTimeout(socketTimeout).build()
			);
	
	if(withProxy){
		HttpHost proxy=new HttpHost(hostInfo.getHostName(),hostInfo.getPort());
		b.setProxy(proxy);
		CredentialsProvider credsProvider = new BasicCredentialsProvider();
		credsProvider.setCredentials(
				new AuthScope(proxy.getHostName(), proxy.getPort()),
				new UsernamePasswordCredentials(hostInfo.getUserName(), hostInfo.getPassword()));
		b.setDefaultCredentialsProvider(credsProvider);
	}
	
	SSLContext sslContext = new SSLContextBuilder().useProtocol("TLSv1.2").loadTrustMaterial(null, new TrustStrategy() {
		public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
			return true;
		}
	}).build();
	b.setSSLContext(sslContext);
	b.setConnectionTimeToLive(180, TimeUnit.SECONDS);

	HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;

	SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
	Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
			.register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory)
			.build();

	PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
	connMgr.setMaxTotal(200);
	connMgr.setDefaultMaxPerRoute(100);
	b.setConnectionManager(connMgr);
	CloseableHttpClient client = b.build();
	return client;
}
 
源代码16 项目: ais-sdk   文件: HttpClientUtils.java
public static CloseableHttpClient acceptsUntrustedCertsHttpClient(boolean withProxy, ProxyHostInfo hostInfo, int connectionTimeout, int connectionRequestTimeout, int socketTimeout)
		throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
	HttpClientBuilder b = HttpClientBuilder.create();
	
	/**
	 * set http proxy
	 */
	
	b.setDefaultRequestConfig( 
			RequestConfig.custom().setConnectTimeout(connectionTimeout).setConnectionRequestTimeout(connectionRequestTimeout).setSocketTimeout(socketTimeout).build()
			);
	
	if(withProxy){
		HttpHost proxy=new HttpHost(hostInfo.getHostName(),hostInfo.getPort());
		b.setProxy(proxy);
		CredentialsProvider credsProvider = new BasicCredentialsProvider();
		credsProvider.setCredentials(
				new AuthScope(proxy.getHostName(), proxy.getPort()),
				new UsernamePasswordCredentials(hostInfo.getUserName(), hostInfo.getPassword()));
		b.setDefaultCredentialsProvider(credsProvider);
	}
	
	SSLContext sslContext = new SSLContextBuilder().useProtocol("TLSv1.2").loadTrustMaterial(null, new TrustStrategy() {
		public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
			return true;
		}
	}).build();
	b.setSSLContext(sslContext);
	b.setConnectionTimeToLive(180, TimeUnit.SECONDS);

	HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;

	SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
	Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
			.register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory)
			.build();

	PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
	connMgr.setMaxTotal(200);
	connMgr.setDefaultMaxPerRoute(100);
	b.setConnectionManager(connMgr);
	CloseableHttpClient client = b.build();
	return client;
}
 
源代码17 项目: cyberduck   文件: HttpConnectionPoolBuilder.java
/**
 * @param proxy    Proxy configuration
 * @param listener Log listener
 * @param prompt   Prompt for proxy credentials
 * @return Builder for HTTP client
 */
public HttpClientBuilder build(final Proxy proxy, final TranscriptListener listener, final LoginCallback prompt) {
    final HttpClientBuilder configuration = HttpClients.custom();
    // Use HTTP Connect proxy implementation provided here instead of
    // relying on internal proxy support in socket factory
    switch(proxy.getType()) {
        case HTTP:
        case HTTPS:
            final HttpHost h = new HttpHost(proxy.getHostname(), proxy.getPort(), Scheme.http.name());
            if(log.isInfoEnabled()) {
                log.info(String.format("Setup proxy %s", h));
            }
            configuration.setProxy(h);
            configuration.setProxyAuthenticationStrategy(new CallbackProxyAuthenticationStrategy(ProxyCredentialsStoreFactory.get(), host, prompt));
            break;
    }
    configuration.setUserAgent(new PreferencesUseragentProvider().get());
    final int timeout = preferences.getInteger("connection.timeout.seconds") * 1000;
    configuration.setDefaultSocketConfig(SocketConfig.custom()
        .setTcpNoDelay(true)
        .setSoTimeout(timeout)
        .build());
    configuration.setDefaultRequestConfig(this.createRequestConfig(timeout));
    configuration.setDefaultConnectionConfig(ConnectionConfig.custom()
        .setBufferSize(preferences.getInteger("http.socket.buffer"))
        .setCharset(Charset.forName(host.getEncoding()))
        .build());
    if(preferences.getBoolean("http.connections.reuse")) {
        configuration.setConnectionReuseStrategy(new DefaultClientConnectionReuseStrategy());
    }
    else {
        configuration.setConnectionReuseStrategy(new NoConnectionReuseStrategy());
    }
    configuration.setRetryHandler(new ExtendedHttpRequestRetryHandler(preferences.getInteger("http.connections.retry")));
    configuration.setServiceUnavailableRetryStrategy(new DisabledServiceUnavailableRetryStrategy());
    if(!preferences.getBoolean("http.compression.enable")) {
        configuration.disableContentCompression();
    }
    configuration.setRequestExecutor(new LoggingHttpRequestExecutor(listener));
    // Always register HTTP for possible use with proxy. Contains a number of protocol properties such as the
    // default port and the socket factory to be used to create the java.net.Socket instances for the given protocol
    configuration.setConnectionManager(this.createConnectionManager(this.createRegistry()));
    configuration.setDefaultAuthSchemeRegistry(RegistryBuilder.<AuthSchemeProvider>create()
        .register(AuthSchemes.BASIC, new BasicSchemeFactory(
            Charset.forName(preferences.getProperty("http.credentials.charset"))))
        .register(AuthSchemes.DIGEST, new DigestSchemeFactory(
            Charset.forName(preferences.getProperty("http.credentials.charset"))))
        .register(AuthSchemes.NTLM, preferences.getBoolean("webdav.ntlm.windows.authentication.enable") && WinHttpClients.isWinAuthAvailable() ?
            new BackportWindowsNTLMSchemeFactory(null) :
            new NTLMSchemeFactory())
        .register(AuthSchemes.SPNEGO, preferences.getBoolean("webdav.ntlm.windows.authentication.enable") && WinHttpClients.isWinAuthAvailable() ?
            new BackportWindowsNegotiateSchemeFactory(null) :
            new SPNegoSchemeFactory())
        .register(AuthSchemes.KERBEROS, new KerberosSchemeFactory()).build());
    return configuration;
}
 
源代码18 项目: metron   文件: TaxiiHandler.java
private static HttpClient buildClient(URL proxy, String username, String password) throws Exception
{
  HttpClient client = new HttpClient(); // Start with a default TAXII HTTP client.

  // Create an Apache HttpClientBuilder to be customized by the command line arguments.
  HttpClientBuilder builder = HttpClientBuilder.create().useSystemProperties();

  // Proxy
  if (proxy != null) {
    HttpHost proxyHost = new HttpHost(proxy.getHost(), proxy.getPort(), proxy.getProtocol());
    builder.setProxy(proxyHost);
  }

  // Basic authentication. User & Password
  if (username != null ^ password != null) {
    throw new Exception("'username' and 'password' arguments are required to appear together.");
  }


  // from:  http://stackoverflow.com/questions/19517538/ignoring-ssl-certificate-in-apache-httpclient-4-3
  SSLContextBuilder ssbldr = new SSLContextBuilder();
  ssbldr.loadTrustMaterial(null, new TrustSelfSignedStrategy());
  SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(ssbldr.build(),SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);


  Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
      .register("http", new PlainConnectionSocketFactory())
      .register("https", sslsf)
      .build();


  PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
  cm.setMaxTotal(20);//max connection

  System.setProperty("jsse.enableSNIExtension", "false"); //""
  CloseableHttpClient httpClient = builder
      .setSSLSocketFactory(sslsf)
      .setConnectionManager(cm)
      .build();

  client.setHttpclient(httpClient);
  return client;
}
 
源代码19 项目: irontest   文件: IronTestUtils.java
/**
 * This method trusts all SSL certificates exposed by the API.
 *
 * @param url
 * @param username
 * @param password
 * @param httpMethod
 * @param httpHeaders
 * @param httpBody
 * @return
 * @throws Exception
 */
public static HTTPAPIResponse invokeHTTPAPI(String url, String username, String password, HTTPMethod httpMethod,
                                            List<HTTPHeader> httpHeaders, String httpBody) throws Exception {
    UrlValidator urlValidator = new UrlValidator(new String[] {"http", "https"}, UrlValidator.ALLOW_LOCAL_URLS);
    if (!urlValidator.isValid(url)) {
        throw new RuntimeException("Invalid URL");
    }

    //  to allow special characters like whitespace in query parameters
    String safeUrl = UrlEscapers.urlFragmentEscaper().escape(url);

    //  create HTTP request object and set body if applicable
    HttpUriRequest httpRequest;
    switch (httpMethod) {
        case GET:
            httpRequest = new HttpGet(safeUrl);
            break;
        case POST:
            HttpPost httpPost = new HttpPost(safeUrl);
            httpPost.setEntity(httpBody == null ? null : new StringEntity(httpBody, "UTF-8"));    //  StringEntity doesn't accept null string (exception is thrown)
            httpRequest = httpPost;
            break;
        case PUT:
            HttpPut httpPut = new HttpPut(safeUrl);
            httpPut.setEntity(httpBody == null ? null : new StringEntity(httpBody, "UTF-8"));     //  StringEntity doesn't accept null string (exception is thrown)
            httpRequest = httpPut;
            break;
        case DELETE:
            httpRequest = new HttpDelete(safeUrl);
            break;
        default:
            throw new IllegalArgumentException("Unrecognized HTTP method " + httpMethod);
    }

    //  set request HTTP headers
    for (HTTPHeader httpHeader : httpHeaders) {
        httpRequest.setHeader(httpHeader.getName(), httpHeader.getValue());
    }
    //  set HTTP basic auth
    if (!"".equals(StringUtils.trimToEmpty(username))) {
        String auth = username + ":" + password;
        String encodedAuth = Base64.encodeBase64String(auth.getBytes());
        String authHeader = "Basic " + encodedAuth;
        httpRequest.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
    }

    final HTTPAPIResponse apiResponse = new HTTPAPIResponse();
    ResponseHandler<Void> responseHandler = httpResponse -> {
        apiResponse.setStatusCode(httpResponse.getStatusLine().getStatusCode());
        apiResponse.getHttpHeaders().add(
                new HTTPHeader("*Status-Line*", httpResponse.getStatusLine().toString()));
        Header[] headers = httpResponse.getAllHeaders();
        for (Header header: headers) {
            apiResponse.getHttpHeaders().add(new HTTPHeader(header.getName(), header.getValue()));
        }
        HttpEntity entity = httpResponse.getEntity();
        apiResponse.setHttpBody(entity != null ? EntityUtils.toString(entity) : null);
        return null;
    };

    //  build HTTP Client instance, trusting all SSL certificates, using system HTTP proxy if needed and exists
    SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial((TrustStrategy) (chain, authType) -> true).build();
    HostnameVerifier allowAllHosts = new NoopHostnameVerifier();
    SSLConnectionSocketFactory connectionFactory = new SSLConnectionSocketFactory(sslContext, allowAllHosts);
    HttpClientBuilder httpClientBuilder = HttpClients.custom().setSSLSocketFactory(connectionFactory);
    InetAddress urlHost = InetAddress.getByName(new URL(url).getHost());
    if (!(urlHost.isLoopbackAddress() || urlHost.isSiteLocalAddress())) {    //  only use system proxy for external address
        Proxy systemHTTPProxy = getSystemHTTPProxy();
        if (systemHTTPProxy != null) {
            InetSocketAddress addr = (InetSocketAddress) systemHTTPProxy.address();
            httpClientBuilder.setProxy(new HttpHost(addr.getHostName(), addr.getPort()));
        }
    }
    HttpClient httpClient = httpClientBuilder.build();

    //  invoke the API
    try {
        httpClient.execute(httpRequest, responseHandler);
    } catch (ClientProtocolException e) {
        throw new RuntimeException(e.getCause().getMessage(), e);
    }

    return apiResponse;
}
 
源代码20 项目: dss   文件: CommonsDataLoader.java
/**
 * Configure the proxy with the required credential if needed
 *
 * @param httpClientBuilder
 * @param credentialsProvider
 * @param url
 * @return {@link HttpClientBuilder}
 */
private HttpClientBuilder configureProxy(HttpClientBuilder httpClientBuilder, CredentialsProvider credentialsProvider, String url) {
	if (proxyConfig == null) {
		return httpClientBuilder;
	}

	final String protocol = getURL(url).getProtocol();
	final boolean proxyHTTPS = Protocol.isHttps(protocol) && (proxyConfig.getHttpsProperties() != null);
	final boolean proxyHTTP = Protocol.isHttp(protocol) && (proxyConfig.getHttpProperties() != null);

	ProxyProperties proxyProps = null;
	if (proxyHTTPS) {
		LOG.debug("Use proxy https parameters");
		proxyProps = proxyConfig.getHttpsProperties();
	} else if (proxyHTTP) {
		LOG.debug("Use proxy http parameters");
		proxyProps = proxyConfig.getHttpProperties();
	} else {
		return httpClientBuilder;
	}

	String proxyHost = proxyProps.getHost();
	int proxyPort = proxyProps.getPort();
	String proxyUser = proxyProps.getUser();
	String proxyPassword = proxyProps.getPassword();
	String proxyExcludedHosts = proxyProps.getExcludedHosts();

	if (Utils.isStringNotEmpty(proxyUser) && Utils.isStringNotEmpty(proxyPassword)) {
		AuthScope proxyAuth = new AuthScope(proxyHost, proxyPort);
		UsernamePasswordCredentials proxyCredentials = new UsernamePasswordCredentials(proxyUser, proxyPassword);
		credentialsProvider.setCredentials(proxyAuth, proxyCredentials);
	}

	LOG.debug("proxy host/port: {}:{}", proxyHost, proxyPort);
	// TODO SSL peer shut down incorrectly when protocol is https
	final HttpHost proxy = new HttpHost(proxyHost, proxyPort, Protocol.HTTP.getName());

	if (Utils.isStringNotEmpty(proxyExcludedHosts)) {
		final String[] hosts = proxyExcludedHosts.split("[,; ]");

		HttpRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy) {
			@Override
			public HttpRoute determineRoute(final HttpHost host, final HttpRequest request, final HttpContext context) throws HttpException {

				String hostname = (host != null ? host.getHostName() : null);

				if ((hosts != null) && (hostname != null)) {
					for (String h : hosts) {
						if (hostname.equalsIgnoreCase(h)) {
							// bypass proxy for that hostname
							return new HttpRoute(host);
						}
					}
				}
				return super.determineRoute(host, request, context);
			}
		};

		httpClientBuilder.setRoutePlanner(routePlanner);
	}

	return httpClientBuilder.setProxy(proxy);
}