gpt4 book ai didi

c# - WebClient 似乎不起作用?

转载 作者:行者123 更新时间:2023-11-30 17:17:44 25 4
gpt4 key购买 nike

我有以下代码:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com