Summarize the key points of setting Cookie in C network programming

  • 2021-09-16 07:51:07
  • OfStack

It took 2 days to thoroughly understand the content of cookie in C #. Finding out the following will make you master the cookie of all websites.

cookieCollection is a collection of all cookie for 1 domain
cookeContainer is a container, which can hold a collection of cookie of multiple domains, that is, 1

cookieContainer can contain multiple cookieCollection, and this container can define the size, determine the

How many cookie can be installed at most? If it is full, it will automatically reject the original expired cookie.

Let's talk about the structure of an cookie:
Cookie consists of variable names and values, similar to Javascript variables. Its attributes include the standard Cookie

Variables, there are also variables created by users themselves, and variables in attributes are saved in the form of "variable = value".
According to the regulations of Netscape Company, Cookie format is as follows:

Set-Cookie: NAME=VALUE; Expires = DATE; Path = PATH;

Domain=DOMAIN_NAME; SECURE

NAME=VALUE:

This is a necessary part of every Cookie. NAME is the name of the Cookie, and VALUE is the name of the

The value of Cookie. In the string "NAME=VALUE", there are no semicolons, commas, spaces, and so on
Characters.
Expires=DATE: The Expires variable is a write-only variable that determines the effective end date of Cookie

Period. The attribute value DATE must be written in a specific format: day of the week,
DD-MM-YY HH: MM: SS GMT, GMT indicates that this is GMT. On the contrary, not in this case

The system will not recognize it. This variable can be omitted, if by default,
Cookie property values are not saved on the user's hard disk, but only in memory, Cookie

Files will disappear automatically as the browser closes.
Domain=DOMAIN-NAME: Domain This variable is a write-only variable that determines which

The Web server in the Internet domain can read the Cookie accessed by the browser, that is, only the Cookie from this
The information in Cookie can only be used by pages in individual domains. This setting is optional. If by default, set the

Set the attribute value of Cookie to the domain name of the Web server.
Path=PATH: The Path attribute defines which paths on the Web server are available for pages

Set Cookie. 1 If the user enters the path part in URL from the first character
Begin to include the string defined by the Path attribute, and the browser thinks it has passed the check. If the Path attribute

A value of "/", the Cookie can be read by all WWW resources on the Web server. Likewise
This setting is optional, if by default, the Path property value is the information passed to the browser by the Web server

The pathname of the source.
It can be seen that we can effectively control by setting two variables, Domain and Path

The scope of the Cookie file to be accessed.
Secure: Mark this variable in Cookie to indicate that only when the browser and Web Server

When the communication protocol is encryption authentication protocol, the browser submits the corresponding
Cookie. At present, there is only one such protocol, that is, HTTPS.
Of the above cookie content, only NAME and VALUE pairs are actually sent to the server, and other things

Are all for client browsers to manage cookie, such as whether it is stored on the hard disk? How long will it be stored? This

The browser should send this cookie when visiting which website.

These types of conversion methods:
cookieContainer. GetCookies () obtains cookieCollection, while cookieCollection is straight

Add the index number to obtain a specific cookie,

cookieContainer. add () can add cookie or cookieCollection, or even use the

The cookie information in the header of http can be directly added to cookieContainer, and cookie can be used

cookieContainer. SetCookies (Uri, string), where string is the string content of cookie, which can be obtained by response. Headers. Get ("Set-Cookie"). Note here that Uri in SetCookies () function cannot be exactly the same as the domain name Domain in cookie string, such as Uri=new Uri ("http://. google. com")

Domain =. google. com, at which point the cookieContainer. SetCookies () function will report an error,

Prompt that the domain name is incorrect. The solution is uri = new Uri ("http://www.google.com").

Anyway, it can be changed to other similar paths. Why can't it be the same? Who knows, ask Microsoft to go. That's it

The local problem cost me several hours.

The CookieContainer. GetCookieHeader () function can read out all the fingers in cookieContainer

Set the cookie of the website and display it as a string.
I want to input a string to construct cookie alone, but also very simple, Cookie constructor is done.

cookie Operation Example


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

 
public class Cookie
{
  /// <summary>
  /// Cookies Assignment 
  /// </summary>
  /// <param name="strName"> Primary key </param>
  /// <param name="strValue"> Key value </param>
  /// <param name="strDay"> Effective days </param>
  /// <returns></returns>
  public bool setCookie(string strName, string strValue, int strDay)
  {
    try
    {
      HttpCookie Cookie = new HttpCookie(strName);
      //Cookie.Domain = ".xxx.com";// When accessing across domain names, , To cookie You can specify the domain name , Format is .xxx.com
      Cookie.Expires = DateTime.Now.AddDays(strDay);
      Cookie.Value = strValue;
      System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
      return true;
    }
    catch
    {
      return false;
    }
  }

  /// <summary>
  ///  Read Cookies
  /// </summary>
  /// <param name="strName"> Primary key </param>
  /// <returns></returns>
 
  public string getCookie(string strName)
  {
    HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[strName];
    if (Cookie != null)
    {
      return Cookie.Value.ToString();
    }
    else
    {
      return null;
    }
  }

  /// <summary>
  ///  Delete Cookies
  /// </summary>
  /// <param name="strName"> Primary key </param>
  /// <returns></returns>
  public bool delCookie(string strName)
  {
    try
    {
      HttpCookie Cookie = new HttpCookie(strName);
      //Cookie.Domain = ".xxx.com";// When accessing across domain names, , To cookie You can specify the domain name , Format is .xxx.com
      Cookie.Expires = DateTime.Now.AddDays(-1);
      System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
      return true;
    }
    catch
    {
      return false;
    }
  }
}


Related articles: