Java Code Examples for org.apache.http.impl.client.HttpClientBuilder#setUserAgent()

The following examples show how to use org.apache.http.impl.client.HttpClientBuilder#setUserAgent() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: htmlunit   File: HttpWebConnection.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Lazily initializes the internal HTTP client.
 *
 * @return the initialized HTTP client
 */
protected HttpClientBuilder getHttpClientBuilder() {
    final Thread currentThread = Thread.currentThread();
    HttpClientBuilder builder = httpClientBuilder_.get(currentThread);
    if (builder == null) {
        builder = createHttpClientBuilder();

        // this factory is required later
        // to be sure this is done, we do it outside the createHttpClient() call
        final RegistryBuilder<CookieSpecProvider> registeryBuilder
            = RegistryBuilder.<CookieSpecProvider>create()
                        .register(HACKED_COOKIE_POLICY, htmlUnitCookieSpecProvider_);
        builder.setDefaultCookieSpecRegistry(registeryBuilder.build());

        builder.setDefaultCookieStore(new HtmlUnitCookieStore(webClient_.getCookieManager()));
        builder.setUserAgent(webClient_.getBrowserVersion().getUserAgent());
        httpClientBuilder_.put(currentThread, builder);
    }

    return builder;
}
 
Example 2
Source Project: cetty   File: SyncHttpClientGenerator.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected CloseableHttpClient build(Payload payload) {
    HttpClientBuilder httpClientBuilder = HttpClients.custom();

    httpClientBuilder.setConnectionManager(poolingHttpClientConnectionManager);
    if (payload.getUserAgent() != null) {
        httpClientBuilder.setUserAgent(payload.getUserAgent());
    } else {
        httpClientBuilder.setUserAgent("");
    }

    httpClientBuilder.setConnectionManagerShared(true);

    httpClientBuilder.setRedirectStrategy(new CustomRedirectStrategy());

    SocketConfig.Builder socketConfigBuilder = SocketConfig.custom();
    socketConfigBuilder.setSoKeepAlive(true).setTcpNoDelay(true);
    socketConfigBuilder.setSoTimeout(payload.getSocketTimeout());
    SocketConfig socketConfig = socketConfigBuilder.build();
    httpClientBuilder.setDefaultSocketConfig(socketConfig);
    poolingHttpClientConnectionManager.setDefaultSocketConfig(socketConfig);
    httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(payload.getRetryTimes(), true));
    reduceCookie(httpClientBuilder, payload);
    return  httpClientBuilder.build();
}
 
Example 3
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();
 }
 
Example 4
/**
 * Creates a HttpClientBuilder with the default settings of GERBIL.
 * 
 * @return a HttpClientBuilder with the default settings of GERBIL.
 */
public HttpClientBuilder generateHttpClientBuilder() {
    HttpClientBuilder builder = HttpClientBuilder.create();
    builder.setUserAgent(userAgent);

    String proxyHost = GerbilConfiguration.getInstance().getString(PROXY_HOST_KEY);
    int proxyPort = GerbilConfiguration.getInstance().getInt(PROXY_PORT_KEY, DEFAULT_PROXY_PORT);

    if (proxyHost != null) {
        HttpHost proxy = new HttpHost(proxyHost, proxyPort);
        DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
        builder.setRoutePlanner(routePlanner);
    }
    // Use a redirect strategy that allows the "direct redirect" of POST requests
    // without creating a completely new request
    builder.setRedirectStrategy(new SimpleRedirectStrategy()).build();

    return builder;
}
 
Example 5
/**
 * Returns a client with all our selected properties / params.
 * 
 * @param timeout
 *            - socket timeout to set
 * @return client
 */
public static final HttpClient getClient(int timeout) {
    HttpClientBuilder client = HttpClientBuilder.create();
    // set the connection manager
    client.setConnectionManager(connectionManager);
    // dont retry
    client.setRetryHandler(new DefaultHttpRequestRetryHandler(0, false));
    // establish a connection within x seconds
    RequestConfig config = RequestConfig.custom().setSocketTimeout(timeout).build();
    client.setDefaultRequestConfig(config);
    // no redirects
    client.disableRedirectHandling();
    // set custom ua
    client.setUserAgent(userAgent);
    // set the proxy if the user has one set
    if ((System.getProperty("http.proxyHost") != null) && (System.getProperty("http.proxyPort") != null)) {
        HttpHost proxy = new HttpHost(System.getProperty("http.proxyHost").toString(), Integer.valueOf(System.getProperty("http.proxyPort")));
        client.setProxy(proxy);
    }
    return client.build();
}
 
Example 6
protected void configClient(HttpClientBuilder cb, Map<Prop, Object> settings) throws HTTPException {
  cb.useSystemProperties();
  String agent = (String) settings.get(Prop.USER_AGENT);
  if (agent != null)
    cb.setUserAgent(agent);
  session.setInterceptors(cb);
  session.setContentDecoderRegistry(cb);
  session.setClientManager(cb, this);
  session.setRetryHandler(cb);
}
 
Example 7
Source Project: anyline   File: HttpUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static CloseableHttpClient createClient(String userAgent){ 
	CloseableHttpClient client = null; 
	HttpClientBuilder builder = HttpClients.custom().setDefaultRequestConfig(requestConfig); 
	builder.setUserAgent(userAgent); 
	client = builder.build(); 
	return client; 
}
 
Example 8
Source Project: anyline   File: HttpUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static CloseableHttpClient createClient(){ 
	CloseableHttpClient client = null; 
	HttpClientBuilder builder = HttpClients.custom().setDefaultRequestConfig(requestConfig); 
	builder.setUserAgent(userAgent); 
	client = builder.build(); 
	return client; 
}
 
Example 9
Source Project: anyline   File: HttpUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static CloseableHttpClient defaultSSLClient(){ 
	if(null == client){ 
		HttpClientBuilder builder = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()).setConnectionManager(connManager).setDefaultRequestConfig(requestConfig); 
		builder.setUserAgent(userAgent);    
		client = builder.build(); 
	} 
	return client; 
}
 
Example 10
CloseableHttpClient createClient() {

        HttpClientBuilder builder = HttpClientBuilder.create();
        builder.setDefaultCookieStore(cookieStore);
        builder.setDefaultCredentialsProvider(credentialsProvider);
        builder.setUserAgent(format("SAP CM Client/%s based on Olingo v%s",
                getShortVersion(),
                getOlingoV2Version()));
        return builder.build();
    }
 
Example 11
@PostConstruct
public void init() {
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    httpClientBuilder.setUserAgent("JuniperBot DiscordBot (https://github.com/goldrenard/JuniperBot, 1.0)");
    HttpActionClient httpActionClient = HttpActionClient.builder()
            .withUrl(SCRIPT_ENDPOINT)
            .withUserAgent("JuniperBot", version, "[email protected]")
            .withClient(httpClientBuilder.build())
            .build();
    client = new MediaWikiBot(httpActionClient);
}
 
Example 12
Source Project: webmagic   File: HttpClientGenerator.java    License: Apache License 2.0 5 votes vote down vote up
private CloseableHttpClient generateClient(Site site) {
    HttpClientBuilder httpClientBuilder = HttpClients.custom();

    httpClientBuilder.setConnectionManager(connectionManager);
    if (site.getUserAgent() != null) {
        httpClientBuilder.setUserAgent(site.getUserAgent());
    } else {
        httpClientBuilder.setUserAgent("");
    }
    if (site.isUseGzip()) {
        httpClientBuilder.addInterceptorFirst(new HttpRequestInterceptor() {

            public void process(
                    final HttpRequest request,
                    final HttpContext context) throws HttpException, IOException {
                if (!request.containsHeader("Accept-Encoding")) {
                    request.addHeader("Accept-Encoding", "gzip");
                }
            }
        });
    }
    //解决post/redirect/post 302跳转问题
    httpClientBuilder.setRedirectStrategy(new CustomRedirectStrategy());

    SocketConfig.Builder socketConfigBuilder = SocketConfig.custom();
    socketConfigBuilder.setSoKeepAlive(true).setTcpNoDelay(true);
    socketConfigBuilder.setSoTimeout(site.getTimeOut());
    SocketConfig socketConfig = socketConfigBuilder.build();
    httpClientBuilder.setDefaultSocketConfig(socketConfig);
    connectionManager.setDefaultSocketConfig(socketConfig);
    httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(site.getRetryTimes(), true));
    generateCookie(httpClientBuilder, site);
    return httpClientBuilder.build();
}
 
Example 13
Source Project: git-lfs-migrate   File: Main.java    License: MIT License 5 votes vote down vote up
@NotNull
private static Client createClient(@NotNull AuthProvider auth, @NotNull CmdArgs cmd) throws GeneralSecurityException {
  final HttpClientBuilder httpBuilder = HttpClients.custom();
  httpBuilder.setUserAgent("git-lfs-migrate");
  if (cmd.noCheckCertificate) {
    httpBuilder.setSSLHostnameVerifier((hostname, session) -> true);
    httpBuilder.setSSLContext(SSLContexts.custom()
        .loadTrustMaterial((chain, authType) -> true)
        .build());
  }
  return new Client(auth, httpBuilder.build());
}
 
Example 14
Source Project: activitystreams   File: HttpFetch.java    License: Apache License 2.0 5 votes vote down vote up
private CloseableHttpClient initClient(Builder builder) {
  HttpClientBuilder b = builder.builder;
  b.setConnectionManager(manager);
  ImmutableSet<Header> headers = 
    builder.defaultHeaders.build();
  if (!headers.isEmpty())
    b.setDefaultHeaders(headers);
  if (builder.userAgent != null)
    b.setUserAgent(builder.userAgent);
  return b.build();
}
 
Example 15
Source Project: anyline   File: HttpUtil.java    License: Apache License 2.0 4 votes vote down vote up
private static CloseableHttpClient defaultClient(){ 
	HttpClientBuilder builder = HttpClients.custom().setDefaultRequestConfig(requestConfig); 
	builder.setUserAgent(userAgent); 
	client = builder.build(); 
	return client; 
}
 
Example 16
@Override
public void configureHttpClient(HttpClientBuilder httpClientBuilder) {
    httpClientBuilder.setUserAgent("WildFly-Camel v1.0");
}
 
Example 17
private synchronized void prepare() {
  if (prepared.get()) {
    return;
  }
  Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
    .register("http", this.plainConnectionSocketFactory)
    .register("https", this.sslConnectionSocketFactory)
    .build();

  @SuppressWarnings("resource")
  PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
  connectionManager.setMaxTotal(this.maxTotalConn);
  connectionManager.setDefaultMaxPerRoute(this.maxConnPerHost);
  connectionManager.setDefaultSocketConfig(
    SocketConfig.copy(SocketConfig.DEFAULT)
      .setSoTimeout(this.soTimeout)
      .build()
  );

  this.idleConnectionMonitorThread = new IdleConnectionMonitorThread(
    connectionManager, this.idleConnTimeout, this.checkWaitTime);
  this.idleConnectionMonitorThread.setDaemon(true);
  this.idleConnectionMonitorThread.start();

  HttpClientBuilder httpClientBuilder = HttpClients.custom()
    .setConnectionManager(connectionManager)
    .setConnectionManagerShared(true)
    .setSSLSocketFactory(this.buildSSLConnectionSocketFactory())
    .setDefaultRequestConfig(
      RequestConfig.custom()
        .setSocketTimeout(this.soTimeout)
        .setConnectTimeout(this.connectionTimeout)
        .setConnectionRequestTimeout(this.connectionRequestTimeout)
        .build()
    )
    .setRetryHandler(this.httpRequestRetryHandler);

  if (StringUtils.isNotBlank(this.httpProxyHost)
    && StringUtils.isNotBlank(this.httpProxyUsername)) {
    // 使用代理服务器 需要用户认证的代理服务器
    CredentialsProvider provider = new BasicCredentialsProvider();
    provider.setCredentials(
      new AuthScope(this.httpProxyHost, this.httpProxyPort),
      new UsernamePasswordCredentials(this.httpProxyUsername,
        this.httpProxyPassword));
    httpClientBuilder.setDefaultCredentialsProvider(provider);
  }

  if (StringUtils.isNotBlank(this.userAgent)) {
    httpClientBuilder.setUserAgent(this.userAgent);
  }
  this.closeableHttpClient = httpClientBuilder.build();
  prepared.set(true);
}
 
Example 18
/**
 * @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;
}
 
Example 19
Source Project: vespa   File: ApacheGatewayConnection.java    License: Apache License 2.0 4 votes vote down vote up
public HttpClient createClient() {
    HttpClientBuilder clientBuilder;
    if (connectionParams.useTlsConfigFromEnvironment()) {
        clientBuilder = VespaHttpClientBuilder.create();
    } else {
        clientBuilder = HttpClientBuilder.create();
        if (connectionParams.getSslContext() != null) {
            setSslContext(clientBuilder, connectionParams.getSslContext());
        } else {
            SslContextBuilder builder = new SslContextBuilder();
            if (connectionParams.getPrivateKey() != null && connectionParams.getCertificate() != null) {
                builder.withKeyStore(connectionParams.getPrivateKey(), connectionParams.getCertificate());
            }
            if (connectionParams.getCaCertificates() != null) {
                builder.withTrustStore(connectionParams.getCaCertificates());
            }
            setSslContext(clientBuilder, builder.build());
        }
        if (connectionParams.getHostnameVerifier() != null) {
            clientBuilder.setSSLHostnameVerifier(connectionParams.getHostnameVerifier());
        }
        clientBuilder.setUserTokenHandler(context -> null); // https://stackoverflow.com/a/42112034/1615280
    }
    clientBuilder.setMaxConnPerRoute(1);
    clientBuilder.setMaxConnTotal(1);
    clientBuilder.setConnectionTimeToLive(connectionParams.getConnectionTimeToLive().getSeconds(), TimeUnit.SECONDS);
    clientBuilder.setUserAgent(String.format("vespa-http-client (%s)", Vtag.currentVersion));
    clientBuilder.setDefaultHeaders(Collections.singletonList(new BasicHeader(Headers.CLIENT_VERSION, Vtag.currentVersion)));
    clientBuilder.disableContentCompression();
    // Try to disable the disabling to see if system tests become stable again.
    // clientBuilder.disableAutomaticRetries();
    RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
    requestConfigBuilder.setSocketTimeout(0);
    if (connectionParams.getProxyHost() != null) {
        requestConfigBuilder.setProxy(new HttpHost(connectionParams.getProxyHost(), connectionParams.getProxyPort()));
    }
    clientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());

    log.fine("Creating HttpClient: " + " ConnectionTimeout "
                    + " SocketTimeout 0 secs "
                    + " proxyhost (can be null) " + connectionParams.getProxyHost()
                    + ":" + connectionParams.getProxyPort()
                    + (useSsl ? " using ssl " : " not using ssl")
    );
    return clientBuilder.build();
}
 
Example 20
/**
 * Allows for verification on unverifiable final method. NOTE: if you modify the behavior of this
 * method beyond simply delegating to {@link HttpClientBuilder#setUserAgent}, write a unit test for it.
 */
@VisibleForTesting
void setUserAgent(HttpClientBuilder builder, String value) {
  builder.setUserAgent(value);
}