基于此处的答案: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/