Problem
I’m attempting to make use of HttpContent:
HttpContent myContent = HttpContent.Create(SOME_JSON);
…but I can’t seem to locate the DLL in which it is declared.
First, I attempted to add references to Microsoft.Http and System.Net, but neither of these were found in the list. I also tried referencing System.Net.Http, but the HttpContent class isn’t present.
So, can anyone tell me where I can find the HttpContent class?
Asked by user1416156
Solution #1
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);
Answered by Youngjae
Solution #2
Because HttpContent is abstract, you must use one of the derived classes to turn 6footunder’s comment into an answer:
Answered by Chris S
Solution #3
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);
Answered by Felipe Deveza
Solution #4
While the final version of HttpContent and the complete System.Net.Http namespace will ship with.NET 4.5, you can use the Microsoft.Net.Http package from NuGet to get a.NET 4 version.
Answered by Panagiotis Kanavos
Solution #5
I’m quite sure the code is using Microsoft.Http.HttpContent instead of System.Net.Http.HttpContent. Microsoft.Http was the WCF REST Starter Kit, which was never released to the public before being included in the.NET Framework. It’s still available at http://aspnet.codeplex.com/releases/view/24644.
I wouldn’t recommend using it as a foundation for new code.
Answered by RasmusW
Post is based on https://stackoverflow.com/questions/11145053/cant-find-how-to-use-httpcontent