Web客户端的DownloadStringCompleted事件处理程序永远不会调用永远不会、客户端、事件、程序

由网友(-爱我你怕了吗,)分享简介:我试图让使用Web客户端异步HTTP GET请求,但是,注册的回调不会被调用。我也试着与同步之一,它工作得很好。我究竟做错了什么?Web客户端asyncWebRequest;公共AsyncWebRequest(URI链接){asyncWebRequest =新的Web客户端();URL =新的URI(http://...

我试图让使用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

阅读全文

相关推荐

最新文章