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() . 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: HttpConnectionUtil.java    From red5-server-common with Apache License 2.0 6 votes vote down vote up
/**
 * 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 2
Source File: HttpManagement.java    From gerbil with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 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 3
Source File: HttpWebConnection.java    From htmlunit with 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 4
Source File: SyncHttpClientGenerator.java    From cetty with 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 5
Source File: HttpRequestHandler.java    From Asqatasun with GNU Affero General Public License v3.0 6 votes vote down vote up
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 6
Source File: HttpFetch.java    From activitystreams with 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 7
Source File: HttpUtil.java    From anyline with 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 File: HttpUtil.java    From anyline with 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 File: HttpUtil.java    From anyline with 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
Source File: HttpClientFactory.java    From devops-cm-client with Apache License 2.0 5 votes vote down vote up
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
Source File: HTTPMethod.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 12
Source File: WikiFurService.java    From JuniperBot with GNU General Public License v3.0 5 votes vote down vote up
@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 13
Source File: Main.java    From git-lfs-migrate with 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 File: HttpClientGenerator.java    From webmagic with 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 15
Source File: HttpConnectionPoolBuilder.java    From cyberduck with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @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 16
Source File: HttpClientManagerImpl.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * 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);
}
 
Example 17
Source File: ApacheGatewayConnection.java    From vespa with 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 18
Source File: DefaultApacheHttpClientBuilder.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
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 19
Source File: Http4ClientConfigurer.java    From wildfly-camel with Apache License 2.0 4 votes vote down vote up
@Override
public void configureHttpClient(HttpClientBuilder httpClientBuilder) {
    httpClientBuilder.setUserAgent("WildFly-Camel v1.0");
}
 
Example 20
Source File: HttpUtil.java    From anyline with 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; 
}