2 Replies Latest reply: Oct 25, 2012 10:01 AM by 401556 RSS

    how to insert existing cookies into CookieManager

      I am hitting a website that requires authentication that I have accessed through the web browser, and the required cookies are now stored in my file system, so that i don't have to authenticate through the browser anymore.
      i used a chrome plugin to have those cookies written out to a cookies.txt file, and per instructions in that plugin, i was able to successfully use that cookies.txt with a wget command to hit the website and not have to authenticate.

      But when i write a java HttpURLConnection based application to hit the website, i of course get redirected to the login page.
      is there any way that i can programmatically insert those browser cookies into a CookieStore and set up a CookieManager that will allow me to use those cookies within my application and not have to authenticate?

        • 1. Re: how to insert existing cookies into CookieManager
          i would use Apache HttpClient in stead of URLConnection, it is a more robust (and reliable) API with full support for all HTTP actions, including cookie management.
          • 2. Re: how to insert existing cookies into CookieManager
            Thanks for the response - however, using httpclient, i'm stuck trying to get out of the proxy - keeps telling me "407 authentication required".
            i used the following example: [Proxy authentication|http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientProxyAuthentication.java]

            code snippet:
                    DefaultHttpClient httpclient = new DefaultHttpClient();
                    try {
                                new AuthScope("proxy-url-here", 80),
                                new NTCredentials("username", "password", "computer-name", "domain"));
                        HttpHost targetHost = new HttpHost("target-host-url");
                        HttpHost proxy = new HttpHost("proxy-url-here", 80);
                        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
                        HttpGet httpget = new HttpGet("specific-page-here");
                        HttpResponse response = httpclient.execute(targetHost, httpget);
            any ideas what i'm doing wrong?

            Edited by: nthali on Oct 24, 2012 4:07 PM

            Edited by: nthali on Oct 25, 2012 8:00 AM