Cookie is the so-called "sweet cookie", he earliest is in Netscape Navigator 2.0. Cookie is actually created by the web server, and stores the information on the computer on the computer. So why is the web server to create such a file on the client? This is because when the client sends a request to the web server (for example, when you are ready to browse the page), whether this client is a first visit, the server treats it as the first time, the WEB server does Just simply respond, then turn off the connection with the user. This is obvious when the disadvantage caused by the process. Since the network view company has developed a cookie, you can use the cookie to save the user's identification information. The role of cookie can record the page that you have visited on the site, which helps you customize when you next accessing the site. Cookies can also store individual identifiable information. Personal recognition information is available to identify or contact information, such as names, email addresses, home or work addresses, or phone numbers. However, the website can only access the individual identifiable information you provide. For example, the website will not be able to determine your email name unless you provide an email name. In addition, the website cannot access additional information on your computer through cookies. Of course, unless you are provided. So where is the cookie to store? If the system's system is Window 98 and installed in the "C" disk, the cookie is stored in the "C: / Windows / Cookies" directory; if the machine system is the window 2000 and installed in the "C" disk, then cookie is stored "C: / documents and settings / administrator / cookies" directory. Understand so many knowledge of cookies, let's take a look at how the focus of this article - how COOKIE is programmed. The main contents are two: one is how C # is how to write cookies; the second is how C # is how to access the cookies written.
First, the program design and running software environment introduced in this article:
Microsoft Window 2000 Server Edition .NET Framework SDK Beta 2
C # performs cookie aspects to be implemented by the ASP.NET page.
Second, C # How to write cookie:
In order to write cookies, his steps have three steps, as follows:
First create an HTTPCOOKIE object, constructed a cookie through this object, the name of this object is the Cookie name generated later. The specific code: httpcookie cookie = new httpcookie ("User-defined Cookie Name");
Then the "value" property of the created HTTPCookie object is assigned a string value, "value" is the value of the cookie generated later. The specific code is as follows: cookie.Value = "Users to cookie assignment"; if you want to write the cookie value is not a simple string, but a complex data type, we know that these data types cannot be stored directly to cookie In the cookie, you can only store strings. But you can pass a way to convert this complex data type into multiple strings, then assign the multiple strings to the resulting cookie value, so the content in the cookie is rich, and the cookie is used later. The completion of the function is also powerful. At this time, you may understand why when you browse the web server, the web server will know when you have been viewed, and once you have to wait for information. Because this information has been stored in your first browsing page, the WEB server has a cookie generated. The following code is to store multiple strings to cookie: cookie ["Name"] = "Wang Tian"; cookie ["Gender" = "Men"; cookie ["age" = "26"; cookie Temporary and forever. Permanent cookies are stored on the computer in the form of a file, and when Internet Explorer is retained on the computer. When you access the site again, create the cookie website can be read. When the specific programming is written in this cookie, set the lifecycle of the cookie, the specific code: datetime dtnow = datetime. Now; time; time, Timespan Tsminute = New Timespan (0, 1, 0, 0); cookie. Expires = DTNOW TSMINUTE; The above code is the life period of the created cookie is "an hour", which you can set the specific life of the cookie by modifying the "Timespan" property. Finally, call the "ADD () method of the" response.cookies ", add this object, so you can generate a cookie. The specific code is as follows: response. Cookies. Add (cookie); the following code is to write to cookie's full code (Write.aspx): <% @ language = "c #"%> Creating a cookie in the C # page
This cookie's life cycle is defined as an hour
The picture below is the interface after running the above code:
Figure 01: C # writes a cookie program run interface
Of course, the cookie generated by the above code is a bit monotonous above. In fact, for the very rich cookie, he has many properties, fully utilizing these properties, can take advantage of the powerful function of cookie. The following table is some common attributes of cookie:
Property Description Domain setting / get the domain name that cookie belongs to. Once this property is set, it is only limited to this cookie of the web server. You can set the path to "ccw.com.cn" Path setting / get the path that the cookie should belong to. If it is set, the web page accesses this cookie is defined inside this path. The web page of other paths cannot be accessed. Secure Setting / Get a sign to indicate whether the HTTP protocol is secure to transfer the cookie to the client's browser. Haskeys indicates whether this cookie is composed of multiple strings. When writing to cookies, the maximum use of these properties is important for maximum extent of written cookies.
Third, C # is how to read the generated cookie:
Reading the specified cookie is much easier to write to the cookie, you can do it with the "Request.Cookies" object. Below is a way to read the specified cookie name:
HTTPCOOKIE COOKIE = Request.Cookies ["Cookie Name"];
Here is the value of the Cookie that has been read:
Response.write (cookie. Value. Tostring ()); Master the above points, read the cookie is very easy, the following is the program code reading cookies: <% @ language = "c #" %> Reads specified cookie values in the C # page
The picture below is the interface after running the above code:
Figure 02: Programs run interface to read the value of the specified cookie
Fourth, summary:
So we have already introduced most of the contents of Cookie programming with C #. In fact, cookies have a relatively large role on the Internet. For example, it allows the web site to track the number of accesses, the last access time, and the accesser enter the path of the site. You can also tell the online advertiser advertisement to be clicked, so that it can make the user more accurately; it allows users Enter some sites that have been viewed without typing passwords and username; the most important thing is that it helps sites to statistically user data to achieve personalized services, etc. Master COOKIE programming, you can make full use of cookies in the program to implement these features. Don't believe you try it.