|
我有一个想法,就是建一个静态的哈希表用来存储Cookie,以网站的域名为键,然后值为另一个子哈希表,以cookie的字段名称为键,以它的值为子哈希表的值。
在用HttpHelper类的对象访问一个网站之前检查cookie哈希表里是否已经有这个网站的cookie信息,如果用就在访问这个网站之前把它对应的cookie值给HttpHelper,最后在完成访问后,再把HttpHelper的cookies存储到那个静态的哈希表中
下面是我写的用来存储cookie的哈希表现实代码,问题的问题是我不知道这些访问应该加到HttpHelper中的哪个位置去
[code=csharp]public static Hashtable CookieList = new Hashtable();//用来存储cookie的哈希表
/// <summary>
/// 返回指定网站的cookies
/// </summary>
/// <param name="cookieDomain">Cookie所属网站</param>
/// <returns>CookieContainer</returns>
public static CookieContainer GetCookieContainer(string cookieDomain)
{
CookieContainer myCookieContainer = new CookieContainer();
if (CookieList.ContainsKey(cookieDomain))
{
Hashtable Hash = (Hashtable) CookieList[cookieDomain];
foreach(DictionaryEntry temCookie in Hash){
Cookie ck = new Cookie(temCookie.Key.ToString(), temCookie.Value.ToString());
ck.Domain = cookieDomain;
myCookieContainer.Add(ck);
}
}
return myCookieContainer;
}
/// <summary>
/// 设置指定网站的cookies
/// </summary>
/// <param name="cookieDomain">Cookie所属网站</param>
/// <param name="cookies">Cookie值</param>
public static void SetCookie(string cookieDomain, string cookies)
{
Hashtable Hash;
if (CookieList.ContainsKey(cookieDomain))
{
Hash = (Hashtable)CookieList[cookieDomain];
string[] cookstr = cookies.Split(';');
foreach (string str in cookstr)
{
string[] cookieNameValue = str.Split('=');
if (!Hash.ContainsKey(cookieNameValue[0].Trim().ToString()))
{
Hash.Add(cookieNameValue[0].Trim().ToString(), cookieNameValue[1].Trim().ToString());
}
else
{
Hash[cookieNameValue[0].Trim().ToString()] = cookieNameValue[1].Trim().ToString();
}
}
}
else
{
Hash = new Hashtable();
string[] cookstr = cookies.Split(';');
foreach (string str in cookstr)
{
string[] cookieNameValue = str.Split('=');
Hash.Add(cookieNameValue[0].Trim().ToString(), cookieNameValue[1].Trim().ToString());
}
}
}
/// <summary>
/// 删除指定网站的cookies
/// </summary>
/// <param name="cookieDomain">Cookie所属网站</param>
public static void DeLCookie(string cookieDomain)
{
if (CookieList.ContainsKey(cookieDomain))
{
CookieList.Remove(cookieDomain);
}
}[/code]
|
|