Example usage for org.apache.http.client.protocol RequestAcceptEncoding RequestAcceptEncoding

List of usage examples for org.apache.http.client.protocol RequestAcceptEncoding RequestAcceptEncoding

Introduction

In this page you can find the example usage for org.apache.http.client.protocol RequestAcceptEncoding RequestAcceptEncoding.

Prototype

RequestAcceptEncoding

Source Link

Usage

From source file:com.arcbees.vcs.util.HttpClientWrapperImpl.java

private CloseableHttpClient initHttpClient() {
    RequestConfig requestConfig = getRequestConfig();

    String serverVersion = ServerVersionHolder.getVersion().getDisplayVersion();

    connectionManager = new PoolingHttpClientConnectionManager();

    return HttpClientBuilder.create().useSystemProperties().addInterceptorFirst(new RequestAcceptEncoding())
            .addInterceptorFirst(new ResponseContentEncoding())
            .setRetryHandler(new DefaultHttpRequestRetryHandler(RETRY_COUNT, true))
            .setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig)
            .setUserAgent("JetBrains TeamCity " + serverVersion).build();
}

From source file:org.iglootools.hchelpers.core.DefaultHttpClientFactory.java

private static void handleGzipContentCompression(DefaultHttpClient httpClient) {
    httpClient.addRequestInterceptor(new RequestAcceptEncoding());
    httpClient.addResponseInterceptor(new ResponseContentEncoding());
}

From source file:net.oneandone.shared.artifactory.ArtifactoryModule.java

@Provides
@Named(value = "httpclient")
HttpClient provideHttpClient() {/*w w  w .j a  va  2 s .c  o m*/
    final DefaultHttpClient client = new DefaultHttpClient();
    client.addRequestInterceptor(new RequestAcceptEncoding());
    client.addResponseInterceptor(new ResponseContentEncoding());
    client.addRequestInterceptor(providePreemptiveRequestInterceptor());
    return client;
}

From source file:com.cxic.ip.WebUrl.java

public String getResponseStr() {
    String userAgentCopy = VersionInfo.getUserAgent("Apache-HttpClient", "org.apache.http.client", getClass());

    HttpProcessor httpprocessorCopy = null;
    if (httpprocessorCopy == null) {
        final HttpProcessorBuilder b = HttpProcessorBuilder.create();
        b.addAll(new RequestDefaultHeaders(null), new RequestContent(), new RequestTargetHost(),
                //                    new RequestClientConnControl(),
                new RequestUserAgent(userAgentCopy), new RequestExpectContinue());
        b.add(new RequestAddCookies());
        b.add(new RequestAcceptEncoding());
        b.add(new RequestAuthCache());
        b.add(new ResponseProcessCookies());
        b.add(new ResponseContentEncoding());
        httpprocessorCopy = b.build();/*from   w  ww .java  2s  . c o m*/
    }

    HttpHost proxy = new HttpHost(ip, port, "http");
    //      DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
    CloseableHttpClient httpclient = HttpClients.custom()
            //              .setRoutePlanner(routePlanner)
            .setProxy(proxy).setHttpProcessor(httpprocessorCopy).build();

    String r = "";
    HttpGet httpGet = new HttpGet(url);

    //      httpGet.setHeader("GET http://www.stilllistener.com/checkpoint1/test2/ HTTP/1.1","");
    //      httpGet.setHeader("Host","www.stilllistener.com");
    //      httpGet.setHeader("User-Agent","Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0");
    //      httpGet.setHeader("User-Agent","Baiduspider+(+http://www.baidu.com/search/spider.htm)");
    //      httpGet.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    //      httpGet.setHeader("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
    //      httpGet.setHeader("Accept-Encoding","gzip, deflate");
    //      httpGet.setHeader("Referer",url);
    //      httpGet.setHeader("Connection","keep-alive");
    //      httpGet.setHeader("Cache-Control","max-age=0");

    CloseableHttpResponse response1 = null;
    try {
        response1 = httpclient.execute(httpGet);
        HttpEntity entity1 = response1.getEntity();
        String line = response1.getStatusLine().getReasonPhrase();
        //         System.out.println(line);
        if (line.equals("OK")) {
            r = EntityUtils.toString(entity1);
            //            System.out.println(r);
            // do something useful with the response body
            // and ensure it is fully consumed
            //             InputStream in = entity1.getContent();
            //             System.out.println(EntityUtils.toString(entity1,"GB2312"));
            //System.out.println(in.toString());
        }
        // do something useful with the response body
        // and ensure it is fully consumed
        //          InputStream in = entity1.getContent();
        //          System.out.println(EntityUtils.toString(entity1,"GB2312"));
        //System.out.println(in.toString());

        //System.out.println(EntityUtils.toString(entity1));
        EntityUtils.consume(entity1);
        response1.close();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return r;
}

From source file:jetbrains.teamcilty.github.api.impl.HttpClientWrapperImpl.java

public HttpClientWrapperImpl()
        throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
    final String serverVersion = ServerVersionHolder.getVersion().getDisplayVersion();

    final HttpParams ps = new BasicHttpParams();

    DefaultHttpClient.setDefaultHttpParams(ps);
    final int timeout = TeamCityProperties.getInteger("teamcity.github.http.timeout", 300 * 1000);
    HttpConnectionParams.setConnectionTimeout(ps, timeout);
    HttpConnectionParams.setSoTimeout(ps, timeout);
    HttpProtocolParams.setUserAgent(ps, "JetBrains TeamCity " + serverVersion);

    final SchemeRegistry schemaRegistry = SchemeRegistryFactory.createDefault();
    final SSLSocketFactory sslSocketFactory = new SSLSocketFactory(new TrustStrategy() {
        public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            return !TeamCityProperties.getBoolean("teamcity.github.verify.ssl.certificate");
        }//from  w  ww  .ja  va2s  .  c  om
    });
    schemaRegistry.register(new Scheme("https", 443, sslSocketFactory));

    final DefaultHttpClient httpclient = new DefaultHttpClient(new ThreadSafeClientConnManager(schemaRegistry),
            ps);

    setupProxy(httpclient);

    httpclient.setRoutePlanner(new ProxySelectorRoutePlanner(
            httpclient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault()));
    httpclient.addRequestInterceptor(new RequestAcceptEncoding());
    httpclient.addResponseInterceptor(new ResponseContentEncoding());
    httpclient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(3, true));

    myClient = httpclient;
}

From source file:com.soulgalore.crawler.guice.HttpClientProvider.java

/**
 * Get the client.//from w  ww . ja  v  a 2  s .  c o m
 * 
 * @return the client
 */
public HttpClient get() {
    final ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager();
    cm.setMaxTotal(nrOfThreads);
    cm.setDefaultMaxPerRoute(maxToRoute);
    final DefaultHttpClient client = HTTPSFaker.getClientThatAllowAnyHTTPS(cm);

    client.getParams().setParameter("http.socket.timeout", socketTimeout);
    client.getParams().setParameter("http.connection.timeout", connectionTimeout);

    client.addRequestInterceptor(new RequestAcceptEncoding());
    client.addResponseInterceptor(new ResponseContentEncoding());

    CookieSpecFactory csf = new CookieSpecFactory() {
        public CookieSpec newInstance(HttpParams params) {
            return new BestMatchSpecWithURLErrorLog();
        }
    };

    client.getCookieSpecs().register("bestmatchwithurl", csf);
    client.getParams().setParameter(ClientPNames.COOKIE_POLICY, "bestmatchwithurl");

    if (!"".equals(proxy)) {
        StringTokenizer token = new StringTokenizer(proxy, ":");

        if (token.countTokens() == 3) {
            String proxyProtocol = token.nextToken();
            String proxyHost = token.nextToken();
            int proxyPort = Integer.parseInt(token.nextToken());

            HttpHost proxy = new HttpHost(proxyHost, proxyPort, proxyProtocol);
            client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        } else
            System.err.println("Invalid proxy configuration: " + proxy);
    }

    if (auths.size() > 0) {

        for (Auth authObject : auths) {
            client.getCredentialsProvider().setCredentials(
                    new AuthScope(authObject.getScope(), authObject.getPort()),
                    new UsernamePasswordCredentials(authObject.getUserName(), authObject.getPassword()));
        }
    }

    return client;
}

From source file:com.google.pubsub.clients.common.MetricsHandler.java

private void initialize() {
    synchronized (this) {
        try {/*from   www  . j a v a 2  s.com*/
            HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
            JsonFactory jsonFactory = new JacksonFactory();
            GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
            if (credential.createScopedRequired()) {
                credential = credential.createScoped(
                        Collections.singletonList("https://www.googleapis.com/auth/cloud-platform"));
            }
            monitoring = new Monitoring.Builder(transport, jsonFactory, credential)
                    .setApplicationName("Cloud Pub/Sub Loadtest Framework").build();
            String zoneId;
            String instanceId;
            try {
                DefaultHttpClient httpClient = new DefaultHttpClient();
                httpClient.addRequestInterceptor(new RequestAcceptEncoding());
                httpClient.addResponseInterceptor(new ResponseContentEncoding());

                HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 30000);
                HttpConnectionParams.setSoTimeout(httpClient.getParams(), 30000);
                HttpConnectionParams.setSoKeepalive(httpClient.getParams(), true);
                HttpConnectionParams.setStaleCheckingEnabled(httpClient.getParams(), false);
                HttpConnectionParams.setTcpNoDelay(httpClient.getParams(), true);

                SchemeRegistry schemeRegistry = httpClient.getConnectionManager().getSchemeRegistry();
                schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
                schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
                httpClient.setKeepAliveStrategy((response, ctx) -> 30);
                HttpGet zoneIdRequest = new HttpGet(
                        "http://metadata.google.internal/computeMetadata/v1/instance/zone");
                zoneIdRequest.setHeader("Metadata-Flavor", "Google");
                HttpResponse zoneIdResponse = httpClient.execute(zoneIdRequest);
                String tempZoneId = EntityUtils.toString(zoneIdResponse.getEntity());
                if (tempZoneId.lastIndexOf("/") >= 0) {
                    zoneId = tempZoneId.substring(tempZoneId.lastIndexOf("/") + 1);
                } else {
                    zoneId = tempZoneId;
                }
                HttpGet instanceIdRequest = new HttpGet(
                        "http://metadata.google.internal/computeMetadata/v1/instance/id");
                instanceIdRequest.setHeader("Metadata-Flavor", "Google");
                HttpResponse instanceIdResponse = httpClient.execute(instanceIdRequest);
                instanceId = EntityUtils.toString(instanceIdResponse.getEntity());
            } catch (IOException e) {
                log.info("Unable to connect to metadata server, assuming not on GCE, setting "
                        + "defaults for instance and zone.");
                instanceId = "local";
                zoneId = "us-east1-b"; // Must use a valid cloud zone even if running local.
            }

            monitoredResource.setLabels(
                    ImmutableMap.of("project_id", project, "instance_id", instanceId, "zone", zoneId));
            createMetrics();
        } catch (IOException e) {
            log.error("Unable to initialize MetricsHandler, trying again.", e);
            executor.execute(this::initialize);
        } catch (GeneralSecurityException e) {
            log.error("Unable to initialize MetricsHandler permanently, credentials error.", e);
        }
    }
}

From source file:jetbrains.buildServer.commitPublisher.github.api.impl.HttpClientWrapperImpl.java

public HttpClientWrapperImpl()
        throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
    final String serverVersion = ServerVersionHolder.getVersion().getDisplayVersion();

    final HttpParams ps = new BasicHttpParams();

    DefaultHttpClient.setDefaultHttpParams(ps);
    final int timeout = TeamCityProperties.getInteger("teamcity.github.http.timeout", 300 * 1000);
    HttpConnectionParams.setConnectionTimeout(ps, timeout);
    HttpConnectionParams.setSoTimeout(ps, timeout);
    HttpProtocolParams.setUserAgent(ps, "JetBrains TeamCity " + serverVersion);

    final SchemeRegistry schemaRegistry = SchemeRegistryFactory.createDefault();
    final SSLSocketFactory sslSocketFactory = new SSLSocketFactory(new TrustStrategy() {
        public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            return !TeamCityProperties.getBoolean("teamcity.github.verify.ssl.certificate");
        }/*  w w  w  .  ja v a  2s  . c om*/
    }) {
        @Override
        public Socket connectSocket(int connectTimeout, Socket socket, HttpHost host,
                InetSocketAddress remoteAddress, InetSocketAddress localAddress, HttpContext context)
                throws IOException {
            if (socket instanceof SSLSocket) {
                try {
                    PropertyUtils.setProperty(socket, "host", host.getHostName());
                } catch (Exception ex) {
                    LOG.warn(String.format(
                            "A host name is not passed to SSL connection for the purpose of supporting SNI due to the following exception: %s",
                            ex.toString()));
                }
            }
            return super.connectSocket(connectTimeout, socket, host, remoteAddress, localAddress, context);
        }
    };
    schemaRegistry.register(new Scheme("https", 443, sslSocketFactory));

    final DefaultHttpClient httpclient = new DefaultHttpClient(new ThreadSafeClientConnManager(schemaRegistry),
            ps);

    setupProxy(httpclient);

    httpclient.setRoutePlanner(new ProxySelectorRoutePlanner(
            httpclient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault()));
    httpclient.addRequestInterceptor(new RequestAcceptEncoding());
    httpclient.addResponseInterceptor(new ResponseContentEncoding());
    httpclient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(3, true));

    myClient = httpclient;
}