当前位置: 首页>>代码示例>>Java>>正文


Java HttpClientBuilder.setProxy方法代码示例

本文整理汇总了Java中org.apache.http.impl.client.HttpClientBuilder.setProxy方法的典型用法代码示例。如果您正苦于以下问题:Java HttpClientBuilder.setProxy方法的具体用法?Java HttpClientBuilder.setProxy怎么用?Java HttpClientBuilder.setProxy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.http.impl.client.HttpClientBuilder的用法示例。


在下文中一共展示了HttpClientBuilder.setProxy方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: runSparqlUpdate

import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
/**
 * @param sparql
 * @return
 */
public boolean runSparqlUpdate(String sparql) {

  if (logger.isDebugEnabled())
    logger.debug("run SPARQL update: {}", sparql);

  HttpClientBuilder clientBuilder = HttpClientBuilder.create();

  setCloudAuthHeaderIfConfigured(clientBuilder);

  if (proxyHost != null && proxyPort != null) {
    HttpHost proxy = new HttpHost(proxyHost, proxyPort);
    clientBuilder.setProxy(proxy);
  }

  try (CloseableHttpClient client = clientBuilder.build()) {
    UpdateRequest update = UpdateFactory.create(sparql, Syntax.syntaxARQ);
    UpdateProcessor processor = UpdateExecutionFactory.createRemoteForm(update, buildSPARQLUrl().toString(), client);
    processor.execute();
  } catch (IOException ioe) {
    throw new RuntimeException("IOException.", ioe);
  }
  return true;
}
 
开发者ID:Smartlogic-Semaphore-Limited,项目名称:Java-APIs,代码行数:28,代码来源:OEModelEndpoint.java

示例2: initHttpClient

import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
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()
                    .setConnectionRequestTimeout(
                            this.clientConfig.getConnectionRequestTimeout())
                    .setConnectTimeout(this.clientConfig.getConnectionTimeout())
                    .setSocketTimeout(this.clientConfig.getSocketTimeout()).build();
    this.idleConnectionMonitor = new IdleConnectionMonitorThread(this.connectionManager);
    this.idleConnectionMonitor.start();
}
 
开发者ID:tencentyun,项目名称:cos-java-sdk-v5,代码行数:23,代码来源:DefaultCosHttpClient.java

示例3: runSparqlQuery

import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
/**
 * Runs the SPARQL query and returns a detached ResultSet.
 * If you have a large query, use the same technique inline to stream results and save memory.
 *
 * @param sparql
 * @return
 */
public ResultSet runSparqlQuery(String sparql) {

  if (logger.isDebugEnabled())
    logger.debug("run SPARQL query: {}", sparql);

  Query query = QueryFactory.create(sparql);
  ResultSet results = null;
  HttpClientBuilder clientBuilder = HttpClientBuilder.create();

  setCloudAuthHeaderIfConfigured(clientBuilder);

  if (proxyHost != null && proxyPort != null) {
    HttpHost proxy = new HttpHost(proxyHost, proxyPort);
    clientBuilder.setProxy(proxy);
  }

  try (CloseableHttpClient client = clientBuilder.build();
       QueryExecution qe = QueryExecutionFactory.sparqlService(buildSPARQLUrl().toString(), query, client)) {
    results = ResultSetFactory.copyResults(qe.execSelect());
  } catch (IOException ioe) {
    throw new RuntimeException("IOException.", ioe);
  }
  return results;
}
 
开发者ID:Smartlogic-Semaphore-Limited,项目名称:Java-APIs,代码行数:32,代码来源:OEModelEndpoint.java

示例4: getBuilder

import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
@NotNull
static HttpClientBuilder getBuilder() {
  final HttpClientBuilder builder = HttpClients.custom().setSSLContext(CertificateManager.getInstance().getSslContext()).
    setMaxConnPerRoute(100000).setConnectionReuseStrategy(DefaultConnectionReuseStrategy.INSTANCE);

  final HttpConfigurable proxyConfigurable = HttpConfigurable.getInstance();
  final List<Proxy> proxies = proxyConfigurable.getOnlyBySettingsSelector().select(URI.create(EduStepicNames.STEPIC_URL));
  final InetSocketAddress address = proxies.size() > 0 ? (InetSocketAddress)proxies.get(0).address() : null;
  if (address != null) {
    builder.setProxy(new HttpHost(address.getHostName(), address.getPort()));
  }
  final ConfirmingTrustManager trustManager = CertificateManager.getInstance().getTrustManager();
  try {
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, new TrustManager[]{trustManager}, new SecureRandom());
    builder.setSSLContext(sslContext);
  }
  catch (NoSuchAlgorithmException | KeyManagementException e) {
    LOG.error(e.getMessage());
  }
  return builder;
}
 
开发者ID:medvector,项目名称:educational-plugin,代码行数:23,代码来源:EduStepicClient.java

示例5: getProxyExecutor

import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
public static HttpCommandExecutor getProxyExecutor(URL url, Properties prop) {

        prop = decrypt(prop);

        String proxyHost = prop.getProperty("proxyHost");
        int proxyPort = Integer.valueOf(prop.getProperty("proxyPort"));
        String proxyUserDomain = prop.getProperty("proxyUserDomain");
        String proxyUser = prop.getProperty("proxyUser");
        String proxyPassword = prop.getProperty("proxyPassword");

        HttpClientBuilder builder = HttpClientBuilder.create();
        HttpHost proxy = new HttpHost(proxyHost, proxyPort);
        CredentialsProvider credsProvider = new BasicCredentialsProvider();

        credsProvider.setCredentials(new AuthScope(proxyHost, proxyPort),
                new NTCredentials(proxyUser, proxyPassword, getWorkstation(), proxyUserDomain));
        if (url.getUserInfo() != null && !url.getUserInfo().isEmpty()) {
            credsProvider.setCredentials(new AuthScope(url.getHost(), (url.getPort() > 0 ? url.getPort() : url.getDefaultPort())),
                    new UsernamePasswordCredentials(proxyUser, proxyPassword));
        }
        builder.setProxy(proxy);
        builder.setDefaultCredentialsProvider(credsProvider);
        HttpClient.Factory factory = new SimpleHttpClientFactory(builder);
        return new HttpCommandExecutor(new HashMap<String, CommandInfo>(), url, factory);

    }
 
开发者ID:CognizantQAHub,项目名称:Cognizant-Intelligent-Test-Scripter,代码行数:27,代码来源:RemoteProxy.java

示例6: createRequestFactory

import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
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();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);

    return requestFactory;
}
 
开发者ID:SAP,项目名称:cf-java-client-sap,代码行数:32,代码来源:RestUtil.java

示例7: getHttpClient

import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
/**
 * Construct httpclient with SSL protocol
 *
 * @param clientConfiguration the client configuration
 * @return {@code CloseableHttpClient}
 * @throws Exception
 */
public static CloseableHttpClient getHttpClient(ClientConfiguration clientConfiguration) throws Exception {
    if (clientConfiguration == null) {
        clientConfiguration = new ClientConfiguration();
    }

    SSLConnectionSocketFactory sslSocketFactory = createSslConnectionSocketFactory(clientConfiguration);
    HttpClientBuilder builder = HttpClients.custom();

    // set proxy
    String proxyHost = clientConfiguration.getProxyHost();
    int proxyPort = clientConfiguration.getProxyPort();

    if (!StringUtils.isEmpty(proxyHost) && proxyPort > 0) {
        HttpHost proxy = new HttpHost(proxyHost, proxyPort);
        builder.setProxy(proxy);

        String username = clientConfiguration.getProxyUserName();
        String password = clientConfiguration.getProxyPassword();

        if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
            CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            AuthScope authscope = new AuthScope(proxy);
            Credentials credentials = new UsernamePasswordCredentials(username,
                    password);
            credentialsProvider.setCredentials(authscope, credentials);
            builder.setDefaultCredentialsProvider(credentialsProvider);
        }
    }
    builder.setUserAgent(VersionUtil.getDefaultUserAgent());
    CloseableHttpClient httpclient = builder.setSSLSocketFactory(sslSocketFactory).build();
    return httpclient;
}
 
开发者ID:SimpleMessageNotification,项目名称:smn-sdk-java,代码行数:40,代码来源:HttpUtil.java

示例8: build

import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
/**
 * @param listener Log listener
 * @param prompt   Prompt for proxy credentials
 * @return Builder for HTTP client
 */
public HttpClientBuilder build(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
    final Proxy proxy = proxyFinder.find(host);
    switch(proxy.getType()) {
        case HTTP:
        case HTTPS:
            final HttpHost h = new HttpHost(proxy.getHostname(), proxy.getPort(), StringUtils.lowerCase(proxy.getType().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));
    final String encoding;
    if(null == host.getEncoding()) {
        encoding = preferences.getProperty("browser.charset.encoding");
    }
    else {
        encoding = host.getEncoding();
    }
    configuration.setDefaultConnectionConfig(ConnectionConfig.custom()
        .setBufferSize(preferences.getInteger("http.socket.buffer"))
        .setCharset(Charset.forName(encoding))
        .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, new NTLMSchemeFactory())
        .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory())
        .register(AuthSchemes.KERBEROS, new KerberosSchemeFactory()).build());
    return configuration;
}
 
开发者ID:iterate-ch,项目名称:cyberduck,代码行数:65,代码来源:HttpConnectionPoolBuilder.java


注:本文中的org.apache.http.impl.client.HttpClientBuilder.setProxy方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。