當前位置: 首頁>>代碼示例>>Java>>正文


Java HttpClientBuilder.setUserAgent方法代碼示例

本文整理匯總了Java中org.apache.http.impl.client.HttpClientBuilder.setUserAgent方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpClientBuilder.setUserAgent方法的具體用法?Java HttpClientBuilder.setUserAgent怎麽用?Java HttpClientBuilder.setUserAgent使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.http.impl.client.HttpClientBuilder的用法示例。


在下文中一共展示了HttpClientBuilder.setUserAgent方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: createHttpClientBuilder

import org.apache.http.impl.client.HttpClientBuilder; //導入方法依賴的package包/類
protected HttpClientBuilder createHttpClientBuilder(SiteConfig siteConfig) {
    HttpClientBuilder httpClientBuilder = HttpClients.custom();

    httpClientBuilder.setConnectionTimeToLive(siteConfig.getConnTimeToLiveMillis(), TimeUnit.MILLISECONDS);
    httpClientBuilder.setMaxConnPerRoute(siteConfig.getMaxConnPerRoute());
    httpClientBuilder.setMaxConnTotal(siteConfig.getMaxConnTotal());
    httpClientBuilder.setUserAgent(siteConfig.getUserAgent());

    httpClientBuilder.setRetryHandler(createHttpRequestRetryHandler());
    httpClientBuilder.setRedirectStrategy(createRedirectStrategy());
    httpClientBuilder.setSSLContext(createSSLContext());
    httpClientBuilder.setSSLHostnameVerifier(createSSLHostnameVerifier());

    httpClientBuilder.setDefaultConnectionConfig(createConnectionConfig(siteConfig));
    httpClientBuilder.setDefaultSocketConfig(createSocketConfig(siteConfig));
    httpClientBuilder.setDefaultCookieSpecRegistry(createCookieSpecRegistry());
    httpClientBuilder.setDefaultCookieStore(createCookieStore());

    return httpClientBuilder;
}
 
開發者ID:brucezee,項目名稱:jspider,代碼行數:21,代碼來源:HttpClientFactory.java

示例2: makeHttpClient

import org.apache.http.impl.client.HttpClientBuilder; //導入方法依賴的package包/類
/**
 * Creates new HttpClient for interacting with webmate API.
 *
 * @param authInfo    authentication information (email address + token)
 * @param environment API endpoint address
 * @return new HttpClient to be used by Service clients.
 */
private static HttpClient makeHttpClient(WebmateAuthInfo authInfo, WebmateEnvironment environment, HttpClientBuilder httpClientBuilder) {

    httpClientBuilder.setUserAgent(WEBMATE_JAVASDK_USERAGENT);

    List<Header> headers = new ArrayList<>();

    headers.add(new BasicHeader(WEBMATE_USER_HEADERKEY, authInfo.emailAddress));
    headers.add(new BasicHeader(WEBMATE_APITOKEN_HEADERKEY, authInfo.apiKey));
    headers.add(new BasicHeader("Content-Type", "application/json"));

    httpClientBuilder.setDefaultHeaders(headers);
    return httpClientBuilder.build();
}
 
開發者ID:webmate-io,項目名稱:webmate-sdk-java,代碼行數:21,代碼來源:WebmateApiClient.java

示例3: 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

示例4: configureUserAgent

import org.apache.http.impl.client.HttpClientBuilder; //導入方法依賴的package包/類
public void configureUserAgent(HttpClientBuilder builder) {
    builder.setUserAgent(UriTextResource.getUserAgentString());
}
 
開發者ID:lxxlxx888,項目名稱:Reer,代碼行數:4,代碼來源:HttpClientConfigurer.java

示例5: 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.setUserAgent方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。