C# 如何从HttpClientHandler.CookieContainer获取cookies
代码如下:C# 如何从HttpClientHandler.CookieContainer获取cookies,c#,.net,windows-runtime,C#,.net,Windows Runtime,代码如下: public static async Task<string> DownloadPageWithCookiesAsync(string url) { HttpClientHandler handler = new HttpClientHandler(); handler.UseDefaultCredentials = true; handler.AllowAutoRedirect = true; handler.UseCookies =
public static async Task<string> DownloadPageWithCookiesAsync(string url)
{
HttpClientHandler handler = new HttpClientHandler();
handler.UseDefaultCredentials = true;
handler.AllowAutoRedirect = true;
handler.UseCookies = true;
handler.CookieContainer = new CookieContainer();
HttpClient client = new HttpClient(handler);
HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
string responseBody = response.Content.ReadAsString();
return responseBody;
}
公共静态异步任务下载页面WithCookiesAsync(字符串url)
{
HttpClientHandler handler=新的HttpClientHandler();
handler.UseDefaultCredentials=true;
handler.AllowAutoRedirect=true;
handler.UseCookies=true;
handler.CookieContainer=新CookieContainer();
HttpClient=新的HttpClient(处理程序);
HttpResponseMessage response=wait client.GetAsync(url);
response.EnsureSuccessStatusCode();
string responseBody=response.Content.ReadAsString();
返回响应体;
}
在
client.GetAsync(url)之后代码>运行时,处理程序.CookieContainer
包含7个cookie。如何访问它们?使用CookieContainer的GetCookies方法,指定您想要Cookie的URI。它返回您可以枚举的CookieCollection。尝试:
CookieCollection cookies = handler.CookieContainer.GetCookies(new Uri(/*Uri that the cookies are associated with*/));
for(int i = 0; i < cookies.Count; i++)
{
Cookie c = cookies[i];
//Do stuff with the cookie.
}
CookieCollection cookies=handler.CookieContainer.GetCookies(新Uri(/*与Cookie关联的Uri*/);
对于(int i=0;i
我相信,您还可以使用foreach循环迭代CookieCollection。intloop1,loop2;
int loop1, loop2;
HttpCookieCollection MyCookieColl;
HttpCookie MyCookie;
MyCookieColl = Request.Cookies;
// Capture all cookie names into a string array.
String[] arr1 = MyCookieColl.AllKeys;
// Grab individual cookie objects by cookie name.
for (loop1 = 0; loop1 < arr1.Length; loop1++)
{
MyCookie = MyCookieColl[arr1[loop1]];
Response.Write("Cookie: " + MyCookie.Name + "<br>");
Response.Write ("Secure:" + MyCookie.Secure + "<br>");
//Grab all values for single cookie into an object array.
String[] arr2 = MyCookie.Values.AllKeys;
//Loop through cookie Value collection and print all values.
for (loop2 = 0; loop2 < arr2.Length; loop2++)
{
Response.Write("Value" + loop2 + ": " + Server.HtmlEncode(arr2[loop2]) + "<br>");
}
}
HttpCookieCollection mycokieColl;
HttpCookie-Mycokie;
MyCookieColl=Request.Cookies;
//将所有cookie名称捕获到字符串数组中。
字符串[]arr1=mycokiecoll.AllKeys;
//按cookie名称获取各个cookie对象。
for(loop1=0;loop1”;
响应。写入(“安全:+mycokie.Secure+”
”;
//将单个cookie的所有值捕获到对象数组中。
字符串[]arr2=mycokie.Values.AllKeys;
//循环执行cookie值集合并打印所有值。
for(loop2=0;loop2”;
}
}
CookieCollection cookies=handler.CookieContainer.GetCookies(/*blah blah*/);
foreach(cookies.OfType()中的var cookie)
{
//处理cookies
}
foreach语句无法对“System.Net.CookieContainer”类型的变量进行操作,因为“System.Net.CookieContainer”不包含“GetEnumerator”的公共定义。
使用for循环来获取它们?谢谢,但是循环什么?没有定义索引运算符。您误解了-不要在CookieContainer上使用foreach。更确切地说,foreach将忽略.GetCookies()调用的结果。返回一个具有迭代器的CookieCollection。
CookieCollection cookies = handler.CookieContainer.GetCookies(/*blah-blah*/);
foreach (var cookie in cookies.OfType<System.Net.Cookie>())
{
// process cookies
}