2011-04-19 90 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