Skip to main content
 首页 » 编程设计

c#之如何将 cookie 从 System.Net.HttpWebResponse 传送到下一个 System.Net.HttpWebRequest

2024年01月13日30Leo_wl

我正在尝试执行一些自动网络请求,并且需要维护一个到下一个 cookie。我可以看到我正在从初始响应中取回我想要的 cookie,但我无法将它附加到下一个请求。

C#代码

// response part 
 
using (var wresp = (System.Net.HttpWebResponse)wrequest.GetResponse()) 
{ 
 
    // respblob gets returned and is accessible to the next request 
    respblob.CookieList = new List<System.Net.Cookie>();  
 
    foreach (System.Net.Cookie cook in wresp.Cookies) 
    { 
        respblob.CookieList.Add(cook); 
    } 
 
    // ... more stuff not related to cookies 
} 
 
 
// next request part 
   var wrequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url); 
 
    wrequest.Method = "POST"; 
    wrequest.ContentType = "application/x-www-form-urlencoded"; 
 
    wrequest.CookieContainer = new System.Net.CookieContainer(); 
 
    // request.CookieList contains one cookie as expected  
    // from the previous response 
    for (int j = 0; j < request.CookieList.Count; j++) 
    { 
        wrequest.CookieContainer.Add(request.CookieList[j]); 
    } 
 
 
// .... write data to request body 
 
// ... complete the request, etc 

这是两个请求/响应操作的记录交换。

要求:

GET http://domain.com/Login.aspx?ReturnUrl=%2fDefault.aspx HTTP/1.1 
Host: domain.com 
Connection: Keep-Alive 

回复:

HTTP/1.1 200 OK 
Date: Tue, 15 May 2012 17:17:52 GMT 
Server: Microsoft-IIS/6.0 
X-Powered-By: ASP.NET 
X-AspNet-Version: 2.0.50727 
Set-Cookie: ASP.NET_SessionId=zzz4fpb4alwi1du2yavx5tah; path=/; HttpOnly 
Cache-Control: private 
Content-Type: text/html; charset=utf-8 
Content-Length: 24408 
 
 
 
...html content... 

下一个请求:

POST http://domain.com/Login.aspx?ReturnUrl=%2fDefault.aspx HTTP/1.1 
Host: domain.com 
Content-Type: application/x-www-form-urlencoded 
Content-Length: 979 
Expect: 100-continue 
 
__LASTFOCUS=&__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=viewstateclipped&__EVENTVALIDATION=validationclipped&ctl00%2524ContentPlaceHolder1%2524Login1%2524LoginButton=&ctl00%2524ContentPlaceHolder1%2524Login1%2524UserName=my.email%40example.com&ctl00%2524ContentPlaceHolder1%2524Login1%2524Password=myPassword 

因此即使 cookie 存在于 HttpWebRequest CookieContainer 中,它也不会随请求一起发送。我做错了什么?

请您参考如下方法:

您应该为正在使用的两个 HttpWebRequest 对象使用相同的 CookieContainer 实例。只需创建一个 CookieContainer 实例一次:

var cookieContainer = new CookieContainer(); 

然后让你的两个请求对象都使用这个实例:

var request1 = (HttpWebRequest)WebRequest.Create("http://example.com/url1"); 
// assign the cookie container for the first request 
request1.CookieContainer = cookieContainer; 
... go ahead and send the request and process the response 
 
 
var request2 = (HttpWebRequest)WebRequest.Create("http://example.com/url2"); 
// reuse the same cookie container instance as the first request 
request2.CookieContainer = cookieContainer; 
... go ahead and send the request and process the response 

由于您对这两个请求使用相同的 CookieContainer,因此当第一个请求将 cookie 存储在此容器中时,cookie 将与第二个请求一起自动发出。这当然假设第二个请求与第一个请求是针对同一个域。

此外,由于 cookie 是一个 session cookie(HttpOnly 标志),您无法从客户端读取它的值。