Analysis of the difference between ES1en. Cookies and ES3en. Cookies

  • 2020-12-07 04:11:57
  • OfStack

NET provides a variety of methods to read and write Cookie. Request. Cookies is the Cookie transmitted from the client to the server in the form of Cookie header. Response.Cookies is created on the server and transmitted to the client as the ES9en-ES10en header. In other words, one is sent from the client to the server, and one is sent from the server to the client.

The first time Cookies is created, the following two read methods get the same thing:

C # code


HttpCookie hc = new HttpCookie("User2");
hc["UserName"] = "mengxianhui";
Response.Cookies.Add(hc);
  
Response.Cookies["User1"]["UserId"] = "net_lover";
  
Response.Write(Request.Cookies["User1"].Values["UserId"].ToString());
Response.Write(Request.Cookies["User2"].Values["UserName"].ToString());
Response.Write("<hr>");
Response.Write(Response.Cookies["User1"].Values["UserId"].ToString());
Response.Write(Response.Cookies["User2"].Values["UserName"].ToString());

However, once Cookie exists and is read using the above method, the result is different, Response.Cookies can immediately read to the new Cookie, while Request.Cookies read to the previous set, that is, must send a request to the server through the client to read. Why is there such a difference? It should be the implementation of.ES28en. There is a paragraph like this:
C # code


if (includeResponse && (this.Response != null))
{
    HttpCookieCollection cookies = this.Response.Cookies;
    if (cookies.Count > 0)
    {
        HttpCookie[] dest = new HttpCookie[cookies.Count];
        cookies.CopyTo(dest, 0);
        for (int i = 0; i < dest.Length; i++)
        {
            cookieCollection.AddCookie(dest[i], true);
        }
    }
}

When Cookie does not exist, it should read Cookie in ES37en.Cookies, so the first read is the same, but later read different reasons.

In addition, Response.Cookies must be read after setup. It cannot be read at any time like ES44en.Cookies. If Cookies is read on another page, the following method is wrong

C # code


protected void Page_Load(object sender, EventArgs e)
{
  Response.Write(Response.Cookies["User1"].Values["UserId"].ToString());
  Response.Write(Response.Cookies["User2"].Values["UserName"].ToString());
}

Conclusion:

Request. Cookies: Mainly used to obtain all Cookie values, including JS, ES61en. Cookies and Response? The & # 63; .Cookie value created by Cookies3 methods; At this point, we can see that ES66en.Cookies can both obtain Cookie and create Cookie, so what is the difference between Cookie created by Response and JS created by ES77en? Through experiments, it is found that Cookie created by ES74en.Cookies is only ? Request. Cookies can be obtained, but the other two methods cannot be obtained, which means that Cookie created by ES79en. Cookies can only be used.

Response. Cookies: Mainly used for the creation, assignment and deletion of Cookie, after ? Response.Cookies operates on Cookie, all methods get the updated value, that is, ES92en. Cookies is to modify the value of Cookie in all containers; In addition to being the ultimate modifier of Cookie, we may find that part of the Cookie value can be obtained through ES98en.Cookies when we write the code. It has been found by experiments that the Cookie value obtained here is only the Cookie value in this session and created through ES102en.Cookies, which is basically useless.


Related articles: