C# 找不到如何使用 HttpContent
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11145053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't find how to use HttpContent
提问by user1416156
I am trying to use HttpContent:
我正在尝试使用HttpContent:
HttpContent myContent = HttpContent.Create(SOME_JSON);
...but I am not having any luck finding the DLL where it is defined.
...但我没有找到定义它的 DLL 的运气。
First, I tried adding references to Microsoft.Httpas well as System.Net, but neither is in the list. I also tried adding a reference to System.Net.Httpbut the HttpContentclass is not available.
首先,我尝试添加对Microsoft.Httpas 和 的引用System.Net,但都没有在列表中。我也尝试添加对的引用,System.Net.Http但HttpContent该类不可用。
So, can anyone tell me where I can find the HttpContentclass?
那么,谁能告诉我在哪里可以找到HttpContent课程?
采纳答案by dlev
The class is listed as being presentin the System.Net.Httpassembly. Note that this class is new for .NET 4.5, so you need to be using that version of the BCL.
该类被列为存在于System.Net.Http程序集中。请注意,此类是 .NET 4.5 的新增类,因此您需要使用该版本的 BCL。
回答by EkoostikMartin
The System.Net.Httpnamespace (where HttpContentclass resides) is new to .Net 4.5, are you using a VS2012 RC?
该System.Net.Http命名空间(如HttpContent类所在)是一个新的.NET 4.5,你使用VS2012 RC?
Otherwise, you wouldn't have access to this.
否则,您将无法访问此内容。
回答by Panagiotis Kanavos
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.Httppackage from NuGet
虽然 HttpContent 的最终版本和整个 System.Net.Http 命名空间将随 .NET 4.5 一起提供,但您可以通过添加来自 NuGet的Microsoft.Net.Http包来使用 .NET 4 版本
回答by Chris S
To take 6footunder's comment and turn it into an answer, HttpContentis abstract so you need to use one of the derived classes:
获取 6footunder 的评论并将其转换为答案HttpContent是抽象的,因此您需要使用派生类之一:


回答by Youngjae
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);
回答by RasmusW
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
我很确定代码没有使用 System.Net.Http.HttpContent 类,而是使用 Microsoft.Http.HttpContent。Microsoft.Http 是 WCF REST Starter Kit,它在被放入 .NET Framework 之前从未进行过预览。您仍然可以在这里找到它:http: //aspnet.codeplex.com/releases/view/24644
I would not recommend basing new code on it.
我不建议基于它的新代码。
回答by Felipe Deveza
For JSON Post:
对于 JSON 帖子:
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync("http://www.sample.com/write", stringContent);
Non-JSON:
非 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/
https://blog.pedrofelix.org/2012/01/16/the-new-system-net-http-classes-message-content/

