Can't find how to use HttpContent

C#asp.net.NetDll

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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

Solution 2 - C#

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

Solution 3 - C#

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

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

Solution 4 - C#

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

Solution 5 - C#

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.

Solution 6 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1416156View Question on Stackoverflow
Solution 1 - C#YoungjaeView Answer on Stackoverflow
Solution 2 - C#Chris SView Answer on Stackoverflow
Solution 3 - C#Felipe DevezaView Answer on Stackoverflow
Solution 4 - C#Panagiotis KanavosView Answer on Stackoverflow
Solution 5 - C#RasmusWView Answer on Stackoverflow
Solution 6 - C#NX ENGINEERINGView Answer on Stackoverflow