Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "Set-Cookie" header

I'm trying to get 'Set Cookie' header using apache httpclietn-4.2.2 and having some problems.

Header in Firebug:

Set-Cookie  remixreg_sid=deleted; expires=Thu, 10-Nov-2011 04:32:30 GMT; path=/; 
domain=.vk.com remixapi_sid=deleted; expires=Thu, 10-Nov-2011 04:32:30 GMT; path=/; 
domain=.vk.com remixrec_sid=deleted; expires=Thu, 10-Nov-2011 04:32:30 GMT; path=/;
domain=.vk.com remixsid=0000000000000000000000000000000000000000000000000000; expires=Mon, 04-Nov-2013 16:10:24 GMT; path=/; domain=.vk.com

How I'm trying to obtain it:

 //location is a header with url I need to do GET request to
 Header location = response.getFirstHeader("Location");
 HttpGet httpGet = new HttpGet(location.getValue());
 httpClient.getParams().setParameter(
 //tried to use different policies
 ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2965);
 Header [] allHeaders=response.getAllHeaders();

In allHeaders I have all headers except "Set Cookie". And I have warnings like this:

WARNING: Invalid cookie header: "Set-Cookie: remixlang=0; expires=Mon, 18-Nov-2013 
03:21:47 GMT; path=/; domain=.vk.com". Unrecognized cookie header 'Set-Cookie: 
remixlang=0; expires=Mon, 18-Nov-2013 03:21:47 GMT; path=/; domain=.vk.com'
Nov 09, 2012 4:31:41 AM org.apache.http.client.protocol.ResponseProcessCookies 
processCookies

So I think the problem is with 'expires' date.

What I tried to do:

1) Invalid cookie header : Unable to parse expires attribute when expires attribute is empty Created custom CookieSpec and tried to use it:

 httpClient.getCookieSpecs().register("vkCookie", new CookieSpecFactory() {
     public CookieSpec newInstance(HttpParams params){
         return new VkCookieSpec();
         }
     });
HttpClientParams.setCookiePolicy(httpClient.getParams(), "vkCookie");

2) Tried to set Data Format in httpClient params :

  httpClient.getParams().setParameter(CookieSpecPNames.DATE_PATTERNS, Arrays.asList("EEE, dd-MMM-yyyy HH:mm:ss z"));

But I'm still getting that warning. Would appreciate any help.

like image 492
pomkine Avatar asked Nov 09 '12 04:11

pomkine


1 Answers

I know this is an old question. But I had the same problem and just wanted to post my snippet to solve it, in particular setting CookieSpecs.STANDARD explicitly (see spec on apache commons for details):

        RequestConfig globalConfig = RequestConfig.custom()
                .setCookieSpec(CookieSpecs.DEFAULT)
                .build();
        CloseableHttpClient httpClient = HttpClients.custom()
                .setDefaultRequestConfig(globalConfig)
                .build();
        RequestConfig localConfig = RequestConfig.copy(globalConfig)
                .setCookieSpec(CookieSpecs.STANDARD)
                .build();
        HttpGet httpGet = new HttpGet(url);
        httpGet.setConfig(localConfig); 

        // Request
        CloseableHttpResponse response = httpClient.execute(httpGet);

Hope this helps.

like image 181
muelleth Avatar answered Sep 28 '22 07:09

muelleth