Android Cookie共享到WebView避免再次登錄(保持登錄狀態)

歡迎訪問個人網站:http://www.bincpu.com

最近在做項目時用到了webview打開指定鏈接的網頁,可已經把webview設置了cookie但始終跳轉到登錄頁面,這明顯是cookie沒有設置成功導致webview沒有將設置好的cookie發送出去……

CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.setCookie(url, cookies);//cookies是在HttpClient中獲得的cookie
CookieSyncManager.getInstance().sync();

通過上述代碼即可把事先保存下來的cookie和指定的url關聯起來,達到保持登錄的狀態,避免重複登錄。遺憾的是,筆者在開發過程當中嚴格按照上述設置cookie的代碼來進行cookie的設置,可結果還是失敗,始終跳轉到登錄頁提示登錄。後來返回去查看了下Android API文檔,發現setCookie函數的兩個參數值說明如下:

public void setCookie(String url, String value)

Added in API level 1

Sets a cookie for the given URL. Any existing cookie with the same host, path and name will be replaced with the new cookie. The cookie being set must not have expired and must not be a session cookie, otherwise it will be ignored.


urlthe URL for which the cookie is set
valuethe cookie as a string, using the format of the 'Set-Cookie' HTTP response header
value參數的作用是cookie值的字符串形式,但格式一定要是http請求頭格式"Set-Cookie"。

api文檔裏的這句話突然讓我驚醒,於是經過百度我查到了cookie的格式相關的文章,現摘抄如下:

原文鏈接:http://www.cnblogs.com/hdtianfu/archive/2013/05/30/3108295.html

Cookie相關的Http頭

   有連個Http頭部和Cookie有關:Set-Cookie和Cookie。

   Set-Cookie由服務器發送,它包含在響應請求的頭部中。它用於在客戶端創建一個Cookie

   Cookie頭由客戶端發送,包含在HTTP請求的頭部中。注意,只有cookie的domain和path與請求的URL匹配纔會發送這個cookie。


Set-Cookie Header

   Set-Cookie響應頭的格式如下所示:


       Set-Cookie: <name>=<value>[; <name>=<value>]...

                   [; expires=<date>][; domain=<domain_name>]

                   [; path=<some_path>][; secure][; httponly]


   expires=<date>: 設置cookie的有效期,如果cookie超過date所表示的日期時,cookie將失效。

                   如果沒有設置這個選項,那麼cookie將在瀏覽器關閉時失效。

                   注意:date是格林威治時間(GMT),使用如下格式表示:

                       DAY, DD MMM YYYY HH:MM:SS GMT


                       DAY

                           The day of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat).

                       DD

                           The day in the month (such as 01 for the first day of the month).

                       MMM

                           The three-letter abbreviation for the month (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec).

                       YYYY

                           The year.

                       HH

                           The hour value in military time (22 would be 10:00 P.M., for example).

                       MM

                           The minute value.

                       SS

                           The second value.


   domain=<domain_name> :

   path=<some_path>:

                   注:臨時cookie(沒有expires參數的cookie)不能帶有domain選項。

                   當客戶端發送一個http請求時,會將有效的cookie一起發送給服務器。

                   如果一個cookie的domain和path參數和URL匹配,那麼這個cookie就是有效的。

                   一個URL中包含有domain和path,可以參考http://www.w3school.com.cn/html/html_url.asp


   secure   : 表示cookie只能被髮送到http服務器。

   httponly : 表示cookie不能被客戶端腳本獲取到。


在程序中生成expires

   C的方式

       time_t curTime = time(NULL);

       tm * gmTime = gmtime(&curTime);


       char strExperis[50];

       strftime(strTimeBuf, 100, " %a, %d %b %Y %X GMT;", gmTime);


   JavaScript的方式

       var d = new Date();

       var expires = d.toGMTString();


上述紅色加粗的文字更讓我確信了我的判斷,此時此刻的心情有多麼激動就不用說了,嘿嘿

於是,我在原來設置的cookie字符串上加上了domain和path字段,懷着期盼的心情run了下,哈哈,結果正確了,再也不用反覆登錄了。

經過此次的bug解決,在編程中不能放過任何一個細節,要對每個運行細節都清清楚楚這樣才能避免盲目的去解決問題,最終浪費時間……

歡迎加入討論羣討論:285077071

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章