问题描述
我试图让使用Web客户端异步HTTP GET请求,但是,注册的回调不会被调用。我也试着与同步之一,它工作得很好。我究竟做错了什么?
Web客户端asyncWebRequest;
公共AsyncWebRequest(URI链接)
{
asyncWebRequest =新的Web客户端();
URL =新的URI(http://www.google.com/);
//字符串测试= asyncWebRequest.DownloadString(URL); //这个工程
asyncWebRequest.DownloadStringCompleted + =新DownloadStringCompletedEventHandler(asyncWebRequest_DownloadStringCompleted);
asyncWebRequest.DownloadStringAsync(URL);
}
无效asyncWebRequest_DownloadStringCompleted(对象发件人,DownloadStringCompletedEventArgs E)
{
抛出新的NotImplementedException();
}
也许是因为你处置 Web客户端
才下载完。在code执行不上停止asyncWebRequest.DownloadStringAsync(URL);
和你处置 Web客户端
对象通过关闭using语句。
尝试处置 Web客户端
在 asyncWebRequest_DownloadStringCompleted
。
结果
I'm trying to make an asynchronous HTTP GET request using Webclient, however, the registered callback never gets called. I've also tried with the sync one, and it worked fine. What am I doing wrong?
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();
}
Maybe because you disposing the WebClient
before it finished downloading. The code execution don't stop on asyncWebRequest.DownloadStringAsync(url);
and you are disposing the WebClient
object by closing the using statement.
try to dispose the WebClient
on asyncWebRequest_DownloadStringCompleted
.
results
这篇关于Web客户端的DownloadStringCompleted事件处理程序永远不会调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!