System.Net.Http:从命名空间的缺失? (使用.NET 4.5) [英] System.Net.Http: missing from namespace? (using .net 4.5)
问题描述
TL; DR:我是新来这个语言,也不知道我在做什么。
这是我的班,到目前为止:
使用系统;
使用System.Collections.Generic;
使用System.Net.Http;
使用的System.Web;
使用System.Net;
使用System.IO;公共类MyClass的
{
私人常量字符串URL =https://sub.domain.com/objects.json?api_key=123;
私人常量字符串数据= @{对象:{名:标题}}; 公共静态无效的CreateObject()
{
HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(URL);
request.Method =POST;
request.ContentType =应用/ JSON;
request.ContentLength = data.Length;
StreamWriter的requestWriter =新的StreamWriter(request.GetRequestStream(),System.Text.Encoding.ASCII);
requestWriter.Write(数据);
requestWriter.Close(); 尝试
{
//得到响应
WebResponse类WebResponse类= request.GetResponse();
流webStream = webResponse.GetResponseStream();
StreamReader的responseReader =新的StreamReader(webStream);
串响应= responseReader.ReadToEnd();
responseReader.Close();
}
赶上(引发WebException我们)
{
字符串webExceptionMessage = we.Message;
}
赶上(异常前)
{
//没有必要在这里做什么特别的事情....
}
} 静态无效的主要(字串[] args)
{
MyClass.CreateObject();
}
}
当我做CSC filename.cs,我得到以下错误:
类型或命名空间名称'HTTP'在命名空间System.Net不存在(是否缺少程序集引用?)
块引用>解决方案<一个href=\"https://msdn.microsoft.com/en-us/library/system.net.http.httpclient(v=vs.110).aspx\"><$c$c>HttpClient$c$c>住在<一个href=\"https://msdn.microsoft.com/en-us/library/system.net.http(v=vs.110).aspx\"><$c$c>System.Net.Http$c$c>命名空间。
您需要添加:
使用System.Net.Http;
和确保您引用
System.Net.Http.dll
在.NET 4.5。在code贴不出现做
Web客户端
东西。是不是有什么毛病code,它是用实际编制的HttpWebRequest
?更新
要打开的添加引用的对话框中您的项目中的解决方案管理器的右键单击并选择的添加引用... 的。它看起来应该是这样的:
TL; DR: I'm new to this language and have no idea what I'm doing
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:
The type or namespace name 'Http' does not exist in the namespace 'System.Net' (are you missing an assembly reference?)
解决方案
HttpClient
lives in theSystem.Net.Http
namespace.You'll need to add:
using System.Net.Http;
And make sure you are referencing
System.Net.Http.dll
in .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 usingHttpWebRequest
?
Update
To open the Add Reference dialog right-click on your project in Solution Explorer and select Add Reference.... It should look something like:
这篇关于System.Net.Http:从命名空间的缺失? (使用.NET 4.5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!