我试图下载放在互联网上的RSS源的XML源代码,但是当我的进程到达WebResponse
语句(第二行下面)时,进程停止,并且没有任何下一步发生。没有错误,没有例外,或者没有这样的。我等了几十分钟,但仍然没有发生任何事情。
WebRequest request = WebRequest.Create(source.Url);
WebResponse response = await request.GetResponseAsync(); // at this line it stops
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream);
string xml = readStream.ReadToEnd().Trim();
readStream.Dispose();
response.Dispose();
知道导致它的原因吗?
编辑:
public static async Task<string> GetContent(string uri)
{
WebRequest request = WebRequest.Create(url);
using (WebResponse response = await request.GetResponseAsync().ConfigureAwait(false))
{
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream);
return readStream.ReadToEnd().Trim();
}
}
public async Task<ObservableCollection<Source>> GetArticlesFromSource()
{
sourceDefinitions = await GetSourceDefinitions();
string imageFolderName = "ArticleImages";
string imageFolderPath = localFolder.Path + "\\" + imageFolderName;
StorageFolder imageFolder = await localFolder.CreateFolderAsync(imageFolderName, CreationCollisionOption.ReplaceExisting);
foreach (var source in sourceDefinitions)
{
if (source.Selected == "true")
{
ObservableCollection<Article> articlesStep1 = new ObservableCollection<Article>();
/*WebRequest request = WebRequest.Create(source.Url);
WebResponse response = await request.GetResponseAsync();
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream);*/
string xml = await GetContent("http://www.honzachalupa.cz/").ConfigureAwait(false);
Debug.WriteLine(xml);
...
答案 0 :(得分:1)
您的应用程序几乎肯定会在调用堆栈上方的返回任务上调用Wait
或Result
,并且我在我的博客上完整地解释了此will cause a deadlock。
要解决此问题,请找到上游Wait
/ Result
来电并将其更改为await
。换句话说,一直使用&#34; async&#34;。