260

I am trying to use HttpContent:

HttpContent myContent = HttpContent.Create(SOME_JSON);

...but I am not having any luck finding the DLL where it is defined.

First, I tried adding references to Microsoft.Http as well as System.Net, but neither is in the list. I also tried adding a reference to System.Net.Http but the HttpContent class is not available.

So, can anyone tell me where I can find the HttpContent class?

0

6 Answers 6

472

Just use...

var stringContent = new StringContent(jObject.ToString());
var response = await httpClient.PostAsync("http://www.sample.com/write", stringContent);

Or,

var stringContent = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync("http://www.sample.com/write", stringContent);
6
  • Says the content parameter needs to be IHttpContent and not StringContent. When I cast it to the interface it's happy, though. Commented May 13, 2015 at 1:39
  • 3
    Ended up using HttpStringContent. I couldn't use the StringContent class because PostAsync (or PutAsync in my case) doesn't accept StringContent even if you cast it to a IHttpContent object. Commented May 13, 2015 at 2:03
  • 4
    StringContent works for me with PostAsync, but if you want or need to use HttpContent, you can do so like this: HttpContent content = new StringContent(jsonString); Commented Aug 28, 2017 at 20:58
  • I keep seeing this answer to what appears to be a similar problem to mine, however as I debug through both of my APIs, I get a PostAsync("path", StringContent) to fire but when it hits the other API I don't have a body to parse and use and on return I get a 500... I am at a loss as it appears I am doing it just like this. Only difference is that I don't serialize the object as it is already serialized when I receive it in the first API and it is a simple passthrough.
    – Slagmoth
    Commented Aug 14, 2019 at 17:07
  • 2
    This should be the answer :/
    – DanielV
    Commented Nov 27, 2019 at 13:30
174

To take 6footunder's comment and turn it into an answer, HttpContent is abstract so you need to use one of the derived classes:

enter image description here

2
  • 12
    Looks like he used Visual Studio's "Class Diagram" feature (Right-click your project, Add Item, Class Diagram. Then you can go thru Solution Explorer and expand References to get diagrams of libraries you reference.) learn.microsoft.com/en-us/visualstudio/ide/…
    – Arin
    Commented Jul 5, 2017 at 22:43
  • @Chris, Please explain how you generated this class diagram for HttpContent... Commented Sep 15, 2021 at 1:49
130

For JSON Post:

var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync("http://www.sample.com/write", stringContent);

Non-JSON:

var stringContent = new FormUrlEncodedContent(new[]
{
    new KeyValuePair<string, string>("field1", "value1"),
    new KeyValuePair<string, string>("field2", "value2"),
});
var response = await httpClient.PostAsync("http://www.sample.com/write", stringContent);

And if you need to get the response:

var stringContent = await response.Content.ReadAsStringAsync();

https://blog.pedrofelix.org/2012/01/16/the-new-system-net-http-classes-message-content/

3
  • I'm trying to use your "non-json" example, how should I read the data on the receiving end?
    – Joe
    Commented Jun 23, 2020 at 17:21
  • 2
    var contents = await response.Content.ReadAsStringAsync(); Commented Jun 23, 2020 at 17:52
  • Related answer: stackoverflow.com/a/20005964/10210583
    – D. Ror.
    Commented Jul 16 at 14:54
6

While the final version of HttpContent and the entire System.Net.Http namespace will come with .NET 4.5, you can use a .NET 4 version by adding the Microsoft.Net.Http package from NuGet

0

I'm pretty sure the code is not using the System.Net.Http.HttpContent class, but instead Microsoft.Http.HttpContent. Microsoft.Http was the WCF REST Starter Kit, which never made it out preview before being placed in the .NET Framework. You can still find it here: http://aspnet.codeplex.com/releases/view/24644

I would not recommend basing new code on it.

-3

Just leaving the way using Microsoft.AspNet.WebApi.Client here.

Example:

var client = HttpClientFactory.Create();
var result = await client.PostAsync<ExampleClass>("http://www.sample.com/write", new ExampleClass(), new JsonMediaTypeFormatter());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.