Re: QUESTION ABOUT COOKIES
Joan Balagueró Valls wrote: Hello, Here the attach with the trace. It's a post request from my local app to my servlet. In all tests I've always used: bcc.setVersion(1); bcc.setAttribute(ClientCookie.VERSION_ATTR, "1"); Thanks, Joan. Joan, I am afraid the attachment got stripped away. Post it inline. Oleg -Mensaje original- De: Oleg Kalnichevski [mailto:ol...@apache.org] Enviado el: martes, 07 de julio de 2009 17:18 Para: HttpClient User Discussion Asunto: Re: QUESTION ABOUT COOKIES On Tue, Jul 07, 2009 at 05:04:59PM +0200, Joan Balaguer? Valls wrote: 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() + 6)); // 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() + 6)); 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() + 6)), 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, Post a wire log of the session: http://hc.apache.org/httpcomponents-client/logging.html Also, try setting cookie version to version 1 to force the use of a RFC compliant cookie spec and see if that makes any difference. Oleg Joan. -Mensaje original- De: Oleg Kalnichevski [mailto:ol...@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-unsubscr...@hc.apache.org For additional commands, e-mail: httpclient-users-h...@hc.apache.org - To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org For additional commands, e-mail: httpclient-u
RE: QUESTION ABOUT COOKIES
Hello, Here the attach with the trace. It's a post request from my local app to my servlet. In all tests I've always used: bcc.setVersion(1); bcc.setAttribute(ClientCookie.VERSION_ATTR, "1"); Thanks, Joan. -Mensaje original- De: Oleg Kalnichevski [mailto:ol...@apache.org] Enviado el: martes, 07 de julio de 2009 17:18 Para: HttpClient User Discussion Asunto: Re: QUESTION ABOUT COOKIES On Tue, Jul 07, 2009 at 05:04:59PM +0200, Joan Balaguer? Valls wrote: > 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() + 6)); // 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() + > 6)); > 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() + 6)), 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, > Post a wire log of the session: http://hc.apache.org/httpcomponents-client/logging.html Also, try setting cookie version to version 1 to force the use of a RFC compliant cookie spec and see if that makes any difference. Oleg > Joan. > > > > > -Mensaje original- > De: Oleg Kalnichevski [mailto:ol...@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 > >
Re: QUESTION ABOUT COOKIES
On Tue, Jul 07, 2009 at 05:04:59PM +0200, Joan Balaguer? Valls wrote: > 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() + 6)); // 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() + > 6)); > 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() + 6)), 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, > Post a wire log of the session: http://hc.apache.org/httpcomponents-client/logging.html Also, try setting cookie version to version 1 to force the use of a RFC compliant cookie spec and see if that makes any difference. Oleg > Joan. > > > > > -----Mensaje original- > De: Oleg Kalnichevski [mailto:ol...@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-unsubscr...@hc.apache.org > For additional commands, e-mail: httpclient-users-h...@hc.apache.org > > > > - > To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org > For additional commands, e-mail: httpclient-users-h...@hc.apache.org > - To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org For additional commands, e-mail: httpclient-users-h...@hc.apache.org
RE: QUESTION ABOUT COOKIES
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() + 6)); // 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() + 6)); 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() + 6)), 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:ol...@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-unsubscr...@hc.apache.org For additional commands, e-mail: httpclient-users-h...@hc.apache.org - To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org For additional commands, e-mail: httpclient-users-h...@hc.apache.org
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-unsubscr...@hc.apache.org For additional commands, e-mail: httpclient-users-h...@hc.apache.org
Re: Question About Cookies
Joan Balagueró wrote: Now I know how to send a cookie just related to one thread. But how to receive a cookie from my remote server? I was using : Cookie[] auxCookies = objHttp.getState().getCookies(); Now I know this is incorrect. I suppose that I must get cookies from the Post method. You don't. HttpClient automagically puts cookies into the HttpState object that you use for executing the method. Don't do anything, just let it happen. And make sure that the thread is using the same HttpState objects for the followup requests. Do NOT create HttpState objects for each request, just one for each thread or session. cheers, Roland - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Question About Cookies
Hello, Thanks for your response. Now I know how to send a cookie just related to one thread. But how to receive a cookie from my remote server? I was using : Cookie[] auxCookies = objHttp.getState().getCookies(); Now I know this is incorrect. I suppose that I must get cookies from the Post method. But how? Should I use the "method.getResponseHeader("Set-Cookie")? Or there is another way? Thanks, Joan. -Mensaje original- De: Roland Weber [mailto:[EMAIL PROTECTED] Enviado el: miércoles, 05 de marzo de 2008 19:38 Para: HttpClient User Discussion Asunto: Re: Question About Cookies Hello Joan, there is no need to shout. > The question is: doing this, am I setting this cookie for all threads or > just for the current thread that is executing this code? For all threads. > Obviously, I want > to set this cookie just for the current thread, not for all. Is this > correct? And if not, how should I do it? You should have a separate HttpState object for each thread instead of using the default HttpState object for all of them: http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/Ht tpClient.html#executeMethod(org.apache.commons.httpclient.HostConfiguration, %20org.apache.commons.httpclient.HttpMethod,%20org.apache.commons.httpclient .HttpState) > And the following question is: this request is sent to another servlet, that > reads it. But if I examine the HttpServletRequest, I always get null for > cookies attribute. Its like httpclient is not including the cookie in the > request Im sending to the remote servlet. Check the domain and path attributes of your cookie. Enable wire logging to see wether the cookie is sent or not. http://hc.apache.org/httpclient-3.x/logging.html hope that helps, Roland - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Question About Cookies
Hello Joan, there is no need to shout. The question is: doing this, am I setting this cookie for all threads or just for the current thread that is executing this code? For all threads. Obviously, I want to set this cookie just for the current thread, not for all. Is this correct? And if not, how should I do it? You should have a separate HttpState object for each thread instead of using the default HttpState object for all of them: http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/HttpClient.html#executeMethod(org.apache.commons.httpclient.HostConfiguration,%20org.apache.commons.httpclient.HttpMethod,%20org.apache.commons.httpclient.HttpState) And the following question is: this request is sent to another servlet, that reads it. But if I examine the HttpServletRequest, I always get null for cookies attribute. It’s like httpclient is not including the cookie in the request I’m sending to the remote servlet. Check the domain and path attributes of your cookie. Enable wire logging to see wether the cookie is sent or not. http://hc.apache.org/httpclient-3.x/logging.html hope that helps, Roland - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]