我是这个语言的新手,不知道我在做什么
到目前为止,这是我的类:
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();
}
}
当我执行csc filename.cs时,我得到以下错误:
Http类型或命名空间名称‘
’在命名空间'System.Net‘中不存在(是否缺少程序集引用?)
发布于 2012-03-08 08:38:16
HttpClient
位于System.Net.Http
名称空间中。
您需要添加:
using System.Net.Http;
并确保您引用的是.NET 4.5中的System.Net.Http.dll
。
发布的代码似乎与webClient
没有任何关系。实际使用HttpWebRequest
编译的代码是否有问题?
更新
若要打开“添加引用”对话框,请在“解决方案资源管理器”中右键单击项目,然后选择“添加引用...”。它应该看起来像这样:
发布于 2012-12-02 19:25:52
NuGet > Microsoft.AspNet.WebApi.Client包
发布于 2015-06-04 20:11:02
我是怎么解决的。
<代码>G211
在我的例子中,我在开始时使用了2.0.0.0版本的.Net 4.0和“汇编”->“扩展”选项"System.Net.Http“。在我的操作之后,“程序集”和“框架”选项"System.Net.Http“和"System.Net.Http”具有相同的4.0.0.0版本。
https://stackoverflow.com/questions/9611316
复制相似问题