[c#] System.Net.Http: missing from namespace? (using .net 4.5)

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?)

This question is related to c# api rest post

The answer is


To solve the problem :

  1. Go to your solution explorer.
  2. Right click on the project name and choose add
  3. Select references and allow the .Net framework 4.5 to finish loading
  4. Scroll down and select System.Net.Http and click ok.

Problem solved.


You'll need a using System.Net.Http at the top.


Assuming that your using Visual Studio 10, you can download an install that includes System.Net.Http, for Visual Studio 10 here: download MVC4 for VS10

Once you've installed it, right click on the References folder in the VS Project and then select Add Reference. Then, select the Browse tab. Navigate to the assemblies install path for the MVC4 install (usually in Program Files(x86)/Microsoft ASP.NET/ASP.NET MVC4/assemblies) and select the assembly named 'System.Net.Http.dll'. Now you can add your 'using System.Net.Http' at the top of your code and begin creating HttpClient connections.


You need to have a Reference to the System.Web.Http assembly which has the HTTPClient class, your trying to use. Try adding the below line before your class declaration

using System.Web.Http;

If you still get the Error, try doing this in Visual Studio

  1. Right click on the References folder on your project.
  2. Select Add Reference.
  3. Select the .NET tab (or select the Browse button if it is not a .NET Framework assembly).
  4. Double-click the assembly containing the namespace in the error message (System.Web.Http.dll).
  5. Press the OK button.

HttpClient is new in .net 4.5. You should probably be using HttpWebRequest.


I had this issue after upgrading to .NET Framework 4.7.2. I found out that Nuget package for System.Net.Http is no longer recommended. Here are workarounds:


NuGet > Microsoft.AspNet.WebApi.Client package


In Visual Studio you can use nuget to load the package

Microsoft.AspNet.WebApi.WebHost

just go to add reference then add

system.net.http

enter image description here


Making the "Copy Local" property True for the reference did it for me. Expand References, right-click on System.Net.Http and change the value of Copy Local property to True in the properties window. I'm using VS2019.


For me, it was getting the nuget package Microsoft.Net.Http .(https://blogs.msdn.microsoft.com/bclteam/p/httpclient/)


How I solved it.

  1. Open project (!) "Properties", choose "Application", select targeting framework ".Net Framework 4.5"
  2. Right click on your project -> Add reference
  3. Make sure that in "Assemblies" -> "Extensions" option "System.Net.Http" is unchecked
  4. Go to "Assemblies" -> "Framework" and select "System.Net.Http" and "System.Net.Http" options
  5. That`s all!

In my case i had at the beginning .Net 4.0 and "Assemblies" -> "Extensions" option "System.Net.Http" with version 2.0.0.0. After my actions "Assemblies" -> "Framework" options "System.Net.Http" and "System.Net.Http" had the same 4.0.0.0 version.


Make sure it's referenced in your web.config:


With the Nuget Package Manager install Microsoft.AspNet.WebApi.Core.

After this:

using System.Web.Http;

or if you use VB

imports System.Web.Http


Visual Studio Package Manager Console > Install-Package Microsoft.AspNet.WebApi.Client


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to api

I am receiving warning in Facebook Application using PHP SDK Couldn't process file resx due to its being in the Internet or Restricted zone or having the mark of the web on the file Failed to load resource: the server responded with a status of 404 (Not Found) css Call another rest api from my server in Spring-Boot How to send custom headers with requests in Swagger UI? This page didn't load Google Maps correctly. See the JavaScript console for technical details How can I send a Firebase Cloud Messaging notification without use the Firebase Console? Allow Access-Control-Allow-Origin header using HTML5 fetch API How to send an HTTP request with a header parameter? Laravel 5.1 API Enable Cors

Examples related to rest

Access blocked by CORS policy: Response to preflight request doesn't pass access control check Returning data from Axios API Access Control Origin Header error using Axios in React Web throwing error in Chrome JSON parse error: Can not construct instance of java.time.LocalDate: no String-argument constructor/factory method to deserialize from String value How to send json data in POST request using C# How to enable CORS in ASP.net Core WebAPI RestClientException: Could not extract response. no suitable HttpMessageConverter found REST API - Use the "Accept: application/json" HTTP Header 'Field required a bean of type that could not be found.' error spring restful API using mongodb MultipartException: Current request is not a multipart request

Examples related to post

How to post query parameters with Axios? How can I add raw data body to an axios request? HTTP POST with Json on Body - Flutter/Dart How do I POST XML data to a webservice with Postman? How to set header and options in axios? Redirecting to a page after submitting form in HTML How to post raw body data with curl? How do I make a https post in Node Js without any third party module? How to convert an object to JSON correctly in Angular 2 with TypeScript Postman: How to make multiple requests at the same time