软件下载 | 资讯教程 | 最近更新 | 下载排行 | 一键转帖 | 发布投稿
您的位置:最火下载站 > 网络编程 > ASP.NET > HttpWebRequest采集读取网站挂载Cookie的通用方法

HttpWebRequest采集读取网站挂载Cookie的通用方法

HttpWebRequest采集时添加:httpWebRequest.CookieContainer = new CookieContainer();就能读取到远程挂载的cookie,那么怎样去挂载上的cookie呢?

下面方法为大家解除烦恼。

遍历方法:

Copy to ClipboardLiehuo.Net Codes引用的内容:[www.veryhuo.com]
public static List<Cookie> GetAllCookies(CookieContainer cc)
{
List<Cookie> lstCookies = new List<Cookie>();

Hashtable table = (Hashtable)cc.GetType().InvokeMember("m_domainTable", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Instance, null, cc, new object[] { });
StringBuilder sb = new StringBuilder();
foreach (object pathList in table.Values)
{
SortedList lstCookieCol = (SortedList)pathList.GetType().InvokeMember("m_list", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Instance, null, pathList, new object[] { });
foreach (CookieCollection colCookies in lstCookieCol.Values)
foreach (Cookie c in colCookies)
{
lstCookies.Add(c);
sb.AppendLine(c.Domain + ":" + c.Name + "____" + c.Value + "\r\n");
}
}
return lstCookies;
}

使用:

Copy to ClipboardLiehuo.Net Codes引用的内容:[www.veryhuo.com]
List<Cookie> _cookieList = GetAllCookies(req.CookieContainer);
string _cookieValue = _cookieList[0].ToString();

作者:王磊,博客:http://stone_w.cnblogs.com/

    相关阅读
    网友评论
    栏目导航
    推荐软件