Example usage for org.apache.http.protocol HttpProcessorBuilder add

List of usage examples for org.apache.http.protocol HttpProcessorBuilder add

Introduction

In this page you can find the example usage for org.apache.http.protocol HttpProcessorBuilder add.

Prototype

public HttpProcessorBuilder add(HttpResponseInterceptor httpResponseInterceptor) 

Source Link

Usage

From source file:org.Cherry.Modules.Web.Engine.WebEngine.java

private HttpProcessor getHttpProcessor() {
    if (null == _httpProcessor) {
        final HttpProcessorBuilder httpProcessorBuilder = HttpProcessorBuilder.create();

        httpProcessorBuilder.add(new ResponseDate());
        httpProcessorBuilder.add(new ResponseServer(getOriginServer()));
        httpProcessorBuilder.add(new ResponseContent());
        httpProcessorBuilder.add(new ResponseConnControl());
        httpProcessorBuilder.add(getRequestInterceptorService());
        httpProcessorBuilder.add(getResponseInterceptorService());

        _httpProcessor = httpProcessorBuilder.build();
    }//www  .  ja v  a2s  .  c  o  m

    return _httpProcessor;
}

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  w  w  . j a v  a  2 s  .  c om*/
    }

    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:com.gargoylesoftware.htmlunit.HttpWebConnection.java

private void configureHttpProcessorBuilder(final HttpClientBuilder builder, final WebRequest webRequest)
        throws IOException {
    final HttpProcessorBuilder b = HttpProcessorBuilder.create();
    for (final HttpRequestInterceptor i : getHttpRequestInterceptors(webRequest)) {
        b.add(i);
    }/*from ww  w . j  av  a 2  s .  c om*/

    // These are the headers used in HttpClientBuilder, excluding the already added ones
    // (RequestClientConnControl and RequestAddCookies)
    b.addAll(new RequestDefaultHeaders(null), new RequestContent(), new RequestTargetHost(),
            new RequestExpectContinue());
    b.add(new RequestAcceptEncoding());
    b.add(new RequestAuthCache());
    b.add(new ResponseProcessCookies());
    b.add(new ResponseContentEncoding());
    builder.setHttpProcessor(b.build());
}