c# - Webclient 的 DownloadStringCompleted 事件处理程序从未调用过

标签 c# .net webclient

我正在尝试使用 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

结果

enter image description here

关于c# - Webclient 的 DownloadStringCompleted 事件处理程序从未调用过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7373172/

相关文章:

.net - 如何确定winform中给定颜色的禁用颜色?

c# - DownloadStringAsync 在第一次调用时阻塞线程 14 秒

C# ASP.NET MVC 5 如何执行原始查询?

c# - 在wpf中的两个按钮之间绘制线

.net - Array.GetLowerBound(int) 的目的是什么?

.net - 在 HSM 上使用私钥的 SSL

c# - WebClient.DownloadString(url) 不适用于第二个参数

c# - 简单 WebClient 仅按名称不工作 : Customized cultures cannot be passed by LCID,

c# - 如何通过将字典值作为过滤器参数来使用 C# 驱动程序查询 MongoDb 集合

c# - 如何在 Windows 应用程序中显示进度条?