[C#] 纯文本查看 复制代码
HttpResult ret = m_hb.GetHtml(itm);
if (ret.Cookie.Split(',').Length > ret.CookieCollection.Count)
{
string host = ret.Header["Host"];
if (null == host || host.Length == 0)
{
Uri uri = new Uri(itm.URL);
host = uri.Host;
}
CookieCollection cookielist = BaseTool.StrCookieToCookieCollection(host, ret.Cookie);
cookie.Add(cookielist);
}
else
{
cookie.Add(ret.CookieCollection);
}
[C#] 纯文本查看 复制代码
public CookieCollection StrCookieToCookieCollection(string baseDomain, string cookiestr)
{
CookieCollection cc = new CookieCollection();
string[] cookies = cookiestr.Split(',');
foreach(string c in cookies)
{
Cookie cookie = new Cookie();
string[] list = c.ToString().Split(new string[] { ",", ";" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string item in list)
{
string itemcookie = item.ToLower().Trim().Replace("\r\n", string.Empty).Replace("\n", string.Empty);
//排除空字符串
if (string.IsNullOrWhiteSpace(itemcookie)) continue;
else if (!itemcookie.Contains("="))
{
if(itemcookie == "httponly")
cookie.HttpOnly = true;
else if(itemcookie == "secure")
cookie.Secure = true;
}
else if (itemcookie.Contains("path="))
{
cookie.Path = itemcookie.Replace("path=", string.Empty);
}
else if (itemcookie.Contains("expires="))
{
cookie.Expires = Convert.ToDateTime(itemcookie.Replace("expires=", string.Empty));
}
else if (itemcookie.Contains("domain="))
{
cookie.Domain = itemcookie.Replace("domain=", string.Empty);
}
else
{
string[] kv = item.ToString().Replace("\r\n", string.Empty).Replace("\n", string.Empty).Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
if (kv.Length == 2)
{
cookie.Name = kv[0].Trim();
cookie.Value = kv[1].Trim();
}
}
}
if (cookie.Domain.Length == 0)
{
cookie.Domain = baseDomain;
}
cc.Add(cookie);
}
return cc;
}
[C#] 纯文本查看 复制代码
HttpResult ret = m_hb.GetHtml(itm);
if (ret.Cookie.Split(',').Length > ret.CookieCollection.Count)
{
string host = ret.Header["Host"];
if (null == host || host.Length == 0)
{
Uri uri = new Uri(itm.URL);
host = uri.Host;
}
CookieCollection cookielist = BaseTool.StrCookieToCookieCollection(host, ret.Cookie);
cookie.Add(cookielist);
}
else
{
cookie.Add(ret.CookieCollection);
}