我正在尝试使用 Windows 8 应用程序的异步功能下载和解析网站。
我在这个方法中调用下载代码:
private async Task<Show> getSeasonAndEpisodeInformation(Show currentShow)
{
int seriesID = currentShow.SeriesID;
EpisodeBuilder epBuilder = new EpisodeBuilder(seriesID);
List<Episode> eps = await epBuilder.getEpisodeList();
SeasonBuilder seasonBuilder = new SeasonBuilder(eps);
return currentShow;
}
getEpisodeList() 函数是:
public async Task<List<Episode>> getEpisodeList()
{
httpClient = new HttpClient();
httpClient.MaxResponseContentBufferSize = 2560000;
httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
try
{
List<Episode> episodes = new List<Episode>();
HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://www.example.com"));
response.EnsureSuccessStatusCode();
//Never get here
return episodesList;
}
catch (Exception)
{
}
return null;
}
如果我的 getEpisodeList 方法无效,那么网页将下载并继续。我已经调试并确保它是永远不会完成的 httpClient.GetAsync(..) 行。有没有办法解决?
在 getSeasonAndEpisodeInformation 方法中,我需要确保 getEpisodeList() 方法在继续之前已返回 - 因此在调用 epBuilder.getEpisodeList() 时使用 await 关键字。