Search in sources :

Example 11 with BasicCookieStore

use of org.apache.http.impl.client.BasicCookieStore in project android-player-samples by BrightcoveOS.

the class MainActivity method httpGet.

public String httpGet(String url) {
    String domain = getResources().getString(R.string.ais_domain);
    String result = "";
    CookieStore cookieStore = new BasicCookieStore();
    BasicHttpContext localContext = new BasicHttpContext();
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    // If we have a cookie stored, parse and use it. Otherwise, use a default http client.
    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        if (!authorizationCookie.equals("")) {
            String[] cookies = authorizationCookie.split(";");
            for (int i = 0; i < cookies.length; i++) {
                String[] kvp = cookies[i].split("=");
                if (kvp.length != 2) {
                    throw new Exception("Illegal cookie: missing key/value pair.");
                }
                BasicClientCookie c = new BasicClientCookie(kvp[0], kvp[1]);
                c.setDomain(domain);
                cookieStore.addCookie(c);
            }
        }
        HttpResponse httpResponse = httpClient.execute(httpGet, localContext);
        result = EntityUtils.toString(httpResponse.getEntity());
    } catch (Exception e) {
        Log.e(TAG, e.getLocalizedMessage());
    }
    return result;
}
Also used : CookieStore(org.apache.http.client.CookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 12 with BasicCookieStore

use of org.apache.http.impl.client.BasicCookieStore in project redisson by redisson.

the class RedissonSessionManagerTest method testSwitchServer.

@Test
public void testSwitchServer() throws Exception {
    // start the server at http://localhost:8080/myapp
    TomcatServer server = new TomcatServer("myapp", 8080, "src/test/");
    server.start();
    Executor executor = Executor.newInstance();
    BasicCookieStore cookieStore = new BasicCookieStore();
    executor.use(cookieStore);
    write(executor, "test", "1234");
    Cookie cookie = cookieStore.getCookies().get(0);
    Executor.closeIdleConnections();
    server.stop();
    server = new TomcatServer("myapp", 8080, "src/test/");
    server.start();
    executor = Executor.newInstance();
    cookieStore = new BasicCookieStore();
    cookieStore.addCookie(cookie);
    executor.use(cookieStore);
    read(executor, "test", "1234");
    remove(executor, "test", "null");
    Executor.closeIdleConnections();
    server.stop();
}
Also used : Cookie(org.apache.http.cookie.Cookie) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) Executor(org.apache.http.client.fluent.Executor) Test(org.junit.Test)

Example 13 with BasicCookieStore

use of org.apache.http.impl.client.BasicCookieStore in project ats-framework by Axway.

the class HttpClient method addCookie.

/**
     * Add Cookie
     *
     * @param name cookie name
     * @param value cookie value
     * @param domain cookie domain
     * @param isSecure whether the cookie is secure or not
     * @param expirationDate cookie expiration date
     * @param path cookie path
     */
public void addCookie(String name, String value, String domain, String path, Date expirationDate, boolean isSecure) {
    if (httpContext == null) {
        httpContext = new BasicHttpContext();
    }
    BasicCookieStore cookieStore = (BasicCookieStore) httpContext.getAttribute(HttpClientContext.COOKIE_STORE);
    if (cookieStore == null) {
        cookieStore = new BasicCookieStore();
        httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
    }
    BasicClientCookie cookie = new BasicClientCookie(name, value);
    cookie.setDomain(domain);
    cookie.setPath(path);
    cookie.setExpiryDate(expirationDate);
    cookie.setSecure(isSecure);
    cookieStore.addCookie(cookie);
}
Also used : BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie)

Example 14 with BasicCookieStore

use of org.apache.http.impl.client.BasicCookieStore in project ats-framework by Axway.

the class HttpClient method removeCookie.

/**
     * Remove a Cookie by name and path
     *
     * @param name cookie name
     * @param path cookie path
     */
public void removeCookie(String name, String path) {
    if (httpContext != null) {
        BasicCookieStore cookieStore = (BasicCookieStore) httpContext.getAttribute(HttpClientContext.COOKIE_STORE);
        if (cookieStore != null) {
            List<Cookie> cookies = cookieStore.getCookies();
            cookieStore.clear();
            for (Cookie cookie : cookies) {
                if (!cookie.getName().equals(name) || !cookie.getPath().equals(path)) {
                    cookieStore.addCookie(cookie);
                }
            }
        }
    }
}
Also used : Cookie(org.apache.http.cookie.Cookie) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore)

Example 15 with BasicCookieStore

use of org.apache.http.impl.client.BasicCookieStore in project aries by apache.

the class HttpTestCase method testSessionBean.

public void testSessionBean() throws Exception {
    Bundle tb5Bundle = installBundle("tb6.jar");
    try {
        String path = "/foo";
        RequestInfoDTO requestInfoDTO = waitFor(path);
        assertEquals("foo", requestInfoDTO.servletDTO.name);
        HttpClientBuilder clientBuilder = hcbf.newBuilder();
        CloseableHttpClient httpclient = clientBuilder.build();
        CookieStore cookieStore = new BasicCookieStore();
        HttpContext httpContext = new BasicHttpContext();
        httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
        URI uri = new URIBuilder(getEndpoint()).setPath(path).setParameter("name", "test").build();
        HttpGet httpget = new HttpGet(uri);
        try (CloseableHttpResponse response = httpclient.execute(httpget, httpContext)) {
            HttpEntity entity = response.getEntity();
            assertEquals("test", read(entity));
        }
        for (int i = 0; i < 10; i++) {
            uri = new URIBuilder(getEndpoint()).setPath(path).build();
            httpget = new HttpGet(uri);
            try (CloseableHttpResponse response = httpclient.execute(httpget, httpContext)) {
                HttpEntity entity = response.getEntity();
                assertEquals("test", read(entity));
            }
        }
        uri = new URIBuilder(getEndpoint()).setPath(path).build();
        httpget = new HttpGet(uri);
        try (CloseableHttpResponse response = httpclient.execute(httpget)) {
            HttpEntity entity = response.getEntity();
            assertEquals("", read(entity));
        }
    } finally {
        tb5Bundle.uninstall();
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) Bundle(org.osgi.framework.Bundle) 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) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) URI(java.net.URI) URIBuilder(org.apache.http.client.utils.URIBuilder) CookieStore(org.apache.http.client.CookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) RequestInfoDTO(org.osgi.service.http.runtime.dto.RequestInfoDTO)

Aggregations

BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)33 CookieStore (org.apache.http.client.CookieStore)12 HttpResponse (org.apache.http.HttpResponse)10 Test (org.junit.Test)10 IOException (java.io.IOException)9 HttpGet (org.apache.http.client.methods.HttpGet)9 Cookie (org.apache.http.cookie.Cookie)9 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)7 Header (org.apache.http.Header)6 ClientProtocolException (org.apache.http.client.ClientProtocolException)6 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)6 HttpContext (org.apache.http.protocol.HttpContext)6 TestHttpClient (io.undertow.testutils.TestHttpClient)5 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)5 HttpString (io.undertow.util.HttpString)4 URL (java.net.URL)4 HttpEntity (org.apache.http.HttpEntity)4 HttpClient (org.apache.http.client.HttpClient)4 Executor (org.apache.http.client.fluent.Executor)4 BasicClientCookie (org.apache.http.impl.cookie.BasicClientCookie)4