在 WebClient.UploadStringTaskAsync 之后获取响应 cookie
Get response cookies after WebClient.UploadStringTaskAsync
WebClient重载后class
public class CustomWebClient : WebClient
{
public CustomWebClient()
{
Cookies = new CookieCollection();
CookieContainer = new CookieContainer();
}
public CookieContainer CookieContainer { get; }
public CookieCollection Cookies { get; private set; }
protected override WebRequest GetWebRequest(Uri address)
{
var request = (HttpWebRequest)base.GetWebRequest(address);
request.CookieContainer = CookieContainer;
return request;
}
protected override WebResponse GetWebResponse(WebRequest request)
{
var response = (HttpWebResponse)base.GetWebResponse(request);
Cookies = response.Cookies;
return response;
}
}
我可以使用 client.UploadString
获取 cookie
public string Test()
{
using (var client = new CustomWebClient())
{
//...
client.UploadString("example.com", "json");
return client.Cookies
}
}
但是如何使用 client.UploadStringTaskAsync 获取 cookie?
public async Task<string> Test()
{
using (var client = new CustomWebClient())
{
//...
await client.UploadStringTaskAsync("example.com", "json");
return client.Cookies // There are no cookies!
}
}
您觉得需要更多解释吗?
因为 Whosebug 警告说:
看起来你的 post 主要是代码;请添加更多详细信息。
我通过覆盖 GetWebResponse(WebRequest 请求,IAsyncResult 结果)解决了这个问题
public class CustomWebClient : WebClient
{
public CustomWebClient()
{
Cookies = new CookieCollection();
CookieContainer = new CookieContainer();
}
public CookieContainer CookieContainer { get; }
public CookieCollection Cookies { get; private set; }
protected override WebRequest GetWebRequest(Uri address)
{
var request = (HttpWebRequest)base.GetWebRequest(address);
request.CookieContainer = CookieContainer;
return request;
}
protected override WebResponse GetWebResponse(WebRequest request)
{
var response = (HttpWebResponse)base.GetWebResponse(request);
Cookies = response.Cookies;
return response;
}
protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)
{
var response = (HttpWebResponse)base.GetWebResponse(request);
Cookies = response.Cookies;
return response;
}
}
WebClient重载后class
public class CustomWebClient : WebClient
{
public CustomWebClient()
{
Cookies = new CookieCollection();
CookieContainer = new CookieContainer();
}
public CookieContainer CookieContainer { get; }
public CookieCollection Cookies { get; private set; }
protected override WebRequest GetWebRequest(Uri address)
{
var request = (HttpWebRequest)base.GetWebRequest(address);
request.CookieContainer = CookieContainer;
return request;
}
protected override WebResponse GetWebResponse(WebRequest request)
{
var response = (HttpWebResponse)base.GetWebResponse(request);
Cookies = response.Cookies;
return response;
}
}
我可以使用 client.UploadString
获取 cookiepublic string Test()
{
using (var client = new CustomWebClient())
{
//...
client.UploadString("example.com", "json");
return client.Cookies
}
}
但是如何使用 client.UploadStringTaskAsync 获取 cookie?
public async Task<string> Test()
{
using (var client = new CustomWebClient())
{
//...
await client.UploadStringTaskAsync("example.com", "json");
return client.Cookies // There are no cookies!
}
}
您觉得需要更多解释吗?
因为 Whosebug 警告说:
看起来你的 post 主要是代码;请添加更多详细信息。
我通过覆盖 GetWebResponse(WebRequest 请求,IAsyncResult 结果)解决了这个问题
public class CustomWebClient : WebClient
{
public CustomWebClient()
{
Cookies = new CookieCollection();
CookieContainer = new CookieContainer();
}
public CookieContainer CookieContainer { get; }
public CookieCollection Cookies { get; private set; }
protected override WebRequest GetWebRequest(Uri address)
{
var request = (HttpWebRequest)base.GetWebRequest(address);
request.CookieContainer = CookieContainer;
return request;
}
protected override WebResponse GetWebResponse(WebRequest request)
{
var response = (HttpWebResponse)base.GetWebResponse(request);
Cookies = response.Cookies;
return response;
}
protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)
{
var response = (HttpWebResponse)base.GetWebResponse(request);
Cookies = response.Cookies;
return response;
}
}