2011-04-19 165 views
0

我已将Apache HttpClient与java.sun.net.httpserver一起用作Server。服务器处理Http-Get请求。在开始时,服务器使用set-cookie标头设置会话cookie,这被apache httpclient(根据日志)接受,但问题在于cookie不会被客户端存储或发送回服务器。 我用一些网络浏览器测试了服务器应用程序,并且一切正常。所以这个问题似乎在客户端,尤其是考虑到所有cookie都被记录在log4j上。Apache HttpClient头响应问题

DEBUG [org.apache.http.impl.conn.SingleClientConnManager] Get connection for route HttpRoute[{}->http://localhost] 
DEBUG [org.apache.http.impl.conn.DefaultClientConnectionOperator] Connecting to localhost/127.0.0.1:80 
DEBUG [org.apache.http.client.protocol.RequestAddCookies] CookieSpec selected: best-match 
DEBUG [org.apache.http.client.protocol.RequestAuthCache] Auth cache not set in the context 
DEBUG [org.apache.http.impl.client.DefaultHttpClient] Attempt 1 to execute request 
DEBUG [org.apache.http.impl.conn.DefaultClientConnection] Sending request: GET /login?pass=2Gi/Kzj9 HTTP/1.1 
DEBUG [org.apache.http.headers] >> GET /login?pass=2Gi/Kzj9 HTTP/1.1 
DEBUG [org.apache.http.headers] >> Host: localhost 
DEBUG [org.apache.http.headers] >> Connection: Keep-Alive 
DEBUG [org.apache.http.headers] >> User-Agent: Apache-HttpClient/4.1.1 (java 1.5) 
DEBUG [org.apache.http.impl.conn.DefaultClientConnection] Receiving response: HTTP/1.1 200 OK 
DEBUG [org.apache.http.headers] << HTTP/1.1 200 OK 
DEBUG [org.apache.http.headers] << Transfer-encoding: chunked 
DEBUG [org.apache.http.headers] << Content-type: text/html 
DEBUG [org.apache.http.headers] << Set-cookie: SESSID=0.6092204529970631; expires=Tue, 19-4-11 18:28:43 GMT; Max-Age=3600; Path=/; Version="1" 
DEBUG [org.apache.http.client.protocol.ResponseProcessCookies] Cookie accepted: "[version: 0][name: SESSID][value: 0.6092204529970631][domain: localhost][path: /][expiry: Sun Apr 19 19:28:43 CET 11]". 
DEBUG [org.apache.http.impl.client.DefaultHttpClient] Connection can be kept alive indefinitely 
DEBUG [org.apache.http.impl.conn.SingleClientConnManager] Releasing connection [email protected]d0dd4 
DEBUG [org.apache.http.impl.conn.SingleClientConnManager] Get connection for route HttpRoute[{}->http://localhost] 
DEBUG [org.apache.http.impl.client.DefaultHttpClient] Stale connection check 
DEBUG [org.apache.http.client.protocol.RequestAddCookies] CookieSpec selected: best-match 
DEBUG [org.apache.http.client.protocol.RequestAuthCache] Auth cache not set in the context 
DEBUG [org.apache.http.impl.client.DefaultHttpClient] Attempt 1 to execute request 
DEBUG [org.apache.http.impl.conn.DefaultClientConnection] Sending request: GET /newnode?node=/tp HTTP/1.1 
DEBUG [org.apache.http.headers] >> GET /newnode?node=/tp HTTP/1.1 
DEBUG [org.apache.http.headers] >> Host: localhost 
DEBUG [org.apache.http.headers] >> Connection: Keep-Alive 
DEBUG [org.apache.http.headers] >> User-Agent: Apache-HttpClient/4.1.1 (java 1.5) 
DEBUG [org.apache.http.impl.conn.DefaultClientConnection] Receiving response: HTTP/1.1 200 OK 
DEBUG [org.apache.http.headers] << HTTP/1.1 200 OK 
DEBUG [org.apache.http.headers] << Transfer-encoding: chunked 
DEBUG [org.apache.http.headers] << Content-type: text/html 
DEBUG [org.apache.http.headers] << Set-cookie: SESSID=0.9499481656989606; expires=Tue, 19-4-11 18:28:43 GMT; Max-Age=3600; Path=/; Version="1" 
DEBUG [org.apache.http.client.protocol.ResponseProcessCookies] Cookie accepted: "[version: 0][name: SESSID][value: 0.9499481656989606][domain: localhost][path: /][expiry: Sun Apr 19 19:28:43 CET 11]". 
DEBUG [org.apache.http.impl.client.DefaultHttpClient] Connection can be kept alive indefinitely 
DEBUG [org.apache.http.impl.conn.SingleClientConnManager] Releasing connection [email protected]f53a 
+0

你看了所有的关于HTTP状态管理的HttpClient教程? http://hc.apache.org/httpcomponents-client-ga/tutorial/html/statemgmt.html该链接应该有希望解释使用Cookie与HttpClient所需的一切。 – hooknc 2011-04-19 15:42:09

回答

5
Set-cookie: SESSID=0.9499481656989606; expires=Tue, 19-4-11 18:28:43 GMT; Max-Age=3600; Path=/; Version="1" 

该Cookie值具有多个问题:

(1)它违反RFC2109和2965级的规格,通过使用具有逗号的属性值不与引号包围它。

(2)到期属性看起来很腥。我嫌疑人失效日期设置不正确,cookie只是在它被接受的同一时刻过期。此外,版本1 cookie(符合RFC 2109/RFC 2965)甚至不应该首先使用它。

该Cookie也Max-Age属性较新的饼干都应该使用,但因为cookie是畸形的,HttpClient的对待饼干旧Netscape风格的一个和Expiry属性优先Max-Age

+0

谢谢你,关于格式不正确的cookies的言论帮助了很多人 – user715484 2011-04-20 12:43:33