asp.net - 如何在我的 .NET Framework 4.5 应用程序/站点中使用 HttpClient?

标签 asp.net http razor asp.net-4.5 dotnet-httpclient

基于此处的答案:How can I retrieve and parse just the html returned from an URL?

...我正尝试根据此处找到的代码添加代码:http://msdn.microsoft.com/en-us/library/system.net.http.httpclient.aspx

...即将此添加到\App_Code\Functions.cshtml:

@functions
{
    public static string GetUrlHtml(string dynamicUrl)
    {
        HttpClient client = new HttpClient();
        string body = await client.GetStringAsync(dynamicUrl);
    // parse it using HTML Agility Pack? (http://htmlagilitypack.codeplex.com/)
    }
}

HttpClient 未被识别,并且不提供“解析”上下文菜单项。输入后 Intellisense 不提供“Http”:

@using System.Net.

HttpClient 真的对我不可用吗?如果是这样,我能得到什么作为安慰奖?像这样使用 WebClient 是我的最佳选择:

WebClient wc = new WebClient();
string body = wc.DownloadString(dynamicUrl);
// parse it with html agility pack

...或者,如 https://web.archive.org/web/20211020001935/https://www.4guysfromrolla.com/articles/011211-1.aspx#postadlink 所示,我可以使用 HTML Agility Pack 中的 webGet 类:

var webGet = new HtmlWeb();
var document = webGet.Load(dynamicUrl);

有人对哪个选项最好有任何支持意见吗?

最佳答案

错误的答案是接受的。

这是正确的:

添加到配置

<system.web>
    <compilation>
        <assemblies>
            <add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        </assemblies>
    </compilation>
</system.web>

关于asp.net - 如何在我的 .NET Framework 4.5 应用程序/站点中使用 HttpClient?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16909638/

相关文章:

asp.net - MS Exchange powershell命令创建了错误的电子邮件ID

c# - 在按钮中添加 OnClientClick 时必填字段验证器不起作用

asp.net - 点网核心 : Object reference not set to an instance of an object

python - 非英文文本python不可读

c# - 向下拉列表框添加新值

http - 为什么 HTTP 的使用率远高于 HTTPS?

http - Angular2 HTTP 使用 observables 订阅显示数据未定义

c# - 动态数量的文本框(输入)MVC

asp.net-mvc - 为什么在 Razor 中使用 Html.RenderAction 时会出现 StackOverflowException?

asp.net-mvc - ASP.NET MVC发布不会覆盖CSHTML文件