Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deal with cookie in Android

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to deal with cookie in Android". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to deal with cookie in Android" can help you solve the problem.

Android client Cookie processing

Cookie, sometimes in its plural form Cookies, refers to the data (usually encrypted) stored on the user's local terminal by some websites in order to identify the user and perform session tracking.

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

Initiate

Cookie is generated by the server and sent to User-Agent (usually browsers, clients). The browser will save the key/value of Cookie to 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 cookie). The Cookie name and value can be defined by the server-side developer itself, or it can be written directly to the jsessionid for JSP, so that the server can know whether the user is a legitimate user and whether he or she needs to log in again.

Use

The server can take advantage of the arbitrariness of the information contained in the Cookies to filter and regularly maintain the information to determine the state in the HTTP transmission. The most typical application of Cookies is to determine whether a registered user has logged on to the site, and users may be prompted whether to keep user information the next time they enter the site in order to simplify login procedures. These are the functions of Cookies. Another important application is processing such as "shopping cart". Users may select different items on different pages of the same site over a period of time, and this information is written to Cookies so that the information can be extracted at the time of final payment. At present, the most popular thing in Cookies is to record users' login information, so that you don't have to enter your user name and password on the next visit-- of course, this convenience also has the problem of disclosure of user information, especially when multiple users share a computer.

Life cycle

Cookie can keep the login information to the user's next session with the server, in other words, the next time you visit the same site, the user will find that he or she has logged in without entering a user name and password (of course, it does not rule out the possibility that the user will delete the Cookie manually). And some Cookie are deleted when the user exits the session, which can effectively protect personal privacy.

Cookie is assigned an expiration value when it is generated, which is the life cycle of Cookie, during which the Cookie is valid, and the Cookie beyond the cycle is cleared. Some pages set the life cycle of Cookie to "0" or a negative value, so that when the browser is closed, Cookie is cleared immediately, user information is not recorded, and it is more secure.

Operation of Cookie in Android:

Logic: Cookie is generated by the server. When the client sends the Http request to the server for the first time, "Cookie" is not included in the Http request header, that is, the Header, because there is no Cookie. 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, the cookie value can be placed in the Http request header. The server will know which user has accessed the server again. The client needs to save the Cookie for use when this user accesses the server again. Cookie will have an expiration time. When the client visits the server again after the Cookie failure, the server will return a Cookie failure status code (such as 403, etc.) and include the "Set-Cookie" field in the HttpResponse, which is the new Cookie. What the client needs to do is: 1. Overwrite the new Cookie with the old Cookie. two。 Jump into the user login interface and let the user log back in with the new Cookie. Cookie can be saved in SharePreferenced or Sqlite.

Implementation code:

Public byte [] doPost (String url, List values) {HttpPost post = new HttpPost (url); post.setHeadr ("Accept", "");.} this is the end of the introduction of "how to deal with cookie in Android". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report