我正在尝试使用 Webclient 发出异步 HTTP GET 请求,但是,注册的回调永远不会被调用。我也试过同步的,效果很好。我做错了什么?
WebClient asyncWebRequest;
public AsyncWebRequest(Uri url)
{
asyncWebRequest = new WebClient();
url = new Uri("http://www.google.com/");
// string test = asyncWebRequest.DownloadString(url); // this works
asyncWebRequest.DownloadStringCompleted += new DownloadStringCompletedEventHandler(asyncWebRequest_DownloadStringCompleted);
asyncWebRequest.DownloadStringAsync(url);
}
void asyncWebRequest_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
throw new NotImplementedException();
}
最佳答案
可能是因为您在 WebClient
完成下载之前对其进行了处理。代码执行不会在 asyncWebRequest.DownloadStringAsync(url);
上停止,您将通过关闭 using 语句来处理 WebClient
对象。
尝试在 asyncWebRequest_DownloadStringCompleted
上配置 WebClient
。
结果
关于c# - Webclient 的 DownloadStringCompleted 事件处理程序从未调用过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7373172/