Search in sources :

Example 41 with HttpClientContext

use of org.apache.http.client.protocol.HttpClientContext 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

HttpClientContext (org.apache.http.client.protocol.HttpClientContext)41 IOException (java.io.IOException)16 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)16 HttpHost (org.apache.http.HttpHost)14 AuthScope (org.apache.http.auth.AuthScope)14 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)14 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)13 CredentialsProvider (org.apache.http.client.CredentialsProvider)11 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)11 HttpGet (org.apache.http.client.methods.HttpGet)10 BasicScheme (org.apache.http.impl.auth.BasicScheme)10 HttpResponse (org.apache.http.HttpResponse)9 AuthCache (org.apache.http.client.AuthCache)9 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)9 Header (org.apache.http.Header)8 URI (java.net.URI)6 HttpEntity (org.apache.http.HttpEntity)6 HttpContext (org.apache.http.protocol.HttpContext)4 SocketTimeoutException (java.net.SocketTimeoutException)3 URISyntaxException (java.net.URISyntaxException)3