[c#] 找不到如何使用HttpContent



Answers

只需使用...

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

要么,

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

我正在尝试使用HttpContent

HttpContent myContent = HttpContent.Create(SOME_JSON);

...但我没有找到它定义的DLL的运气。

首先,我尝试添加对Microsoft.HttpSystem.Net引用,但都不在列表中。 我也尝试添加对System.Net.Http的引用,但HttpContent类不可用。

那么,谁能告诉我在哪里可以找到HttpContent类?




对于JSON Post:

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

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

blog.pedrofelix.org/2012/01/16/…







Related