Android Persistent Save Cookie

Source: Internet
Author: User

When parsing Web page information, you need to log in to access, so use httpclient to impersonate the login, and then save the cookie for the next access to use, then you need to persist the contents of the cookie.

Before the first science, the basic knowledge:

What is a cookie?

Cookies are small files that are created in the client's system or are created in the client's browser's memory (if temporary). It can be used to implement the function of state management. We can store a small amount of information on a system that is short, so that it can be used when needed. The most interesting thing is that it is transparent to the user. In your Web application, you can use it everywhere, and it's incredibly simple. Cookies are stored in the form of text. If a Web application uses cookies, the server is responsible for sending cookies and the client browser will store it. The browser will return cookies to the server the next time the page is requested. The most common example is the use of a cookie to store user information, user preferences, "Remember password" operations, and so on. Cookies have many advantages, and of course there are many drawbacks. I'm going to tell you later.

How have cookies been created?

When a client makes a request to the server, the server sends a cookie to the client. The same cookies can be used for subsequent requests. For example, if CodeProject.com stores the session ID as a cookie. When a client requests a page to the Web server for the first time, the server generates the session ID and sends it as a cookie to the client.


Now, with all subsequent requests from the same client, it will use the session ID from the cookie, as shown in the image below.


Browsers and Web servers to exchange cookies for information in response. For different sites, the browser maintains different cookies. If a page requires information from a cookie, when a URL is "clicked", the browser will first search the local system for information about the cookies before turning to the server for information.

Advantages of Cookies

Here are the key benefits of using cookies:

(1) Implementation and use are very simple

(2) The browser is responsible for maintaining the data sent (cookies content).

(3) for cookies from multiple sites, the browser automatically manages them

The disadvantage of cookies

The following are the main disadvantages of cookies:

(1) It stores data in a simple text format, so it's not safe at all

(2) For cookie data, there is a size limit (4kB)

(3) The maximum number of cookies is also limited. Mainstream browsers offer to limit the number of cookies to 20. If the new cookie arrives, then the old will be deleted. Some browsers can support up to 300 cookies.

(4) We need to configure the browser, cookies will not work in the browser configuration of the High security level environment.

What is persistent and non-persistent cookies

We can divide cookies into two categories:

(1) Persistent cookies

(2) Non-persistent cookies

Persistent Cookies: This can be referred to as permanent cookies, which are stored on the client's hard drive until they expire. Persistent cookies should be set to an expiration time. Sometimes, they persist until the user deletes them. Persistent cookies are often used to collect a user's identity information for a system.

non-persistent cookies: They can also be referred to as temporary cookies. If no expiration time is defined, then the cookie will be stored in the browser's memory. The example I showed above is a non-persistent cookie.

Modifying a persistent cookie is no different from a non-persistent cookie. The only difference is that persistent cookies have an expiration time setting.

Cookie Persistence

HttpClient can be used with the persistent cookie store that implements the Cookiestore interface with any physical representation. The default Cookiestore implementation is called Basicclientcookie, which is a simple implementation with java.util.ArrayList. Cookies stored in the Basicclientcookie object are lost when the container object is reclaimed by the garbage collection mechanism. Users can provide more complex implementations, if needed.

The download focuses on how to use httpclient to persist cookies in Android:

One, request the network to obtain the cookie

Let's take a look at the following code:

[Java]View PlainCopy
    1. defaulthttpclient httpclient =  new defaulthttpclient ();   
    2. httpget httpget = new httpget ( "HTTP +/ Www.hlovey.com ");   
    3. httpresponse response =  Httpclient.execute (httpget);   
    4. httpentity entity = response.getentity ();   
    5. list<cookie> cookies = httpclient.getcookiestore (). GetCookies ();  //execute request before there is a cookie

Post emulation Login

[Java]View PlainCopy
    1. HttpPost HttpPost = new HttpPost (URL);
    2. list<namevaluepair> formparams = new arraylist<namevaluepair> ();
    3. Formparams.add (new Basicnamevaluepair ("id", userid));
    4. Formparams.add (new Basicnamevaluepair ("passwd", passwd));
    5. Urlencodedformentity entity;
    6. try {
    7. entity = New Urlencodedformentity (Formparams, mobilesmthencoding);
    8. } catch (Unsupportedencodingexception E1) {
    9. return 3;
    10. }
    11. Httppost.setentity (entity);
    12. Httppost.setheader ("User-agent", useragent);
    13. HttpResponse response = Httpclient.execute (HttpPost);

Android Persistent Save Cookie

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.