hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Joan Balagueró Valls <joan.balagu...@grupoventus.com>
Subject RE: QUESTION ABOUT COOKIES
Date Tue, 07 Jul 2009 15:04:59 GMT
Hello Oleg,

Thanks, after some tests this is exactly what it happens.

When I send this cookie to my servlet, I receive all its data correctly
except the "max-age" attribute, which is always -1.

I'm trying to set MAX_AGE with your API, and I'm getting crazy...

I've tried "stdCookie.setExpiryDate(new
java.util.Date(System.currentTimeMillis() + 60000));  // Expires after 60
seconds

But my servlet gets -1 (when I get cookies from HttpServletRequest with
request.getCookies()).

I supposed that I had to set the same value for MAX_AGE attribute. Then I
tried:

stdCookie.setExpiryDate(new java.util.Date(System.currentTimeMillis() +
60000));
stdCookie.setAttribute(ClientCookie.MAX_AGE_ATTR, "60");  // 60 seconds

But my servlet still receives -1 in MAX_AGE.

Finally, I'm trying to set the "ClientCookie.EXPIRES_ATTR" with the value of
java.util.Date(System.currentTimeMillis() + 60000)), but the value passed to
setAttribute is expected to be a String, and I have a java.util.Date. How
can I make this conversion?

Thanks in advance,

Joan. 




-----Mensaje original-----
De: Oleg Kalnichevski [mailto:olegk@apache.org] 
Enviado el: martes, 07 de julio de 2009 16:18
Para: HttpClient User Discussion
Asunto: Re: QUESTION ABOUT COOKIES

On Mon, Jul 06, 2009 at 08:14:48PM +0200, Joan Balaguer? Valls wrote:
> Hello Oleg,
> 
>  
> 
> I?m trying to send cookies to a servlet with a simple app. Following the
> tutorial:
> 
>  
> 
> HttpContext localContext = new BasicHttpContext();
> 
> CookieStore cookieStore  = new BasicCookieStore();
> 
>  
> BasicClientCookie stdCookie = new BasicClientCookie("name", "value");
> stdCookie.setVersion(1);
> stdCookie.setDomain(".mycompany.com");
> stdCookie.setPath("/");
> stdCookie.setSecure(true);
>  
> // Set attributes EXACTLY as sent by the server 
> stdCookie.setAttribute(ClientCookie.VERSION_ATTR, "1");
> stdCookie.setAttribute(ClientCookie.DOMAIN_ATTR, ".mycompany.com");
> 
>  
> 
>  cookieStore.addCookie(stdCookie);
> 
>  localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
> 
>  
> 
>  HttpEntity entity = objHttp.execute(objPost, localContext).getEntity();
> 
>  
> 
>  
> 
> This does not work (at least for me). To work, you need to add:
> 
>  
> 
> stdCookie.setAttribute(ClientCookie.PATH, "/");
>  
> If I forget any of the ?set? statement, or any of the ?setAttribute?
> statement, it does not work.
>  
>  
> The question is: Why have we to set twice the components of the
> ?BasicClientCookie?, the first using ?stdCookie.set? and the second using
> ?stdCookie.setAttribute??
>  
> And should I set ?ClientCookie.SECURE_ATTR? and
?ClientCookie.MAX_AGE_ATTR?
> ?
> 


Joan

This is because some cookies set domain / path / port attributes explicitly,
while some do not, in which case values of those attributes are derived from
the properties of the origin server.

Consider the following example:

Set-Cookie: 
  stuff="very important"; path="/"; domain="myhost.mydomain.com"; version=1
Set-Cookie: 
  stuff="very important"; version=1

These two cookies are obviously different but they essentially represent the
same piece of state information if sent in response to a request for
"http://myhost.mydomain.com/index.html"

 
>  
> And the second part: when the servlet receives this cookie, it is resent
to
> another servlet using the same sequence of operations. But debugging, one
> can see that the cookie is not added to the cookieStore (the sentence
> ?cookieStore.addCookie(stdCookie);? does not add anything to
?cookieStore?).
> 

A cookie does not get added to the cookie store only if it has expired.

Hope this helps

Oleg

 
> Can you help me?
>  
>  
> Thanks in advance,
>  
> Joan.
> 
>  
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message