从命名空间的缺失

从命名空间的缺失

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

问题描述

TL; DR:我是新来这个语言,也不知道我在做什么。

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

 使用系统;
使用System.Collections.Generic;
使用System.Net.Http;
使用的System.Web;
使用System.Net;
使用System.IO;公共类MyClass的
    {
        私人常量字符串URL =htt​​ps://sub.domain.com/objects.json?api_key=123;
        私人常量字符串数据= @{对象:{名:标题}};        公共静态无效的CreateObject()
        {
            HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(URL);
            request.Method =POST;
            request.ContentType =应用/ JSON;
            request.ContentLength = data.Length;
            StreamWriter的requestWriter =新的StreamWriter(request.GetRequestStream(),System.Text.Encoding.ASCII);
            requestWriter.Write(数据);
            requestWriter.Close();            尝试
            {
                //得到响应
                WebResponse类WebResponse类= request.GetResponse();
                流webStream = webResponse.GetResponseStream();
                StreamReader的responseReader =新的StreamReader(webStream);
                串响应= responseReader.ReadToEnd();
                responseReader.Close();
            }
            赶上(引发WebException我们)
            {
                字符串webExceptionMessage = we.Message;
            }
            赶上(异常前)
            {
                //没有必要在这里做什么特别的事情....
            }
        }        静态无效的主要(字串[] args)
        {
            MyClass.CreateObject();
        }
}

当我做CSC filename.cs,我得到以下错误:

解决方案

HttpClient lives in the System.Net.Http namespace.

You'll need to add:

using System.Net.Http;

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


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


Update

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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 03:27