Webclient 没有得到响应 uri
Webclient not getting response uri
我在 webclient
class 中发现了一个奇怪的行为。当我使用 UploadValues()
同步方法时,我通常得到响应 uri,但是当我使用异步版本 UploadValuesTaskAsync()
到相同的 url 时,它 returns 响应 uri 为空。为什么?
示例:
using (var client = new CustomWebClient())
{
await Get(client);
Get(client);
}
private async Task GetAsync(WebClient client)
{
var resAsync = await client.UploadValuesTaskAsync("http://address.com", requestData);
//client.ResponseUri null here
}
private void Get(WebClient client)
{
var res= client.UploadValues("http://address.com", requestData);
//client.ResponseUri **not** null here
}
在 GetAsync
方法中,client.ResponseUri
为空,而 UploadValues
.
不为空
编辑:
我发现 WebClient 是 "customized":
所以 class 是:
public class CustomWebClient : WebClient
{
public CookieContainer Cookies { get; private set; }
public Uri ResponseUri { get; private set; }
public CustomWebClient()
{
Cookies = new CookieContainer();
}
protected override WebRequest GetWebRequest(Uri address)
{
var request = base.GetWebRequest(address) as HttpWebRequest;
if (request == null)
{
return base.GetWebRequest(address);
}
request.CookieContainer = Cookies;
return request;
}
protected override WebResponse GetWebResponse(WebRequest request)
{
var response = base.GetWebResponse(request);
ResponseUri = response.ResponseUri;
return response;
}
}
我检查了在 synchronous
方法中它在继续到下一行之前点击了 GetWebResponse
方法。但在 asynchronous
版本中,它从未达到 GetWebResponse
。
您的自定义 class 仅覆盖同步 WebResponse GetWebResponse(WebRequest request)
方法的行为。
您需要为异步版本添加覆盖:
protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)
{
var response = base.GetWebResponse(request, result);
ResponseUri = response.ResponseUri;
return response;
}
我在 webclient
class 中发现了一个奇怪的行为。当我使用 UploadValues()
同步方法时,我通常得到响应 uri,但是当我使用异步版本 UploadValuesTaskAsync()
到相同的 url 时,它 returns 响应 uri 为空。为什么?
示例:
using (var client = new CustomWebClient())
{
await Get(client);
Get(client);
}
private async Task GetAsync(WebClient client)
{
var resAsync = await client.UploadValuesTaskAsync("http://address.com", requestData);
//client.ResponseUri null here
}
private void Get(WebClient client)
{
var res= client.UploadValues("http://address.com", requestData);
//client.ResponseUri **not** null here
}
在 GetAsync
方法中,client.ResponseUri
为空,而 UploadValues
.
编辑:
我发现 WebClient 是 "customized":
所以 class 是:
public class CustomWebClient : WebClient
{
public CookieContainer Cookies { get; private set; }
public Uri ResponseUri { get; private set; }
public CustomWebClient()
{
Cookies = new CookieContainer();
}
protected override WebRequest GetWebRequest(Uri address)
{
var request = base.GetWebRequest(address) as HttpWebRequest;
if (request == null)
{
return base.GetWebRequest(address);
}
request.CookieContainer = Cookies;
return request;
}
protected override WebResponse GetWebResponse(WebRequest request)
{
var response = base.GetWebResponse(request);
ResponseUri = response.ResponseUri;
return response;
}
}
我检查了在 synchronous
方法中它在继续到下一行之前点击了 GetWebResponse
方法。但在 asynchronous
版本中,它从未达到 GetWebResponse
。
您的自定义 class 仅覆盖同步 WebResponse GetWebResponse(WebRequest request)
方法的行为。
您需要为异步版本添加覆盖:
protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)
{
var response = base.GetWebResponse(request, result);
ResponseUri = response.ResponseUri;
return response;
}