我試圖使用Webclient進行異步HTTP GET請求,但是,註冊回調永遠不會被調用。我也嘗試過同步,它運行良好。我究竟做錯了什麼?Webclient的DownloadStringCompleted事件處理程序從來沒有調用
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();
}
我猜你是在一個控制檯應用程序測試這一點,您的下載完成之前退出'Main'? – BrokenGlass
Eh。謝謝,這是問題所在。 – norbip