在 C# 中获取 Cookie
Getting Cookies in c#
所以,我正在向 Twitter (RestSharp) 发出 GET 请求,我想收集所有 cookie,然后放入 cookiecollection,首先我进行 get:
RestClient client = new RestClient("https://mobile.twitter.com");
RestRequest GetAuth = new RestRequest("/login");
var GetAuth_Response = client.Get(GetAuth);
现在,我想得到饼干,我这样做:
CookieCollection Cookies = GetAllCookies(client.CookieContainer);
>
public static CookieCollection GetAllCookies(CookieContainer container)
{
var allCookies = new CookieCollection();
var domainTableField = container.GetType().GetRuntimeFields().FirstOrDefault(x => x.Name == "m_domainTable");
var domains = (IDictionary)domainTableField.GetValue(container);
foreach (var val in domains.Values)
{
var type = val.GetType().GetRuntimeFields().First(x => x.Name == "m_list");
var values = (IDictionary)type.GetValue(val);
foreach (CookieCollection cookies in values.Values)
{
allCookies.Add(cookies);
}
}
return allCookies;
}
现在,当我 运行 程序时,我得到这个错误:
Additional information: Object reference not set to an instance of an object.
在这部分:
var domainTableField = container.GetType().GetRuntimeFields().FirstOrDefault(x => x.Name == "m_domainTable");
谁能帮我解决这个问题?谢谢 ;)
编辑:我也尝试检查 if 是否为 null,但我仍然遇到同样的错误,
if (container.GetType().GetRuntimeFields().FirstOrDefault(x => x.Name == "m_domainTable") != null)
您可能会在 return 中得到一个空值。然后获取名称 属性 将引发异常。您必须先检查空值。
var domainTableFielObject = container.GetType().GetRuntimeFields().FirstOrDefault() ;
If(domainTableFielObject! =null)
domainTableField =domainTableFielObject. Where(x => x.Name == "m_domainTable");
我用这个简单的代码解决了我自己的问题:
foreach (var c in GetAuth_Response.Cookies)
{
Cookies.Add(new Cookie(c.Name, c.Value, c.Path, c.Domain));
}
如果你有同样的问题,替换整个GetAllCookies,就用这个^
所以,我正在向 Twitter (RestSharp) 发出 GET 请求,我想收集所有 cookie,然后放入 cookiecollection,首先我进行 get:
RestClient client = new RestClient("https://mobile.twitter.com");
RestRequest GetAuth = new RestRequest("/login");
var GetAuth_Response = client.Get(GetAuth);
现在,我想得到饼干,我这样做:
CookieCollection Cookies = GetAllCookies(client.CookieContainer);
>
public static CookieCollection GetAllCookies(CookieContainer container)
{
var allCookies = new CookieCollection();
var domainTableField = container.GetType().GetRuntimeFields().FirstOrDefault(x => x.Name == "m_domainTable");
var domains = (IDictionary)domainTableField.GetValue(container);
foreach (var val in domains.Values)
{
var type = val.GetType().GetRuntimeFields().First(x => x.Name == "m_list");
var values = (IDictionary)type.GetValue(val);
foreach (CookieCollection cookies in values.Values)
{
allCookies.Add(cookies);
}
}
return allCookies;
}
现在,当我 运行 程序时,我得到这个错误:
Additional information: Object reference not set to an instance of an object.
在这部分:
var domainTableField = container.GetType().GetRuntimeFields().FirstOrDefault(x => x.Name == "m_domainTable");
谁能帮我解决这个问题?谢谢 ;)
编辑:我也尝试检查 if 是否为 null,但我仍然遇到同样的错误,
if (container.GetType().GetRuntimeFields().FirstOrDefault(x => x.Name == "m_domainTable") != null)
您可能会在 return 中得到一个空值。然后获取名称 属性 将引发异常。您必须先检查空值。
var domainTableFielObject = container.GetType().GetRuntimeFields().FirstOrDefault() ;
If(domainTableFielObject! =null)
domainTableField =domainTableFielObject. Where(x => x.Name == "m_domainTable");
我用这个简单的代码解决了我自己的问题:
foreach (var c in GetAuth_Response.Cookies)
{
Cookies.Add(new Cookie(c.Name, c.Value, c.Path, c.Domain));
}
如果你有同样的问题,替换整个GetAllCookies,就用这个^