首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >System.Net.Http:命名空间中缺少?(使用.net 4.5)

System.Net.Http:命名空间中缺少?(使用.net 4.5)
EN

Stack Overflow用户
提问于 2012-03-08 08:33:27
回答 16查看 370.5K关注 0票数 111

我是这个语言的新手,不知道我在做什么

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

代码语言:javascript
运行
复制
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‘中不存在(是否缺少程序集引用?)

EN

回答 16

Stack Overflow用户

回答已采纳

发布于 2012-03-08 08:38:16

HttpClient位于System.Net.Http名称空间中。

您需要添加:

代码语言:javascript
运行
复制
using System.Net.Http;

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

发布的代码似乎与webClient没有任何关系。实际使用HttpWebRequest编译的代码是否有问题?

更新

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

票数 113
EN

Stack Overflow用户

发布于 2012-12-02 19:25:52

NuGet > Microsoft.AspNet.WebApi.Client包

票数 91
EN

Stack Overflow用户

发布于 2015-06-04 20:11:02

我是怎么解决的。

  1. 打开项目(!)“属性”,选择“应用程序”,选择目标框架".Net Framework4.5“
  2. 在项目上单击鼠标右键并添加引用
  3. 确保在"Assemblies”-> "Extensions“选项中取消选中"System.Net.Http”
  4. 转到"Assemblies“-> " Framework”并选择"System.Net.Http“和"System.Net.Http”options
  5. that is!

<代码>G211

在我的例子中,我在开始时使用了2.0.0.0版本的.Net 4.0和“汇编”->“扩展”选项"System.Net.Http“。在我的操作之后,“程序集”和“框架”选项"System.Net.Http“和"System.Net.Http”具有相同的4.0.0.0版本。

票数 17
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9611316

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档