System.Net.Http:命名空间中缺少?(使用 .net 4.5) [英] System.Net.Http: missing from namespace? (using .net 4.5)

查看:31
本文介绍了System.Net.Http:命名空间中缺少?(使用 .net 4.5)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

TL; DR: I'm new to this language and have no idea what I'm doing

这是我目前的课程:

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

when I do csc filename.cs, I get the following error:

命名空间System.Net"中不存在类型或命名空间名称Http"(您是否缺少程序集引用?)

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.

And make sure you are referencing System.Net.Http.dll in .NET 4.5.

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

The code posted doesn't appear to do anything with webClient. Is there something wrong with the code that is actually compiling using HttpWebRequest?

更新

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆