Search in sources :

Example 11 with BasicHttpContext

use of org.apache.http.protocol.BasicHttpContext in project MtgDesktopCompanion by nicho92.

the class TappedOutDeckSniffer method connect.

@Override
public void connect() throws IOException {
    cookieStore = new BasicCookieStore();
    httpContext = new BasicHttpContext();
    httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
    httpclient = HttpClients.custom().setUserAgent(getString("USER_AGENT")).setRedirectStrategy(new LaxRedirectStrategy()).build();
    httpclient.execute(new HttpGet("https://tappedout.net/accounts/login/?next=/"), httpContext);
    HttpPost login = new HttpPost("https://tappedout.net/accounts/login/");
    List<NameValuePair> nvps = new ArrayList<>();
    nvps.add(new BasicNameValuePair("next", "/"));
    nvps.add(new BasicNameValuePair("username", getString("LOGIN")));
    nvps.add(new BasicNameValuePair("PASS", getString("PASS")));
    nvps.add(new BasicNameValuePair("csrfmiddlewaretoken", getCookieValue("csrftoken")));
    login.setEntity(new UrlEncodedFormEntity(nvps));
    login.addHeader("Referer", "https://tappedout.net/accounts/login/?next=/");
    login.addHeader("Upgrade-Insecure-Requests", "1");
    login.addHeader("Origin", "https://tappedout.net");
    HttpResponse resp = httpclient.execute(login, httpContext);
    logger.debug("Connection OK : " + getString("LOGIN") + " " + resp.getStatusLine().getStatusCode());
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpGet(org.apache.http.client.methods.HttpGet) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) LaxRedirectStrategy(org.apache.http.impl.client.LaxRedirectStrategy)

Example 12 with BasicHttpContext

use of org.apache.http.protocol.BasicHttpContext in project wildfly-camel by wildfly-extras.

the class Olingo4IntegrationTest method getRealServiceUrl.

/*
     * Every request to the demo OData 4.0
     * (http://services.odata.org/TripPinRESTierService) generates unique
     * service URL with postfix like (S(tuivu3up5ygvjzo5fszvnwfv)) for each
     * session This method makes reuest to the base URL and return URL with
     * generated postfix
     */
private String getRealServiceUrl(String baseUrl) throws ClientProtocolException, IOException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet(baseUrl);
    HttpContext httpContext = new BasicHttpContext();
    httpclient.execute(httpGet, httpContext);
    HttpUriRequest currentReq = (HttpUriRequest) httpContext.getAttribute(HttpCoreContext.HTTP_REQUEST);
    HttpHost currentHost = (HttpHost) httpContext.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
    String currentUrl = (currentReq.getURI().isAbsolute()) ? currentReq.getURI().toString() : (currentHost.toURI() + currentReq.getURI());
    return currentUrl;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpHost(org.apache.http.HttpHost) HttpGet(org.apache.http.client.methods.HttpGet) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext)

Example 13 with BasicHttpContext

use of org.apache.http.protocol.BasicHttpContext in project androidquery by androidquery.

the class AbstractAjaxCallback method httpDo.

private void httpDo(HttpUriRequest hr, String url, AjaxStatus status) throws ClientProtocolException, IOException {
    DefaultHttpClient client = getClient();
    if (proxyHandle != null) {
        proxyHandle.applyProxy(this, hr, client);
    }
    if (AGENT != null) {
        hr.addHeader("User-Agent", AGENT);
    } else if (AGENT == null && GZIP) {
        hr.addHeader("User-Agent", "gzip");
    }
    if (headers != null) {
        for (String name : headers.keySet()) {
            hr.addHeader(name, headers.get(name));
        }
    }
    if (GZIP && (headers == null || !headers.containsKey("Accept-Encoding"))) {
        hr.addHeader("Accept-Encoding", "gzip");
    }
    if (ah != null) {
        ah.applyToken(this, hr);
    }
    String cookie = makeCookie();
    if (cookie != null) {
        hr.addHeader("Cookie", cookie);
    }
    HttpParams hp = hr.getParams();
    if (proxy != null)
        hp.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    if (timeout > 0) {
        hp.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
        hp.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
    }
    if (!redirect) {
        hp.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);
    }
    HttpContext context = new BasicHttpContext();
    CookieStore cookieStore = new BasicCookieStore();
    context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    request = hr;
    if (abort) {
        throw new IOException("Aborted");
    }
    if (SIMULATE_ERROR) {
        throw new IOException("Simulated Error");
    }
    HttpResponse response = null;
    try {
        // response = client.execute(hr, context);
        response = execute(hr, client, context);
    } catch (HttpHostConnectException e) {
        // if proxy is used, automatically retry without proxy
        if (proxy != null) {
            AQUtility.debug("proxy failed, retrying without proxy");
            hp.setParameter(ConnRoutePNames.DEFAULT_PROXY, null);
            // response = client.execute(hr, context);
            response = execute(hr, client, context);
        } else {
            throw e;
        }
    }
    byte[] data = null;
    String redirect = url;
    int code = response.getStatusLine().getStatusCode();
    String message = response.getStatusLine().getReasonPhrase();
    String error = null;
    HttpEntity entity = response.getEntity();
    File file = null;
    File tempFile = null;
    if (code < 200 || code >= 300) {
        InputStream is = null;
        try {
            if (entity != null) {
                is = entity.getContent();
                byte[] s = toData(getEncoding(entity), is);
                error = new String(s, "UTF-8");
                AQUtility.debug("error", error);
            }
        } catch (Exception e) {
            AQUtility.debug(e);
        } finally {
            AQUtility.close(is);
        }
    } else {
        HttpHost currentHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        redirect = currentHost.toURI() + currentReq.getURI();
        int size = Math.max(32, Math.min(1024 * 64, (int) entity.getContentLength()));
        OutputStream os = null;
        InputStream is = null;
        try {
            file = getPreFile();
            if (file == null) {
                os = new PredefinedBAOS(size);
            } else {
                // file.createNewFile();
                tempFile = makeTempFile(file);
                os = new BufferedOutputStream(new FileOutputStream(tempFile));
            }
            is = entity.getContent();
            boolean gzip = "gzip".equalsIgnoreCase(getEncoding(entity));
            if (gzip) {
                is = new GZIPInputStream(is);
            }
            int contentLength = (int) entity.getContentLength();
            // AQUtility.debug("gzip response", entity.getContentEncoding());
            copy(is, os, contentLength, tempFile, file);
            if (file == null) {
                data = ((PredefinedBAOS) os).toByteArray();
            } else {
                if (!file.exists() || file.length() == 0) {
                    file = null;
                }
            }
        } finally {
            AQUtility.close(is);
            AQUtility.close(os);
        }
    }
    AQUtility.debug("response", code);
    if (data != null) {
        AQUtility.debug(data.length, url);
    }
    status.code(code).message(message).error(error).redirect(redirect).time(new Date()).data(data).file(file).client(client).context(context).headers(response.getAllHeaders());
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpEntity(org.apache.http.HttpEntity) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) DataOutputStream(java.io.DataOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) GZIPInputStream(java.util.zip.GZIPInputStream) HttpHost(org.apache.http.HttpHost) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) PredefinedBAOS(com.androidquery.util.PredefinedBAOS) BufferedOutputStream(java.io.BufferedOutputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) Date(java.util.Date) CookieStore(org.apache.http.client.CookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 14 with BasicHttpContext

use of org.apache.http.protocol.BasicHttpContext in project tutorials by eugenp.

the class HttpClientCookieLiveTest method whenSettingCookiesOnTheRequest_thenCookieSentCorrectly.

@Test
public final void whenSettingCookiesOnTheRequest_thenCookieSentCorrectly() throws IOException {
    final BasicCookieStore cookieStore = new BasicCookieStore();
    final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
    cookie.setDomain(".github.com");
    cookie.setPath("/");
    cookieStore.addCookie(cookie);
    instance = HttpClientBuilder.create().build();
    final HttpGet request = new HttpGet("http://www.github.com");
    final HttpContext localContext = new BasicHttpContext();
    localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
    // localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); // before 4.3
    response = instance.execute(request, localContext);
    assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}
Also used : BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpGet(org.apache.http.client.methods.HttpGet) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) Test(org.junit.Test)

Example 15 with BasicHttpContext

use of org.apache.http.protocol.BasicHttpContext in project tutorials by eugenp.

the class HttpComponentsClientHttpRequestFactoryBasicAuth method createHttpContext.

private HttpContext createHttpContext() {
    AuthCache authCache = new BasicAuthCache();
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(host, basicAuth);
    BasicHttpContext localcontext = new BasicHttpContext();
    localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
    return localcontext;
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache)

Aggregations

BasicHttpContext (org.apache.http.protocol.BasicHttpContext)60 HttpContext (org.apache.http.protocol.HttpContext)37 IOException (java.io.IOException)24 HttpResponse (org.apache.http.HttpResponse)21 HttpGet (org.apache.http.client.methods.HttpGet)19 BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)14 HttpHost (org.apache.http.HttpHost)11 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)11 HttpEntity (org.apache.http.HttpEntity)10 HttpPost (org.apache.http.client.methods.HttpPost)10 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)10 Header (org.apache.http.Header)8 BasicScheme (org.apache.http.impl.auth.BasicScheme)8 InputStream (java.io.InputStream)7 AuthScope (org.apache.http.auth.AuthScope)7 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)7 URI (java.net.URI)6 ArrayList (java.util.ArrayList)6 GZIPInputStream (java.util.zip.GZIPInputStream)6 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)6