Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Net.Http: missing from namespace? (using .net 4.5)

Tags:

rest

c#

post

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

here is my class so far:

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();         } } 

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

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

like image 568
NullVoxPopuli Avatar asked Mar 08 '12 00:03

NullVoxPopuli


People also ask

What is System Net HTTP?

Provides a programming interface for modern HTTP applications, including HTTP client components that allow applications to consume web services over HTTP and HTTP components that can be used by both clients and servers for parsing HTTP headers.

What is HTTP namespace?

Http namespace is designed to provide the following: HTTP client components that allow users to consume modern web services over HTTP. HTTP components that can be used by both clients and servers (HTTP headers and messages, for example).


1 Answers

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:

enter image description here

like image 156
M.Babcock Avatar answered Sep 30 '22 19:09

M.Babcock