C# System.Net.Http:命名空间中缺少?(使用 .net 4.5)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9611316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
System.Net.Http: missing from namespace? (using .net 4.5)
提问by NullVoxPopuli
TL; DR: I'm new to this language and have no idea what I'm doing
TL; DR:我是这门语言的新手,不知道我在做什么
here is my class so far:
到目前为止,这是我的课程:
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Web;
using System.Net;
using System.IO;
public class MyClass
{
private const string URL = "https://sub.domain.com/objects.json?api_key=123";
private const string data = @"{""object"":{""name"":""Title""}}";
public static void CreateObject()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = data.Length;
StreamWriter requestWriter = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
requestWriter.Write(data);
requestWriter.Close();
try
{
// get the response
WebResponse webResponse = request.GetResponse();
Stream webStream = webResponse.GetResponseStream();
StreamReader responseReader = new StreamReader(webStream);
string response = responseReader.ReadToEnd();
responseReader.Close();
}
catch (WebException we)
{
string webExceptionMessage = we.Message;
}
catch (Exception ex)
{
// no need to do anything special here....
}
}
static void Main(string[] args)
{
MyClass.CreateObject();
}
}
when I do csc filename.cs, I get the following error:
当我执行 csc filename.cs 时,出现以下错误:
The type or namespace name 'Http' does not exist in the namespace 'System.Net' (are you missing an assembly reference?)
命名空间“System.Net”中不存在类型或命名空间名称“Http”(您是否缺少程序集引用?)
采纳答案by M.Babcock
HttpClientlives in the System.Net.Httpnamespace.
HttpClient存在于System.Net.Http命名空间中。
You'll need to add:
您需要添加:
using System.Net.Http;
And make sure you are referencing System.Net.Http.dllin .NET 4.5.
并确保您System.Net.Http.dll在 .NET 4.5中引用。
The code posted doesn't appear to do anything with webClient. Is there something wrong with the code that is actually compiling using HttpWebRequest?
发布的代码似乎对webClient. 实际编译使用的代码有问题HttpWebRequest吗?
Update
更新
To open the Add Referencedialog right-click on your project in Solution Explorerand select Add Reference.... It should look something like:
要打开“添加引用”对话框,请在解决方案资源管理器中右键单击您的项目,然后选择“添加引用...”。它应该看起来像:


回答by Keith Nicholas
You'll need a using System.Net.Httpat the top.
你需要一个using System.Net.Http在顶部。
回答by Kibbee
HttpClientis new in .net 4.5. You should probably be using HttpWebRequest.
HttpClient是 .net 4.5 中的新增功能。您可能应该使用HttpWebRequest。
回答by Akhil
You need to have a Reference to the System.Web.Httpassembly which has the HTTPClientclass, your trying to use. Try adding the below line before your class declaration
您需要对具有您尝试使用System.Web.Http的HTTPClient类的程序集的引用。尝试在类声明之前添加以下行
using System.Web.Http;
If you still get the Error, try doing this in Visual Studio
如果您仍然收到错误,请尝试在 Visual Studio
- Right click on the References folder on your project.
- Select Add Reference.
- Select the .NET tab (or select the Browse button if it is not a .NET Framework assembly).
- Double-click the assembly containing the namespace in the error message (
System.Web.Http.dll). - Press the OK button.
- 右键单击项目上的 References 文件夹。
- 选择添加引用。
- 选择 .NET 选项卡(如果不是 .NET Framework 程序集,则选择浏览按钮)。
- 双击错误消息 (
System.Web.Http.dll) 中包含命名空间的程序集。 - 按确定按钮。
回答by user1662812
NuGet > Microsoft ASP.NET Web API Client Libraries
NuGet > Microsoft ASP.NET Web API 客户端库
回答by Ussabin
Assuming that your using Visual Studio 10, you can download an install that includes System.Net.Http, for Visual Studio 10 here: download MVC4 for VS10
假设您使用的是 Visual Studio 10,您可以在此处为 Visual Studio 10下载包含 System.Net.Http 的安装: download MVC4 for VS10
Once you've installed it, right click on the Referencesfolder in the VS Project and then select Add Reference. Then, select the Browsetab. Navigate to the assembliesinstall path for the MVC4 install (usually in Program Files(x86)/Microsoft ASP.NET/ASP.NET MVC4/assemblies) and select the assembly named 'System.Net.Http.dll'. Now you can add your 'using System.Net.Http' at the top of your code and begin creating HttpClient connections.
安装后,右键单击VS 项目中的References文件夹,然后选择Add Reference。然后,选择浏览选项卡。导航到MVC4 安装的程序集安装路径(通常在 Program Files(x86)/Microsoft ASP.NET/ASP.NET MVC4/assemblies 中)并选择名为“System.Net.Http.dll”的程序集。现在,您可以在代码顶部添加“使用 System.Net.Http”并开始创建 HttpClient 连接。
回答by boakye_wozniac
To solve the problem :
解决问题:
- Go to your solution explorer.
- Right click on the project name and choose add
- Select references and allow the .
Net framework 4.5to finish loading - Scroll down and select
System.Net.Httpand click ok.
- 转到您的解决方案资源管理器。
- 右键单击项目名称并选择添加
- 选择引用并允许 .
Net framework 4.5完成加载 - 向下滚动并选择
System.Net.Http并单击确定。
Problem solved.
问题解决了。
回答by LagiNatoRRR
How I solved it.
我是怎么解决的。
- Open project (!) "Properties", choose "Application", select targeting framework ".Net Framework 4.5"
- Right click on your project -> Add reference
- Make sure that in "Assemblies" -> "Extensions" option "System.Net.Http" is unchecked
- Go to "Assemblies" -> "Framework" and select "System.Net.Http" and "System.Net.Http" options
- That`s all!
- 打开项目(!)“属性”,选择“应用程序”,选择目标框架“.Net Framework 4.5”
- 右键单击您的项目 -> 添加引用
- 确保在“程序集”->“扩展”选项中“System.Net.Http”没有被选中
- 转到“程序集”->“框架”并选择“System.Net.Http”和“System.Net.Http”选项
- 仅此而已!
In my case i had at the beginning .Net 4.0 and "Assemblies" -> "Extensions" option "System.Net.Http" with version 2.0.0.0. After my actions "Assemblies" -> "Framework" options "System.Net.Http" and "System.Net.Http" had the same 4.0.0.0 version.
就我而言,我一开始有 .Net 4.0 和“程序集”->“扩展”选项“System.Net.Http”,版本为 2.0.0.0。在我操作“程序集”->“框架”选项后,“System.Net.Http”和“System.Net.Http”具有相同的 4.0.0.0 版本。
回答by lance-p
Visual Studio Package Manager Console > Install-Package Microsoft.AspNet.WebApi.Client
Visual Studio 包管理器控制台 > 安装包 Microsoft.AspNet.WebApi.Client

