Android cookie sharing to WebView avoid login again (stay signed in)

Source: Internet
Author: User
Tags set cookie setcookie

Recently in the project to use the WebView open the specified link to the page, you can already set the WebView cookie but always jump to the login page, it is obvious that the cookie is not set successfully caused WebView not set up the cookie sent out ...

12345 cookiesyncmanager.createinstance (context); cookiemanager Cookiemanager = Cookiemanager.getinstance (); cookiemanager.setacceptcookie (true );cookiemanager.setcookie (URL, cookies); //cookies is a cookie that is obtained in HttpClient CookieSyncManager.getInstance().sync();

The above code allows you to associate a pre-saved cookie with a specified URL to maintain the status of the login and avoid repeated logons. Unfortunately, the author in the development process in strict accordance with the above set cookie code to set the cookie, can result or failure, always jump to the login page prompt login. Later returned to see the next Android API document, found that the Setcookie function of the two parameter values are described as follows:

Public void Setcookie (string url, string value)Added in API Level 1

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

Url The URL for which the cookie is set
Value The cookie as a string, using the format of the ' Set-cookie ' HTTP response header
The value parameter is the string form of the Cookie value, but the format must be the HTTP request header format "Set-cookie".

The API document in this sentence suddenly let me wake up, so after Baidu I found the format of the cookie-related article, now excerpt as follows:

Original link: http://www.cnblogs.com/hdtianfu/archive/2013/05/30/3108295.html

Cookie-related HTTP headers

there is a connection to the HTTP header and cookies: Set-cookie and cookies. Set-cookie is sent by the server, which is included in the header of the response request. It is used to create a cookie on the clientThe cookie header is sent by the client and is included in the header of the HTTP request. Note that only the domain and path of the cookie match the requested URL to send this cookie. Set-cookie Headerthe format of the Set-cookie response header is as follows:Set-cookie: =[; =] ...[; expires=][; domain=][; path=][; secure][; HttpOnly] expires=: Sets the period of validity of the cookie, and if the cookie exceeds the date indicated by date, the cookie will expire. If this option is not set, then the cookie will expire when the browser is closed. Note: Date is Greenwich mean GMT (GMT) and is expressed in the following format:Day , DD MMM YYYY HH:MM:SS GMT DayThe day of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat).DDThe day is in the month (such as- the first day of the month).MMMThe three-letter abbreviation for the month (Jan, Feb, Mar, APR, May, June, Jul, Mar, Sep, OCT, Nov, Dec).YYYYThe year .HHthe hour value in military time (would is 10:00am, for example).MMThe minute value.SSThe second value. domain=:path=:Note: Temporary cookies (cookies without expires parameters) cannot be accompanied by the domain option. when a client sends an HTTP request, a valid cookie is sent to the server along with it. if the domain and path parameters of a cookie match the URL, the cookie is valid.

a URL contains domain and path, you can refer to the http://www.w3school.com.cn/html/html_url.asp

Secure: Indicates that the cookie can only be sent to the HTTP server. HttpOnly: Indicates that the cookie cannot be obtained by client script.

Generating expires in a program

c the Waytime_t curtime = time (NULL);tm * gmtime = gmtime (&curtime);Char strexperis[50];strftime (strtimebuf, +, "%a,%d%b%Y%x GMT;", gmtime); the way of JavaScriptvar d = new Date ();var expires = d.togmtstring (); 

The above-mentioned red bold text more convinced me of my judgment, at this moment how excited the mood is needless to say, hey

So I added the domain and path field to the cookie string that I originally set, and with the desired mood run, haha, the result is correct, no longer have to log in repeatedly.

After this bug resolution, in the programming can not let go of any detail, to each running details are clearly so as to avoid blind to solve the problem, the ultimate waste of time ...

Welcome to the discussion group discussion: 192209234

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.