Change and delete cookies

Source: Internet
Author: User


The first method : response.cookies["UserName"]. Value= "Zhang San"

response.cookies["UserName"]. (1);

The second method:

HttpCookie cookie=new HttpCookie ("UserName");

Cookies. Value= "Zhang San";

Cookies. (1);


Multi-value Cookies (sub-key):

The first method:

response.cookies["UserInfo" ["UserName"]= "Zhang San";

response.cookies["UserInfo" ["pwd"]= "123";

response.cookies["UserInfo"]. (1);

The second method:

HttpCookie cookie=new HttpCookie ("UserInfo");

Cookies. Values ("userName") = "Zhang San";

Cookies. Values ("pwd") = "123";

Cookies. (1);



Delete a single Cookies

response.cookies["UserName"]. (-1);

Delete all the Cookies

HttpCookie Acookie;

String CookieName;

int count=request.cookies.count;

for (int i=0;i<count;i++)


Cookiename=request.cookies[i]. Name;

Acookie=new HttpCookie (cookiename); (-1);



Delete a single child key

HttpCookie acookie=request.cookies["UserInfo"];

ACookie.Values.Remove ("UserName"); (-1);



Modify a single cookie

response.cookies["UserName"]. Value= "John Doe";

response.cookies["UserName"]. (1);

Modify a single sub-key

response.cookies["UserInfo" ["pwd"]= "234";

response.cookies["UserInfo"]. (1);


If (request.cookies["UserName"]!=null)


lable1.text=request.cookies["UserName"]. Value;



If (request.cookies["UserName"]!=null)


HttpCookie cookie=request.cookies["UserName"];

Lable.text=cookie. Value;


multi-valued reads :

If (request.cookies["UserInfo"]!=null)


lable1.text=request.cookies["UserInfo" ["UserName"];

lable2.text=request.cookies["UserInfo" ["pwd"];


Read all the Cookies

for (int i=0;i<request.cookies.count;i++)




String Value=acookie.value;


Adding and deleting cookies

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: 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.