gpt4 book ai didi

c# - System.Net.Http : missing from namespace?(使用.net 4.5)

转载 作者:IT王子 更新时间:2023-10-29 03:36:34 26 4
gpt4 key购买 nike

TL; DR:我是这门语言的新手,不知道自己在做什么

到目前为止,这是我的类(class):

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 时,出现以下错误:

The type or namespace name 'Http' does not exist in the namespace 'System.Net' (are you missing an assembly reference?)

最佳答案

HttpClient住在System.Net.Http命名空间。

您需要添加:

using System.Net.Http;

并确保您在 .NET 4.5 中引用 System.Net.Http.dll


发布的代码似乎没有对 webClient 做任何事情。使用 HttpWebRequest 实际编译的代码有问题吗?


更新

要打开添加引用对话框,请在解决方案资源管理器中右键单击您的项目,然后选择添加引用...。它应该看起来像:

enter image description here

关于c# - System.Net.Http : missing from namespace?(使用.net 4.5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9611316/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com