![](/img/trans.png)
[英]How can I pull a data from any a web site through webrequest in c#?
[英]Iterate through and pull data from .CookieCollection() in C# Web Api project
我使用屏幕截圖重新發布了這個問題,可能會使我的問題更容易理解。
我有一個填充在HttpWebRequest上的CookieContainer。 我正在嘗試遍歷值,並通過以下代碼行進行了進一步的排序:
var getvalue = myWebRequest.CookieContainer.GetCookies(new Uri("https://mydummy.domain.com"));
但是,在cookie集合中列出的4個潛在值中,它總是返回最后一個條目。
有誰知道我如何獲得這份清單,以便我進行一次迭代,然后挑選我所追求的價值?
在最后一個圖像中,我在可能的4個條目中(位置1處顯示 )中的第2個條目。 我要拉出的是JSESSIONID值。
最終找到這篇文章,其中有一個示例完全符合我的要求:
示例代碼:
public static IEnumerable<Cookie> GetAllCookies(this CookieContainer c)
{
Hashtable k = (Hashtable)c.GetType().GetField("m_domainTable", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(c);
foreach (DictionaryEntry element in k)
{
SortedList l = (SortedList)element.Value.GetType().GetField("m_list", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(element.Value);
foreach (var e in l)
{
var cl = (CookieCollection)((DictionaryEntry)e).Value;
foreach (Cookie fc in cl)
{
yield return fc;
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.