在C#Web Api项目中遍历.CookieCollection()并从中提取数据

在C#Web Api项目中遍历.CookieCollection()并从中提取数据,c#,asp.net,cookies,asp.net-web-api,C#,Asp.net,Cookies,Asp.net Web Api,我正在用截图重新发布这个问题,这样可能会使我的问题更容易理解 我有一个在HttpWebRequest上填充的CookieContainer。我正在尝试迭代这些值,并使用以下代码行进行排序: var getvalue = myWebRequest.CookieContainer.GetCookies(new Uri("https://mydummy.domain.com")); 但是,在cookie集合中列出的4个潜在值中,它总是返回最后一个条目 有人知道我怎样才能得到这个列表,这样我就可以

我正在用截图重新发布这个问题,这样可能会使我的问题更容易理解

我有一个在HttpWebRequest上填充的CookieContainer。我正在尝试迭代这些值,并使用以下代码行进行排序:

  var getvalue = myWebRequest.CookieContainer.GetCookies(new Uri("https://mydummy.domain.com"));
但是,在cookie集合中列出的4个潜在值中,它总是返回最后一个条目

有人知道我怎样才能得到这个列表,这样我就可以反复浏览并选择我想要的值了吗


在最后一张图片中,我在可能的4个条目中的第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;
        }
    }
}
}
公共静态IEnumerable GetAllCookies(此CookieContainer c)
{
Hashtable k=(Hashtable)c.GetType().GetField(“m_domainTable”,BindingFlags.Instance | BindingFlags.NonPublic).GetValue(c);
foreach(k中的DictionaryEntry元素)
{
SortedList l=(SortedList)element.Value.GetType().GetField(“m_list”,BindingFlags.Instance | BindingFlags.NonPublic”).GetValue(element.Value);
foreach(l中的变量e)
{
var cl=(CookieCollection)((DictionaryEntry)e).值;
foreach(cl中的Cookie fc)
{
收益率fc;
}
}
}
}