c# - WebClient 似乎不起作用?

标签 c# windows-phone-7 webclient

我有以下代码:

WebClient client = new WebClient();
client.OpenReadAsync(new Uri("whatever"));
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);

和:

void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
  Stream reply = (Stream)e.Result;
  StreamReader s;
  s = new StreamReader(reply);
  this._code = s.ReadToEnd();
  s.Close();
}

在调试时我可以看到编译器没有进入 client_OpenReadCompleted 事件。错在哪里?我已经尝试使用 DownloadStringCompletedDownloadStringAsync 代替,但这也不起作用。

感谢您的帮助。

最佳答案

您的操作顺序不正确。

//create an instance of webclient
WebClient client = new WebClient();
//assign the event handler
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
//call the read method
client.OpenReadAsync(new Uri("whatever"));

关于c# - WebClient 似乎不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6282741/

相关文章:

windows-phone-7 - 'VisualTree' 设置不止一次

C#/.NET Webclient,等待页面加载完成

c# - 我如何将 linq 转换为 lambda

c# - ASP.Net MVC 4 Web API Controller 不适用于 Unity.WebApi

c# - 根据文本长度调整文本框和表单大小

c# - 内置光线追踪?

windows-phone-7 - 出于性能原因正确处理墓碑和返回键?

windows-phone-7 - PhoneApplicationPage 在 OnNavigatedTo 事件期间是否可见?

c# - 端口号和 URL

c# - 在 "await DownloadTaskAsync"上调用 WebClient.CancelAsync 时出现 WebException