Two cookie classes

Source: Internet
Author: User

. NET provides two cookie classes:

System. Web. httpcookie and system. net. Cookie

There are two cookie collection classes.

System. Web. httpcookiecollection and system. net. cookiecollection

The differences between them are as follows:

The system. Web namespace is used for server segments, and system. NET is used for client programs.

In fact, there are more than the following differences:

Next we will compare the attributes of these two cookie classes as follows. These attributes are all copied from the msdn Chinese Version instructions:

System. Web. httpcookie class System. net. Cookie class
Description of the constructor in msdn:
Overloaded. Initializes a new instance of the httpcookie class.
Description of the constructor in msdn:
Overloaded. Initialize a new instance of the cookie class according to Netscape specifications. Generally, applications do not need to construct cookie classes because they are automatically created based on the set-Cookie header received through the HTTP response.
 

Comment

Obtains or sets the comments that the server can add to the cookie.

 

Commenturi

Obtain or set the URI comments provided by the server through cookies.

 

Discard

Gets or sets the discard flag set by the server.

Domain
Obtain or set the domain associated with the cookie.

Domain
Obtain or set the valid URI of a cookie.

 

Expired

Obtains or sets the current status of the cookie.

Expires
Obtain or set the cookie expiration date and time.

Expires
Obtain or set the cookie expiration date and time as datetime.

 

HTTPOnly
Determine whether the page script or other activity content can access this cookie.

Name
Obtain or set the cookie name.

Name
Obtain or set the cookie name.

Path
Obtain or set the virtual path to be transmitted with the current cookie.

Path
Obtain or set the URI that this cookie applies.

 

Port
Obtain or set the list of TCP ports that this cookie applies.

Secure
Gets or sets a value that indicates whether to use Secure Sockets Layer (SSL) (that is, to transmit cookies only through https.

Secure
Obtains or sets the cookie security level.

 

Timestamp
Obtain the time when this cookie is issued as datetime.

Value
Obtain or set a single cookie value.

Value
Obtain or set the cookie value.

Values
Obtains the set of key-value pairs contained in a cookie object.

Version
Obtain or set the HTTP status maintenance version that meets the cookie.

You will see that the system. net. Cookie class has better attributes than the system. Web. httpcookie class, and some of our web developers do not know the attributes. Why?

This should begin with the cookie specification. There are currently the following cookie specifications:

  • Draft Netscape COOKIE: the earliest cookie specification, based on rfc2109. Although this specification is significantly different from rc2109, many servers are compatible with it.
  • Rfc2109 is the first official cookie specification released by W3C. Theoretically, all servers must follow this rule when processing cookies (version 1. Unfortunately, this specification is so strict that many servers improperly implement it or are still using the Netscape specification.
  • Rfc2965 defines cookie version 2 and describes the shortcomings of cookie version 1.

Rfc2965 specification is currently used in a small number. Rfc2109 specifications are much stricter. In actual applications, not all browsers and web servers strictly abide by them. Therefore, the draft Netscape cookie is a simple and widely supported cookie specification.

Come back, let's see it again.Differences between the system. Web. httpcookie class and the system. net. Cookie class

The difference I understand is:

System. Web. httpcookie class

This category was initially designed for use by Web Servers because Microsoft's Web servers did not comply with rfc2109 \ rfc2965 specifications. Instead, it adopts the Netscape cookie draft scheme.

At the same time, in order to take into account some of the previous ASP encoding habits, so we have this class design.

As mentioned in Dudu's previous blog

Traverse system. Web. httpcookiecollection and write the following code:

Foreach (string name in request. Cookies)
{
Info + = string. Format ("{0} = {1} \ r \ n
", Name, request. Cookies [name]. value );
}

Foreach (httpcookie cookie in request. Cookies) may fail. Why does Microsoft have such a design.

System. net. Cookie class

This class should be mainly used by the client at the initial design,

Because the cookies of some servers follow the rfc2109 \ rfc2965 specification, the design of this class has additional attributes.

Related information:

What is the difference between system. net. Cookie and System. Web. httpcookie?

Http://topic.csdn.net/t/20050304/15/3824900.html

Why does foreach (httpcookie cookie in request. Cookies) fail?

Http://www.cnblogs.com/dudu/archive/2004/12/21/80118.html

How does the HTTP Proxy handle cookies correctly?

Http://www.ibm.com/developerworks/cn/java/j-cookie/

Draft Netscape cookies

Http://wp.netscape.com/eng/mozilla/3.0/handbook/javascript/cookies.htm

W3C rfc2109 Specification

Http://www.w3.org/Protocols/rfc2109/rfc2109.txt

W3C rfc2965 Specification

Http://www.ietf.org/rfc/rfc2965.txt

 

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: info-contact@alibabacloud.com 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.