Search in sources :

Example 81 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project lucene-solr by apache.

the class BasicHttpSolrClientTest method testCompression.

@Test
public void testCompression() throws Exception {
    SolrQuery q = new SolrQuery("*:*");
    final String clientUrl = jetty.getBaseUrl().toString() + "/debug/foo";
    try (HttpSolrClient client = getHttpSolrClient(clientUrl)) {
        // verify request header gets set
        DebugServlet.clear();
        try {
            client.query(q);
        } catch (ParseException ignored) {
        }
        assertNull(DebugServlet.headers.toString(), DebugServlet.headers.get("Accept-Encoding"));
    }
    try (HttpSolrClient client = getHttpSolrClient(clientUrl, null, null, true)) {
        try {
            client.query(q);
        } catch (ParseException ignored) {
        }
        assertNotNull(DebugServlet.headers.get("Accept-Encoding"));
    }
    try (HttpSolrClient client = getHttpSolrClient(clientUrl, null, null, false)) {
        try {
            client.query(q);
        } catch (ParseException ignored) {
        }
    }
    assertNull(DebugServlet.headers.get("Accept-Encoding"));
    // verify server compresses output
    HttpGet get = new HttpGet(jetty.getBaseUrl().toString() + "/collection1" + "/select?q=foo&wt=xml");
    get.setHeader("Accept-Encoding", "gzip");
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set(HttpClientUtil.PROP_ALLOW_COMPRESSION, true);
    RequestConfig config = RequestConfig.custom().setDecompressionEnabled(false).build();
    get.setConfig(config);
    CloseableHttpClient httpclient = HttpClientUtil.createClient(params);
    HttpEntity entity = null;
    try {
        HttpResponse response = httpclient.execute(get, HttpClientUtil.createNewHttpClientRequestContext());
        entity = response.getEntity();
        Header ceheader = entity.getContentEncoding();
        assertNotNull(Arrays.asList(response.getAllHeaders()).toString(), ceheader);
        assertEquals("gzip", ceheader.getValue());
    } finally {
        if (entity != null) {
            entity.getContent().close();
        }
        HttpClientUtil.close(httpclient);
    }
    // verify compressed response can be handled
    try (HttpSolrClient client = getHttpSolrClient(jetty.getBaseUrl().toString() + "/collection1")) {
        q = new SolrQuery("foo");
        QueryResponse response = client.query(q);
        assertEquals(0, response.getStatus());
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) Header(org.apache.http.Header) HttpGet(org.apache.http.client.methods.HttpGet) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) HttpResponse(org.apache.http.HttpResponse) ParseException(org.apache.http.ParseException) SolrQuery(org.apache.solr.client.solrj.SolrQuery) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) Test(org.junit.Test)

Example 82 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project local-data-aragopedia by aragonopendata.

the class Utils method processURLGetApache.

public static void processURLGetApache() {
    CookieStore cookieStore = new BasicCookieStore();
    RequestConfig defaultRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).setExpectContinueEnabled(true).setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST)).setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)).build();
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
    try {
        HttpGet httpget = new HttpGet("http://bi.aragon.es/analytics/saw.dll?Go&path=/shared/IAEST-PUBLICA/Estadistica%20Local/03/030018TP&Action=Download&Options=df&NQUser=granpublico&NQPassword=granpublico");
        httpget.addHeader("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36");
        httpget.addHeader("Cookie", "sawU=granpublico; ORA_BIPS_LBINFO=153c4c924b8; ORA_BIPS_NQID=k8vgekohfuquhdg71on5hjvqbcorcupbmh4h3lu25iepaq5izOr07UFe9WiFvM3; __utma=263932892.849551431.1443517596.1457200753.1458759706.17; __utmc=263932892; __utmz=263932892.1456825145.15.6.utmcsr=alzir.dia.fi.upm.es|utmccn=(referral)|utmcmd=referral|utmcct=/kos/iaest/clase-vivienda-agregado");
        httpget.addHeader("content-type", "text/csv; charset=utf-8");
        RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig).setConnectTimeout(5000).setConnectionRequestTimeout(5000).setCookieSpec(CookieSpecs.DEFAULT).build();
        httpget.setConfig(requestConfig);
        HttpClientContext context = HttpClientContext.create();
        context.setCookieStore(cookieStore);
        System.out.println("executing request " + httpget.getURI());
        CloseableHttpResponse response = httpclient.execute(httpget, context);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            System.out.println(EntityUtils.toString(response.getEntity()));
            System.out.println("----------------------------------------");
            context.getRequest();
            context.getHttpRoute();
            context.getTargetAuthState();
            context.getTargetAuthState();
            context.getCookieOrigin();
            context.getCookieSpec();
            context.getUserToken();
        } finally {
            response.close();
        }
        response = httpclient.execute(httpget, context);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            System.out.println(EntityUtils.toString(response.getEntity()));
            System.out.println("----------------------------------------");
            context.getRequest();
            context.getHttpRoute();
            context.getTargetAuthState();
            context.getTargetAuthState();
            context.getCookieOrigin();
            context.getCookieSpec();
            context.getUserToken();
        } finally {
            response.close();
        }
        httpclient.close();
    } catch (ClientProtocolException e) {
        log.error("Error en el método processURLGetApache", e);
    } catch (IOException e) {
        log.error("Error en el método processURLGetApache", e);
    }
}
Also used : CookieStore(org.apache.http.client.CookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Aggregations

RequestConfig (org.apache.http.client.config.RequestConfig)82 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)28 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)27 HttpGet (org.apache.http.client.methods.HttpGet)26 HttpPost (org.apache.http.client.methods.HttpPost)18 Test (org.junit.Test)15 WxErrorException (me.chanjar.weixin.common.exception.WxErrorException)13 StringEntity (org.apache.http.entity.StringEntity)13 IOException (java.io.IOException)12 WxError (me.chanjar.weixin.common.bean.result.WxError)11 URI (java.net.URI)9 HttpEntity (org.apache.http.HttpEntity)9 HttpResponse (org.apache.http.HttpResponse)9 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)9 InputStream (java.io.InputStream)8 HttpHost (org.apache.http.HttpHost)8 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)8 Configurable (org.apache.http.client.methods.Configurable)7 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)7 HttpClient (org.apache.http.client.HttpClient)6