Detailed explanation of cookie processing in Android

Android client cookie processing

Cookies, sometimes in the plural form, refer to the data (usually encrypted) stored on the user's local terminal by some websites in order to identify the user's identity and carry out session tracking.

Cookie was first invented by Lou montulli, a former employee of Netscape, in March 1993.

launch

The cookie is generated by the server and sent to the user agent (generally the browser and the client). The browser will save the key / value of the cookie into a text file in a directory, and send the cookie to the server the next time the same website is requested (provided that the browser is set to enable Cookies). The cookie name and value can be developed and defined by the server. For JSP, jsessionid can also be written directly, so that the server can know whether the user is a legal user and whether it needs to log in again.

purpose

The server can use the arbitrariness of the information contained in cookies to filter and regularly maintain these information to judge the status in HTTP transmission. The most typical application of cookies is to determine whether the registered user has logged in to the website. The user may be prompted whether to retain the user information when entering the website next time in order to simplify the login procedures. These are the functions of cookies. Another important application is processing such as "shopping cart". Users may select different products from different pages of the same website over a period of time, and these information will be written into cookies to extract information at the time of final payment. At present, cookies are most widely used to record user login information, so that you don't need to enter your own user name and password during the next visit - of course, this convenience also has the problem of user information disclosure, especially when multiple users share a computer.

Life cycle

Cookies can keep the login information to the user's next session with the server. In other words, when visiting the same website next time, the user will find that he has logged in without entering the user name and password (of course, it does not rule out that the user can delete cookies manually). Some cookies are deleted when the user exits the session, which can effectively protect personal privacy.

When a cookie is generated, an expire value will be specified, which is the lifetime of the cookie. During this cycle, the cookie is valid, and the cookie beyond the cycle will be cleared. Some pages set the cookie lifetime to "0" or negative value, so that the cookie will be cleared immediately when the browser is closed, and the user information will not be recorded, which is more secure.

Operation of cookies in Android:

Logic: cookies are generated by the server. When the client sends an HTTP request to the server for the first time, because there is no cookie, the "cookie" is not included in the HTTP request header. At this time, there will be an additional field "set cookie" in the corresponding (httpresponse) returned by the server. The value of this field is the cookie value generated by the server. The next time this client program accesses the server, it can put the cookie value into the HTTP request header. The server will know which user accessed the server again. The client needs to save the cookie to use when the user accesses the server again. The cookie will have an expiration time. When the client accesses the server again after the cookie expires, the server will return a cookie expiration status code (such as 403) and include the "set cookie" field in the httpresponse, which is a new cookie. What the client needs to do is: 1. Overwrite the old cookie with the new cookie. 2. Jump into the user login interface and let the user log in again with a new cookie. Cookies can be saved in sharepreferred or SQLite.

Implementation code:

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>