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

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

Introduction

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

Prototype

public RequestDefaultHeaders(final Collection<? extends Header> defaultHeaders) 

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();//from w  w  w.  jav  a 2  s  . 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:org.apache.hive.service.cli.thrift.TestThriftHttpCLIServiceFeatures.java

private void verifyForwardedHeaders(ArrayList<String> headerIPs, String cmd) throws Exception {
    TTransport transport;/*  w  w w.j a v  a 2 s  . c  o  m*/
    DefaultHttpClient hClient = new DefaultHttpClient();
    String httpUrl = getHttpUrl();

    // add an interceptor that adds the X-Forwarded-For header with given ips
    if (!headerIPs.isEmpty()) {
        Header xForwardHeader = new BasicHeader("X-Forwarded-For", Joiner.on(",").join(headerIPs));
        RequestDefaultHeaders headerInterceptor = new RequestDefaultHeaders(Arrays.asList(xForwardHeader));
        hClient.addRequestInterceptor(headerInterceptor);
    }

    // interceptor for adding username, pwd
    HttpBasicAuthInterceptor authInt = new HttpBasicAuthInterceptor(ThriftCLIServiceTest.USERNAME,
            ThriftCLIServiceTest.PASSWORD, null, null, false, null);
    hClient.addRequestInterceptor(authInt);

    transport = new THttpClient(httpUrl, hClient);
    TCLIService.Client httpClient = getClient(transport);

    // Create a new open session request object
    TOpenSessionReq openReq = new TOpenSessionReq();
    TOpenSessionResp openResp = httpClient.OpenSession(openReq);

    //execute a query
    TExecuteStatementReq execReq = new TExecuteStatementReq(openResp.getSessionHandle(), "show tables");
    httpClient.ExecuteStatement(execReq);

    // capture arguments to authorizer impl call and verify ip addresses passed
    ArgumentCaptor<HiveAuthzContext> contextCapturer = ArgumentCaptor.forClass(HiveAuthzContext.class);

    verify(mockedAuthorizer).checkPrivileges(any(HiveOperationType.class),
            Matchers.anyListOf(HivePrivilegeObject.class), Matchers.anyListOf(HivePrivilegeObject.class),
            contextCapturer.capture());

    HiveAuthzContext context = contextCapturer.getValue();
    System.err.println("Forwarded IP Addresses " + context.getForwardedAddresses());

    List<String> auditIPAddresses = new ArrayList<String>(context.getForwardedAddresses());
    Collections.sort(auditIPAddresses);
    Collections.sort(headerIPs);

    Assert.assertEquals("Checking forwarded IP Address", headerIPs, auditIPAddresses);
}

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  w w  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());
}