Package org.apache.http.client

Examples of org.apache.http.client.CookieStore


        this.localServer.register("*", new CookieVer1Service());
       
        DefaultHttpClient client = new DefaultHttpClient();
        client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2965);

        CookieStore cookieStore = new BasicCookieStore();
        HttpContext context = client.getDefaultContext();
        context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
       
        HttpGet httpget = new HttpGet("/test/");
       
        RoutedRequest request1 = new RoutedRequest.Impl(httpget, getDefaultRoute());
        HttpResponse response1 = client.execute(request1, context);
        HttpEntity e1 = response1.getEntity();
        if (e1 != null) {
            e1.consumeContent();
        }
       
        Cookie[] cookies = cookieStore.getCookies();
        assertNotNull(cookies);
        assertEquals(2, cookies.length);

        RoutedRequest request2 = new RoutedRequest.Impl(httpget, getDefaultRoute());
        HttpResponse response2 = client.execute(request2, context);
View Full Code Here


        this.localServer.register("*", new CookieVer2Service());
       
        DefaultHttpClient client = new DefaultHttpClient();
        client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2965);

        CookieStore cookieStore = new BasicCookieStore();
        HttpContext context = client.getDefaultContext();
        context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
       
        HttpGet httpget = new HttpGet("/test/");
       
        RoutedRequest request1 = new RoutedRequest.Impl(httpget, getDefaultRoute());
        HttpResponse response1 = client.execute(request1, context);
        HttpEntity e1 = response1.getEntity();
        if (e1 != null) {
            e1.consumeContent();
        }
       
        Cookie[] cookies = cookieStore.getCookies();
        assertNotNull(cookies);
        assertEquals(1, cookies.length);

        RoutedRequest request2 = new RoutedRequest.Impl(httpget, getDefaultRoute());
        HttpResponse response2 = client.execute(request2, context);
View Full Code Here

        this.localServer.register("*", new SetCookieVersionMixService());
       
        DefaultHttpClient client = new DefaultHttpClient();
        client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2965);

        CookieStore cookieStore = new BasicCookieStore();
        HttpContext context = client.getDefaultContext();
        context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
       
        HttpGet httpget = new HttpGet("/test/");
       
        RoutedRequest request1 = new RoutedRequest.Impl(httpget, getDefaultRoute());
        HttpResponse response1 = client.execute(request1, context);
        HttpEntity e1 = response1.getEntity();
        if (e1 != null) {
            e1.consumeContent();
        }
       
        Cookie[] cookies = cookieStore.getCookies();
        assertNotNull(cookies);
        assertEquals(1, cookies.length);
        assertEquals("right", cookies[0].getValue());
        assertTrue(cookies[0] instanceof SetCookie2);
    }
View Full Code Here

        if (context == null) {
            throw new IllegalArgumentException("HTTP context may not be null");
        }
       
        // Obtain cookie store
        CookieStore cookieStore = (CookieStore) context.getAttribute(
                ClientContext.COOKIE_STORE);
        if (cookieStore == null) {
            LOG.info("Cookie store not available in HTTP context");
            return;
        }
       
        // Obtain the registry of cookie specs
        CookieSpecRegistry registry= (CookieSpecRegistry) context.getAttribute(
                ClientContext.COOKIESPEC_REGISTRY);
        if (registry == null) {
            LOG.info("CookieSpec registry not available in HTTP context");
            return;
        }
       
        // Obtain the target host (required)
        HttpHost targetHost = (HttpHost) context.getAttribute(
                ExecutionContext.HTTP_TARGET_HOST);
        if (targetHost == null) {
            throw new IllegalStateException("Target host not specified in HTTP context");
        }
       
        // Obtain the client connection (required)
        ManagedClientConnection conn = (ManagedClientConnection) context.getAttribute(
                ExecutionContext.HTTP_CONNECTION);
        if (conn == null) {
            throw new IllegalStateException("Client connection not specified in HTTP context");
        }

        String policy = HttpClientParams.getCookiePolicy(request.getParams());
        if (LOG.isDebugEnabled()) {
            LOG.debug("CookieSpec selected: " + policy);
        }
       
        URI requestURI;
        if (request instanceof HttpUriRequest) {
            requestURI = ((HttpUriRequest) request).getURI();
        } else {
            try {
                requestURI = new URI(request.getRequestLine().getUri());
            } catch (URISyntaxException ex) {
                throw new ProtocolException("Invalid request URI: " +
                        request.getRequestLine().getUri(), ex);
            }
        }
       
        String hostName = targetHost.getHostName();
        int port = targetHost.getPort();
        if (port < 0) {
            port = conn.getRemotePort();
        }
       
        CookieOrigin cookieOrigin = new CookieOrigin(
                hostName,
                port,
                requestURI.getPath(),
                conn.isSecure());
       
        // Get an instance of the selected cookie policy
        CookieSpec cookieSpec = registry.getCookieSpec(policy, request.getParams());
        // Get all cookies available in the HTTP state
        Cookie[] cookies = cookieStore.getCookies();
        // Find cookies matching the given origin
        List matchedCookies = new ArrayList(cookies.length);
        for (int i = 0; i < cookies.length; i++) {
            Cookie cookie = cookies[i];
            if (cookieSpec.match(cookie, cookieOrigin)) {
View Full Code Here

        if (context == null) {
            throw new IllegalArgumentException("HTTP context may not be null");
        }
       
        // Obtain cookie store
        CookieStore cookieStore = (CookieStore) context.getAttribute(
                ClientContext.COOKIE_STORE);
        if (cookieStore == null) {
            LOG.info("Cookie store not available in HTTP context");
            return;
        }
View Full Code Here

        this.localServer.register("*",
                new BasicRedirectService(host, port));

        DefaultHttpClient client = new DefaultHttpClient();
       
        CookieStore cookieStore = new BasicCookieStore();
        client.setCookieStore(cookieStore);
       
        BasicClientCookie cookie = new BasicClientCookie("name", "value");
        cookie.setDomain("localhost");
        cookie.setPath("/");
       
        cookieStore.addCookie(cookie);

        HttpContext context = client.getDefaultContext();
        HttpGet httpget = new HttpGet("/oldlocation/");

       
View Full Code Here

        this.localServer.register("*", new CookieVer0Service());
       
        DefaultHttpClient client = new DefaultHttpClient();
        client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2965);

        CookieStore cookieStore = new BasicCookieStore();
        HttpContext context = client.getDefaultContext();
        context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
       
        HttpGet httpget = new HttpGet("/test/");
       
        HttpResponse response1 = client.execute(getServerHttp(), httpget, context);
        HttpEntity e1 = response1.getEntity();
        if (e1 != null) {
            e1.consumeContent();
        }
       
        List<Cookie> cookies = cookieStore.getCookies();
        assertNotNull(cookies);
        assertEquals(1, cookies.size());

        HttpResponse response2 = client.execute(getServerHttp(), httpget, context);
        HttpEntity e2 = response2.getEntity();
View Full Code Here

        this.localServer.register("*", new CookieVer1Service());
       
        DefaultHttpClient client = new DefaultHttpClient();
        client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2965);

        CookieStore cookieStore = new BasicCookieStore();
        HttpContext context = client.getDefaultContext();
        context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
       
        HttpGet httpget = new HttpGet("/test/");
       
        HttpResponse response1 = client.execute(getServerHttp(), httpget, context);
        HttpEntity e1 = response1.getEntity();
        if (e1 != null) {
            e1.consumeContent();
        }
       
        List<Cookie> cookies = cookieStore.getCookies();
        assertNotNull(cookies);
        assertEquals(2, cookies.size());

        HttpResponse response2 = client.execute(getServerHttp(), httpget, context);
        HttpEntity e2 = response2.getEntity();
View Full Code Here

        this.localServer.register("*", new CookieVer2Service());
       
        DefaultHttpClient client = new DefaultHttpClient();
        client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2965);

        CookieStore cookieStore = new BasicCookieStore();
        HttpContext context = client.getDefaultContext();
        context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
       
        HttpGet httpget = new HttpGet("/test/");
       
        HttpResponse response1 = client.execute(getServerHttp(), httpget, context);
        HttpEntity e1 = response1.getEntity();
        if (e1 != null) {
            e1.consumeContent();
        }
       
        List<Cookie> cookies = cookieStore.getCookies();
        assertNotNull(cookies);
        assertEquals(1, cookies.size());

        HttpResponse response2 = client.execute(getServerHttp(), httpget, context);
        HttpEntity e2 = response2.getEntity();
View Full Code Here

        this.localServer.register("*", new SetCookieVersionMixService());
       
        DefaultHttpClient client = new DefaultHttpClient();
        client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2965);

        CookieStore cookieStore = new BasicCookieStore();
        HttpContext context = client.getDefaultContext();
        context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
       
        HttpGet httpget = new HttpGet("/test/");
       
        HttpResponse response1 = client.execute(getServerHttp(), httpget, context);
        HttpEntity e1 = response1.getEntity();
        if (e1 != null) {
            e1.consumeContent();
        }
       
        List<Cookie> cookies = cookieStore.getCookies();
        assertNotNull(cookies);
        assertEquals(1, cookies.size());
        assertEquals("right", cookies.get(0).getValue());
        assertTrue(cookies.get(0) instanceof SetCookie2);
    }
View Full Code Here

TOP

Related Classes of org.apache.http.client.CookieStore

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.