c# - Problems downloading web pages
问题描述
In one controller (MVC ASP.NET) I need to download some web pages, and analyze them. Currently I'm using Visual Studio 2019, and .NET Framework 4.8.
My code is like this (simplified):
public async void GetHtmlStream(Uri urlAddr)
{
HttpClient client = new HttpClient();
using (HttpResponseMessage Resp = client.GetAsync(urlAddr).Result)
{
using (HttpContent content = Resp.Content)
{
Stream stream = await content.ReadAsStreamAsync().Result;
}
}
}
The line Stream stream = await content.ReadAsStreamAsync().Result;
does not compile:
"Error CS1061 'Stream' does not contain definition for 'GetAwaiter' ..."
In a test program, avoiding async and await, i have a line like this:
string result = content.ReadAsStringAsync().Result;
and all works fine.
In the controller, of course nothing works.
I saw a lot of similar issues but I don't understand what should I do to solve the problem.
解决方案
尝试
Stream stream = await content.ReadAsStreamAsync();
反而。
推荐阅读
- google-api - 在进行 postgresql 查询后调用谷歌表格时 Lambda 函数超时
- python-3.x - python os.getenv('APPDATA') 返回 none - os.environ 没有在数据字典中列出 APPDATA
- angular - 角度离子http请求将空数据发送到rest API
- git - 无法将更改推送到 GitHub
- http - 通过 http 发送文件而不实际创建任何文件
- c++11 - 将 Rcpp::List 转换为 const char* 向量的 C++ 向量
- python - 如何在不冻结 UI 的情况下使用 QProcess 循环的输出更新 UI?
- java - 将 java.util.Date 转换为 java.sql.Date 导致日期更改
- postgresql - Postgres - 基于列值的不同聚合
- javascript - 从悬停到点击的下拉菜单