【问题标题】:WebClient could not be found找不到 WebClient
【发布时间】:2016-06-16 22:41:48
【问题描述】:

我已经在 Stack Overflow(和 google)上搜索过,但找不到解决我问题的具体答案。

我想从页面中读取一些内容。我尝试使用Webclient,但这给了我这个错误:

找不到类型或命名空间名称“WebClient”(您 缺少 using 指令或程序集引用?)

我尝试在谷歌上搜索如何解决这个错误,但没有找到正确的答案(我也尝试过HttpClient,结果相同)。

我如何确保获得他特定页面的内容?

顺便说一句,这就是我现在拥有的:

WebClient client = new WebClient();
// Use google as test page
string downloadString = client.DownloadString("http://www.gooogle.com");

我正在使用 Visual Studio 2015 Community 和 ASP.Net v5

【问题讨论】:

  • 您是否将using System.Net; 添加到您的using 指令中?
  • @S.Akbari 是的,我试过了。然后我得到错误:A using clause must precede all other elements defined in the namespace except extern alias declarations
  • 好的。可能你没有把它放在文件的开头!
  • @S.Akbari 我做了(请参阅答案中的评论)

标签: c# asp.net .net webclient dotnet-httpclient


【解决方案1】:

确保您在项目中引用了System.dll

还可以将using System.Net; 包含到您计划使用WebClient 的源代码的 usings 指令部分,或使用其完全限定名称,即:

var client = new System.Net.WebClient()

【讨论】:

  • 谢谢! var client = new System.Net.WebClient() 完美!但是你也知道为什么using System.Net;给我这个错误吗?:A using clause must precede all other elements defined in the namespace except extern alias declarations
  • @MZeinstra using 指令应位于文件的开头。你把它放在哪里了?
  • 我已将此<script runat="server">using System.Net;</script> 放在我的 .aspx 文件的最顶部
  • @MZeinstra 嗯...你提到了 ASP.NET v5 所以aspx 是我想到的最后一件事。在 apsx 文件中,您必须改用 <%@ Import Namespace="System.Net" %>
  • 啊,好吧,对不起。这很好用:D 那我用什么?你觉得呢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-18
  • 1970-01-01
  • 2020-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多