Package org.apache.http.client

Examples of org.apache.http.client.CookieStore


    public void testRedirectWithCookie() throws Exception {
        final HttpHost target = getServerHttp();

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

        final CookieStore cookieStore = new BasicCookieStore();

        final BasicClientCookie cookie = new BasicClientCookie("name", "value");
        cookie.setDomain(target.getHostName());
        cookie.setPath("/");

        cookieStore.addCookie(cookie);

        final HttpContext context = new BasicHttpContext();
        context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
        final HttpGet httpget = new HttpGet("/oldlocation/");

View Full Code Here


        final HttpGet httpget = new HttpGet("http://somehost/stuff");
        final HttpClientContext context = HttpClientContext.create();

        final Lookup<CookieSpecProvider> localCookieSpecRegistry = Mockito.mock(Lookup.class);
        final Lookup<AuthSchemeProvider> localAuthSchemeRegistry = Mockito.mock(Lookup.class);
        final CookieStore localCookieStore = Mockito.mock(CookieStore.class);
        final CredentialsProvider localCredentialsProvider = Mockito.mock(CredentialsProvider.class);
        final RequestConfig localConfig = RequestConfig.custom().build();

        context.setCookieSpecRegistry(localCookieSpecRegistry);
        context.setAuthSchemeRegistry(localAuthSchemeRegistry);
View Full Code Here

    public final static void main(String[] args) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            // Create a local instance of cookie store
            CookieStore cookieStore = new BasicCookieStore();

            // Create local HTTP context
            HttpClientContext localContext = HttpClientContext.create();
            // Bind custom cookie store to the local context
            localContext.setCookieStore(cookieStore);

            HttpGet httpget = new HttpGet("http://localhost/");
            System.out.println("Executing request " + httpget.getRequestLine());

            // Pass local context as a parameter
            CloseableHttpResponse response = httpclient.execute(httpget, localContext);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                List<Cookie> cookies = cookieStore.getCookies();
                for (int i = 0; i < cookies.size(); i++) {
                    System.out.println("Local cookie: " + cookies.get(i));
                }
                EntityUtils.consume(response.getEntity());
            } finally {
View Full Code Here

        connManager.setMaxTotal(100);
        connManager.setDefaultMaxPerRoute(10);
        connManager.setMaxPerRoute(new HttpRoute(new HttpHost("somehost", 80)), 20);

        // Use custom cookie store if necessary.
        CookieStore cookieStore = new BasicCookieStore();
        // Use custom credentials provider if necessary.
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        // Create global request configuration
        RequestConfig defaultRequestConfig = RequestConfig.custom()
            .setCookieSpec(CookieSpecs.BEST_MATCH)
View Full Code Here

            {
                httpRequest.setHeader(entry.getKey(), entry.getValue());
            }
        }
       
        CookieStore cookieStore = httpClient.getCookieStore();
       
        if (cookieStore != null)
        {
            Map<String, String> defaultRequestCookies = proxyPathMapper.getDefaultRequestCookies();
           
            if (defaultRequestCookies != null)
            {
                for (Map.Entry<String, String> entry : defaultRequestCookies.entrySet())
                {
                    cookieStore.addCookie(new BasicClientCookie(entry.getKey(), entry.getValue()));
                }
            }
        }
       
        HttpEntity httpEntity = null;
View Full Code Here

      fetchMethod.setHeader(new BasicHeader("User-Agent",userAgent));
      fetchMethod.setHeader(new BasicHeader("From",from));
      fetchMethod.setHeader(new BasicHeader("Accept","*/*"));
       
      // Use a custom cookie store
      CookieStore cookieStore = new OurBasicCookieStore();
      // If we have any cookies to set, set them.
      if (loginCookies != null)
      {
        if (Logging.connectors.isDebugEnabled())
          Logging.connectors.debug("WEB: Adding "+Integer.toString(loginCookies.getCookieCount())+" cookies for '"+urlPath+"'");
        int h = 0;
        while (h < loginCookies.getCookieCount())
        {
          if (Logging.connectors.isDebugEnabled())
            Logging.connectors.debug("WEB:  Cookie '"+loginCookies.getCookie(h)+"' added");
          cookieStore.addCookie(loginCookies.getCookie(h++));
        }
      }


      // Copy out the current cookies, in case the fetch fails
View Full Code Here

        // Start by accessing the secured index.html of war1
        DefaultHttpClient httpclient = relaxedCookieHttpClient();
        try {
            checkAccessDenied(httpclient, warA1 + "index.html");

            CookieStore store = httpclient.getCookieStore();

            log.debug("Saw JSESSIONID=" + getSessionIdValueFromState(store));

            // Submit the login form
            executeFormLogin(httpclient, warA1);
View Full Code Here

        // Start by accessing the secured index.html of war1
        DefaultHttpClient httpclient = relaxedCookieHttpClient();
        try {
            checkAccessDenied(httpclient, warA1 + "index.html");

            CookieStore store = httpclient.getCookieStore();

            log.debug("Saw JSESSIONID=" + getSessionIdValueFromState(store));

            // Submit the login form
            executeFormLogin(httpclient, warA1);
View Full Code Here

        HttpEntity entity = null;
        ResponseHandler <String> responseHandler = null;
        try {
            BasicHttpContext localContext = new BasicHttpContext();
            // Create a local instance of cookie store
            CookieStore cookieStore = new BasicCookieStore();
            localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

            Header sessionHeader = null;

            if(this.loginAsUrl != null ) {

                String loginAsUri = this.host + this.loginAsUrl;
                String loginAsParamString = "?" + this.loginAsUserParam + "&" + this.loginAsPasswordParam;

                HttpGet req2 = new HttpGet ( loginAsUri + loginAsParamString );
                System.out.println("loginAsUrl:" + loginAsUri + loginAsParamString);

                req2.setHeader("Connection","Keep-Alive");
                HttpResponse rsp = client.execute(req2, localContext);

                Header[] headers = rsp.getAllHeaders();
                for (int i=0; i<headers.length; i++) {
                    Header hdr = headers[i];
                    String headerValue = hdr.getValue();
                    if (headerValue.startsWith("JSESSIONID")) {
                        sessionHeader = hdr;
                    }
                    System.out.println("login: " + hdr.getName() + " : " + hdr.getValue());
                }
                List<Cookie> cookies = cookieStore.getCookies();
                System.out.println("cookies.size(): " + cookies.size());
                for (int i = 0; i < cookies.size(); i++) {
                    System.out.println("Local cookie(0): " + cookies.get(i));
                }
            }
View Full Code Here

      fetchMethod.setHeader(new BasicHeader("User-Agent",userAgent));
      fetchMethod.setHeader(new BasicHeader("From",from));
      fetchMethod.setHeader(new BasicHeader("Accept","*/*"));
       
      // Use a custom cookie store
      CookieStore cookieStore = new OurBasicCookieStore();
      // If we have any cookies to set, set them.
      if (loginCookies != null)
      {
        if (Logging.connectors.isDebugEnabled())
          Logging.connectors.debug("WEB: Adding "+Integer.toString(loginCookies.getCookieCount())+" cookies for '"+urlPath+"'");
        int h = 0;
        while (h < loginCookies.getCookieCount())
        {
          if (Logging.connectors.isDebugEnabled())
            Logging.connectors.debug("WEB:  Cookie '"+loginCookies.getCookie(h)+"' added");
          cookieStore.addCookie(loginCookies.getCookie(h++));
        }
      }


      // Copy out the current cookies, in case the fetch fails
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.