2015-09-02 80 views
0

我用截圖重新發布此問題可能會使我的問題更容易理解。迭代並從C#Web Api項目中的.CookieCollection()中提取數據

我有一個CookieContainer填充HttpWebRequest。我試圖通過值迭代和排序的親近與下面一行代碼:

var getvalue = myWebRequest.CookieContainer.GetCookies(new Uri("https://mydummy.domain.com")); 

但是,出於Cookie集合中列出的4個潛在的價值,它總是返回的最後一項。

有誰知道我可以如何得到清單,所以我迭代和櫻桃選擇我後的價值?

enter image description here

enter image description here

enter image description here

enter image description here

在過去的圖像我有在2位的入口出可能的4個條目的(在陣列位置示出1) 。這是我試圖拔出的JSESSIONID值。

回答

0

最終發現這篇文章其中有這也正是我就是一個例子後:

How can I get all Cookies of a CookieContainer?

示例代碼:

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; 
     } 
    } 
} 
}