如何为 HttpClientPostAsync 第二个参数设置 HttpContent?

public static async Task<string> GetData(string url, string data)
{
UriBuilder fullUri = new UriBuilder(url);


if (!string.IsNullOrEmpty(data))
fullUri.Query = data;


HttpClient client = new HttpClient();


HttpResponseMessage response = await client.PostAsync(new Uri(url), /*expects HttpContent*/);


response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();


return responseBody;
}

PostAsync接受另一个需要为 HttpContent的参数。

我如何设置一个 HttpContent? 没有任何地方的文档,为 Windows Phone 8工作。

如果我使用 GetAsync,它工作得很好! 但是它需要是 POST,其内容为 key = “ bla”,something = “ yay”

//编辑

非常感谢您的回答... ... 这个方法很有效,但仍有一些不确定性:

    public static async Task<string> GetData(string url, string data)
{
data = "test=something";


HttpClient client = new HttpClient();
StringContent queryString = new StringContent(data);


HttpResponseMessage response = await client.PostAsync(new Uri(url), queryString );


//response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();


return responseBody;
}

我假设在 api 方面,数据“ test = something”将作为后期数据“ test”获取,但显然它并非如此。另一方面,我可能需要通过 post 数据发布整个对象/数组,因此我假设 json 将是最佳选择。关于如何获取邮件数据有什么想法吗?

比如:

class SomeSubData
{
public string line1 { get; set; }
public string line2 { get; set; }
}


class PostData
{
public string test { get; set; }
public SomeSubData lines { get; set; }
}


PostData data = new PostData {
test = "something",
lines = new SomeSubData {
line1 = "a line",
line2 = "a second line"
}
}
StringContent queryString = new StringContent(data); // But obviously that won't work
552708 次浏览

这在我找不到如何使用HttpContent的一些答案以及这个博客中得到了回答。

总之,你不能直接设置HttpContent的实例,因为它是抽象类。您需要根据需要使用从它派生的类。很可能是StringContent,它允许你在构造函数中设置响应的字符串值、编码和媒体类型。看:http://msdn.microsoft.com/en-us/library/system.net.http.stringcontent.aspx

为了补充Preston的答案,下面是标准库中可用的HttpContent派生类的完整列表:

Credit: https://pfelix.wordpress.com/2012/01/16/the-new-system-net-http-classes-message-content/

信贷: https://pfelix.wordpress.com/2012/01/16/the-new-system-net-http-classes-message-content/

还有一个假定的ObjectContent,但我无法在ASP.NET Core中找到它。

当然,你可以通过Microsoft.AspNet.WebApi.Client扩展跳过整个HttpContent(你必须导入它才能在ASP中工作)。NET Core现在:https://github.com/aspnet/Home/issues/1558),然后你可以做这样的事情:

var response = await client.PostAsJsonAsync("AddNewArticle", new Article
{
Title = "New Article Title",
Body = "New Article Body"
});
    public async Task<ActionResult> Index()
{
apiTable table = new apiTable();
table.Name = "Asma Nadeem";
table.Roll = "6655";


string str = "";
string str2 = "";


HttpClient client = new HttpClient();


string json = JsonConvert.SerializeObject(table);


StringContent httpContent = new StringContent(json, System.Text.Encoding.UTF8, "application/json");


var response = await client.PostAsync("http://YourSite.com/api/apiTables", httpContent);


str = "" + response.Content + " : " + response.StatusCode;


if (response.IsSuccessStatusCode)
{
str2 = "Data Posted";
}


return View();
}