Example usage for org.apache.http.util VersionInfo getUserAgent

List of usage examples for org.apache.http.util VersionInfo getUserAgent

Introduction

In this page you can find the example usage for org.apache.http.util VersionInfo getUserAgent.

Prototype

public static String getUserAgent(String str, String str2, Class<?> cls) 

Source Link

Usage

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();/*w  ww .  ja v  a2s .com*/
    }

    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:org.apache.http.impl.execchain.MinimalClientExec.java

public MinimalClientExec(final HttpRequestExecutor requestExecutor,
        final HttpClientConnectionManager connManager, final ConnectionReuseStrategy reuseStrategy,
        final ConnectionKeepAliveStrategy keepAliveStrategy) {
    Args.notNull(requestExecutor, "HTTP request executor");
    Args.notNull(connManager, "Client connection manager");
    Args.notNull(reuseStrategy, "Connection reuse strategy");
    Args.notNull(keepAliveStrategy, "Connection keep alive strategy");
    this.httpProcessor = new ImmutableHttpProcessor(new RequestContent(), new RequestTargetHost(),
            new RequestClientConnControl(), new RequestUserAgent(
                    VersionInfo.getUserAgent("Apache-HttpClient", "org.apache.http.client", getClass())));
    this.requestExecutor = requestExecutor;
    this.connManager = connManager;
    this.reuseStrategy = reuseStrategy;
    this.keepAliveStrategy = keepAliveStrategy;
}

From source file:org.apache.taverna.download.impl.DownloadManagerImpl.java

public String getUserAgent() {
    Package pack = getClass().getPackage();
    String httpClientVersion = VersionInfo.getUserAgent("Apache-HttpClient", "org.apache.http.client",
            Request.class);
    return "Apache-Taverna-OSGi" + "/" + pack.getImplementationVersion() + " (" + httpClientVersion + ")";
}