Search in sources :

Example 11 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project tutorials by eugenp.

the class HttpClientCookieLiveTest method whenSettingCookiesOnTheHttpClient_thenCookieSentCorrectly.

@Test
public final void whenSettingCookiesOnTheHttpClient_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().setDefaultCookieStore(cookieStore).build();
    final HttpGet request = new HttpGet("http://www.github.com");
    response = instance.execute(request);
    assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}
Also used : BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) HttpGet(org.apache.http.client.methods.HttpGet) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) Test(org.junit.Test)

Example 12 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie 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 13 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project cheetah by togethwy.

the class HttpClientHelper method getDefaultClient.

public static CloseableHttpClient getDefaultClient(SiteConfig siteConfig) {
    HttpClientBuilder httpClientBuilder = HttpClients.custom();
    httpClientBuilder.setUserAgent(siteConfig.getUserAgent()).setRedirectStrategy(new DefaultRedirectStrategy()).setDefaultHeaders(convertMap2List(siteConfig.getHeaders()));
    CookieStore cookieStore = new BasicCookieStore();
    Map<String, String> cookieMap = siteConfig.getCookies();
    String domain = siteConfig.getDomain();
    cookieMap.forEach((name, value) -> {
        BasicClientCookie cookie = new BasicClientCookie(name, value);
        cookie.setDomain(domain);
        cookie.setPath("/");
        cookieStore.addCookie(cookie);
    });
    httpClientBuilder.setDefaultCookieStore(cookieStore);
    return httpClientBuilder.build();
}
Also used : CookieStore(org.apache.http.client.CookieStore) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie)

Example 14 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project k-9 by k9mail.

the class WebDavStoreTest method createOkResponseWithCookie.

private Answer<HttpResponse> createOkResponseWithCookie() {
    return new Answer<HttpResponse>() {

        @Override
        public HttpResponse answer(InvocationOnMock invocation) {
            HttpContext context = (HttpContext) invocation.getArguments()[1];
            if (context.getAttribute(ClientContext.COOKIE_STORE) != null) {
                BasicCookieStore cookieStore = (BasicCookieStore) context.getAttribute(ClientContext.COOKIE_STORE);
                BasicClientCookie cookie = new BasicClientCookie("cookie", "meLikeCookie");
                cookieStore.addCookie(cookie);
            }
            return OK_200_RESPONSE;
        }
    };
}
Also used : Answer(org.mockito.stubbing.Answer) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpContext(org.apache.http.protocol.HttpContext) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie)

Example 15 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project SeaStar by 13120241790.

the class SyncHttpClient method sendRequest.

/**
 * Puts a new request in queue as a new thread in pool to be executed
 *
 * @param client
 * 				HttpClient to be used for request, can differ in single requests
 * @param httpContext
 * 				HttpContext in which the request will be executed
 * @param uriRequest
 * 				instance of HttpUriRequest, which means it must be of
 *            HttpDelete, HttpPost, HttpGet, HttpPut, etc.
 * @param contentType
 * 				MIME body type, for POST and PUT requests, may be null
 * @param context
 * 				 Context of Android application
 * @return
 * @throws HttpException
 */
protected String sendRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, String contentType, Context context) throws HttpException {
    String responseBody = "";
    if (contentType != null) {
        uriRequest.addHeader("Content-Type", contentType);
    }
    // set cookie
    List<Cookie> list = cookieStore.getCookies();
    if (list != null && list.size() > 0) {
        for (Cookie cookie : list) {
            uriRequest.setHeader("Cookie", cookie.getValue());
        }
    }
    try {
        // get the response from assets
        URI uri = uriRequest.getURI();
        NLog.e(tag, "url : " + uri.toString());
        String scheme = uri.getScheme();
        if (!TextUtils.isEmpty(scheme) && ASSETS_PATH.equals(scheme)) {
            String fileName = uri.getAuthority();
            InputStream intput = context.getAssets().open(fileName);
            responseBody = inputSteamToString(intput);
            NLog.e(tag, "responseBody : " + responseBody);
            return responseBody;
        }
        // get the response from network
        HttpEntity bufferEntity = null;
        HttpResponse response = client.execute(uriRequest, httpContext);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            bufferEntity = new BufferedHttpEntity(entity);
            responseBody = EntityUtils.toString(bufferEntity, ENCODE_UTF8);
            // if(NLog.isDebug()){
            // String key = uriRequest.getURI().toString().replace("/", "");
            // CacheManager.saveTestData(responseBody, key);
            // }
            NLog.e(tag, "responseBody : " + responseBody);
        }
        // get cookie to save local
        Header[] headers = response.getHeaders("Set-Cookie");
        if (headers != null && headers.length > 0) {
            for (int i = 0; i < headers.length; i++) {
                String cookie = headers[i].getValue();
                BasicClientCookie newCookie = new BasicClientCookie("cookie" + i, cookie);
                cookieStore.addCookie(newCookie);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new HttpException(e);
    }
    return responseBody;
}
Also used : Cookie(org.apache.http.cookie.Cookie) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) HttpResponse(org.apache.http.HttpResponse) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) URI(java.net.URI) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) Header(org.apache.http.Header)

Aggregations

BasicClientCookie (org.apache.http.impl.cookie.BasicClientCookie)88 BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)36 Test (org.junit.Test)24 HttpResponse (org.apache.http.HttpResponse)19 Cookie (org.apache.http.cookie.Cookie)17 HttpClient (org.apache.http.client.HttpClient)15 HttpGet (org.apache.http.client.methods.HttpGet)15 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)15 Map (java.util.Map)14 RequestConfig (org.apache.http.client.config.RequestConfig)13 HttpPost (org.apache.http.client.methods.HttpPost)9 CookieAttributeHandler (org.apache.http.cookie.CookieAttributeHandler)9 CookieOrigin (org.apache.http.cookie.CookieOrigin)9 URL (java.net.URL)8 HttpEntity (org.apache.http.HttpEntity)8 CookieStore (org.apache.http.client.CookieStore)8 IOException (java.io.IOException)7 Date (java.util.Date)7 ClientCookie (org.apache.http.cookie.ClientCookie)7 HashMap (java.util.HashMap)6