TL; DR:我是这门语言的新手,不知道自己在做什么
到目前为止,这是我的课程:
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 时,出现以下错误:
命名空间“System.Net”中不存在类型或命名空间名称“Http”(是否缺少程序集引用?)
HttpClient住在System.Net.Http命名空间。
您需要添加:
using System.Net.Http;
并确保您正在引用System.Net.Http.dll
在 .NET 4.5 中。
发布的代码似乎没有做任何事情webClient
。实际编译的代码是否有问题HttpWebRequest
?
Update
要打开添加参考对话框中右键单击您的项目解决方案浏览器并选择添加参考...。它应该看起来像:
![enter image description here](https://i.stack.imgur.com/XVEoQ.png)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)