Cookie写入浏览器的过程:我们可以使用如下代码在Asp.net项目中写一个Cookie 并发送到客户端的浏览器(为了简单我没有设置其它属性)。
HttpCookie cookie = new HttpCookie("MyCookieName", "string value");
Response.Cookies.Add(cookie);
Response.Cookies.Add(cookie);
代码的关键点在于调用了Response.Cookies.Add(),我们来看看它们是如何实现的。首先来看Response.Cookies
public HttpCookieCollection Cookies
{
get
{
if (this._cookies == null)
{
this._cookies = new HttpCookieCollection(this, false);
}
return this._cookies;
}
}
{
get
{
if (this._cookies == null)
{
this._cookies = new HttpCookieCollection(this, false);
}
return this._cookies;
}
}
再来看HttpCookieCollection这个集合的实现:
public sealed class HttpCookieCollection : NameObjectCollectionBase
{
public HttpCookieCollection() : base(StringComparer.OrdinalIgnoreCase)
{
}
internal HttpCookieCollection(HttpResponse response, bool readOnly)
: base(StringComparer.OrdinalIgnoreCase)
{
this._response = response;
base.IsReadOnly = readOnly;
}
public void Add(HttpCookie cookie)
{
if (this._response != null)
{
this._response.BeforeCookieCollectionChange();
}
this.AddCookie(cookie, true);
if (this._response != null)
{
this._response.OnCookieAdd(cookie);
}
}
{
public HttpCookieCollection() : base(StringComparer.OrdinalIgnoreCase)
{
}
internal HttpCookieCollection(HttpResponse response, bool readOnly)
: base(StringComparer.OrdinalIgnoreCase)
{
this._response = response;
base.IsReadOnly = readOnly;
}
public void Add(HttpCookie cookie)
{
if (this._response != null)
{
this._response.BeforeCookieCollectionChange();
}
this.AddCookie(cookie, true);
if (this._response != null)
{
this._response.OnCookieAdd(cookie);
}
}
注意:由于在HttpResponse中创建HttpCookieCollection时,把HttpResponse对象传入了,因此,在调用HttpCookieCollection.Add()方法时,会调用HttpResponse的OnCookieAdd(cookie); 我们接着看:
internal void OnCookieAdd(HttpCookie cookie)
{
this.Request.AddResponseCookie(cookie);
}
{
this.Request.AddResponseCookie(cookie);
}
又转到Request对象的调用了,接着看
internal void AddResponseCookie(HttpCookie cookie)
{
if (this._cookies != null)
{
this._cookies.AddCookie(cookie, true);
}
if (this._params != null)
{
this._params.MakeReadWrite();
this._params.Add(cookie.Name, cookie.Value);
this._params.MakeReadOnly();
}
}
{
if (this._cookies != null)
{
this._cookies.AddCookie(cookie, true);
}
if (this._params != null)
{
this._params.MakeReadWrite();
this._params.Add(cookie.Name, cookie.Value);
this._params.MakeReadOnly();
}
}
从以上的代码分析中,我们可以看到,我们调用Response.Cookies.Add()时,Cookie也会加到HttpRequest.Cookies中。
而且,如果我们访问过程Request.Params[],也会加到那个集合中。现在,您该明白了为什么当一个key有多个值时,为什么会用逗号分开来了吧。
或许,看到这里,您又有了一个新的想法:对于有多值的情况,还要我来按逗号拆分它们,太麻烦了,有没有不要拆分的方法呢?
答案是:有的,您可以访问NameValueCollection的GetValues方法,这个方法的实现如下:
public virtual string[] GetValues(string name)
{
ArrayList list = (ArrayList)base.BaseGet(name);
return GetAsStringArray(list);
}
private static string[] GetAsStringArray(ArrayList list)
{
int count = (list != null) ? list.Count : 0;
if( count == 0 ) {
return null;
}
string[] array = new string[count];
list.CopyTo(0, array, 0, count);
return array;
}
{
ArrayList list = (ArrayList)base.BaseGet(name);
return GetAsStringArray(list);
}
private static string[] GetAsStringArray(ArrayList list)
{
int count = (list != null) ? list.Count : 0;
if( count == 0 ) {
return null;
}
string[] array = new string[count];
list.CopyTo(0, array, 0, count);
return array;
}