1 //Create a HttpCookie object
2 HttpCookie cookie = new HttpCookie ("RON");
3 /Set this
cookie value 4 cookies. Value = "My name is Xiao Wei";
5 //Set the life cycle of the cookie, defined here as three minutes
6 DateTime dtnow = DateTime.Now;
7 TimeSpan Tsminute = new TimeSpan (0, 0, 3, 0);
8 cookies. Expires = Dtnow + tsminute;
9 cookie["Name"] = "Little Wei"; cookie["Sex"] = "male";
One cookie["age"] = ""; Add this cookie Response.Cookies.Add (cookie); Response.Write ("Cookie created");
HttpCookie in the System.Web namespace, so you need to quote
1 using System.Web;
* Create Cookies
The name and value of the cookie must be specified when creating the cookie. In the example, the name of the cookie is "RON", and value is "My name is Xiao Wei". Cookies are stored in the browser, and the difference between each cookie is based on the value of the property name. So, if the name is the same, the contents of the cookie will be overwritten. And those cookie["Name"] and so on are the subkeys of the cookie["RON".
*cookie Validity period
Another important attribute of the cookie is the expiration date. Set by property expires. In the example, the cookie is set to be valid for three minutes. So far, the longest period of validity is 50, this is the YA-land set AH. The company has been good for 50 years. In fact, in the validity of the settings I prefer this sentence.
Cookie. Expires = DateTime.Now.AddMinutes (3);
Simple, easy to understand.
For the duration of the issue, there are two of times more special:
#永久性 Cookie settings never expire for up to 50 years
#非永久性 cookies are not set to expire the browser disappears
* Multi-value Cookies
Cookies can hold multiple values, and you can just create a cookie with a button. For example
1 HttpCookie acookie = new HttpCookie ("UserInfo");
2 acookie.values["userName"] = "Rond";
3 acookie.values["lastvisit"] = DateTime.Now.ToString ();
4 Acookie.expires = DateTime.Now.AddDays (1);
A cookie named UserInfo is created in the example, and the cookie has two subkeys.
the scope of the *cookie
By default, the entire site of the cookie is valid. This means that all Web pages under the same site (which can also be understood as a single domain name) can be obtained from cookies. If you don't want your cookie to work in all scopes. You can limit the range of cookies. method is through the path property of the cookie, where the property value is the same as the range, and the relative path is valid. Such as
1 Appcookie.path = "/application1";
In addition to qualifying the path range for a cookie, you can also qualify the domain name range of the cookie with the domain property of the cookie. For example
1 Appcookie.domain = "jsjx.gdufs.edu.cn";
In the example, the cookie is limited to the "Information College" (jsjx.gdufs.edu.cn) website of "Guangdong University of Foreign languages and foreign trade", but it will not produce effect on the main website of "Guangdong University of Foreign languages and foreign Trade" (gdufs.edu.cn).
* Submit Cookies
Cookies are stored in the browser, so they need to be submitted in order to keep the information in the browser.
1 Response.Cookies.Add (Acookie);
This allows you to save the data in a browser.
Another way to create a ps:cookie is to refer to the MSDN instructions.
read:
Let's start with an example.
Get cookie HttpCookie cookie = request.cookies["RON"];
Determine if there is a user-entered cookie
if (null = =)
{
Response.Write ("The specified cookie is not found.") ");
}
else
{
//Find the specified cookie, display the value of the cookie
String strcookievalue = cookie. Value.tostring ();
Response.Write ("The value of the cookie is:" + strcookievalue + "<br >");
Response.Write (cookie["UserName"]);
}
* Get
Through the request response, we can get to request.cookies["RON". The value of the cookie can be read from the original setting. Cookie. Value can get all the values saved by the cookie. However, individuals prefer the cookie["UserName"] to get the value of the cookie they want.
If you are using a multi-valued set of the subkey of the cookie. So the way to get and set the way is similar.
* Read multiple cookies
More than one cookie may be saved on top of a browser. But how to get all the cookies. Write a loop and it comes out.
for (int i = 0; i < Request.Cookies.Count i++)
{
Response.Write ("cookie[+ i +"], name is: "+ request.cookies[i ]. Name + "<br/>");
The value of Response.Write ("cookie[" + i +) is: "+ request.cookies[i]. Value + "<br/>");
}
If you don't want the key/value of the key to appear together, you want to get it individually. You can also write a loop to show it all.
The name of the subkey
for (int i = 0; i < Request.Cookies.Count i++)
{
Response.Write ("cookie[+ i +"] is: "+ reques T.cookies[i]. Name + "<br/>");
if (request.cookies[i). HasKeys)
{for
(int j = 0; J < Request.cookies[i]. Values.count; J + +)
{
Response.Write ("cookie[" + i + "] [" + j + "] has the name:" + request.cookies[i]. VALUES.ALLKEYS[J] + "<br/>");
The value of Response.Write ("cookie[" + i + "] [" + j + "] is:" + request.cookies[i]. VALUES[J] + "<br/>");}}
Delete:
Cookies are saved on the browser side, and the time is set by the developer, and when the time expires, the browser will delete cookies. So the job of deleting cookies is to expire the cookie's expiration date. You can set the expiration date to the current time and the cookie expires immediately. You can also set the past time and the cookie will also be deleted.
As an example, I created a cookie called Ron, and now I'm going to delete it. So to create a new cookie with the same name, the time is set to the past.
* Delete a cookie
HttpCookie acookie= New HttpCookie ("RON");
Acookie.expires = DateTime.Now.AddDays ( -1);
RESPONSE.COOKIES.ADD (Acookie);
This allows the cookie["RON" to be deleted.
You may not remember how many cookies you saved on your browser at the time of the test, and you can write a loop to remove all cookies.
* Delete all Cookies