【发布时间】:2018-10-24 17:44:31
【问题描述】:
我正在尝试从 API 获取响应,但我没有任何运气。我正在使用 C# ASP.NET 运行 async 方法,但来自 response 的信息显示状态和许多内容,但实际响应除外。
这是我的代码:
private static async Task PostBasicAsync(CancellationToken cancellationToken)
{
Michael Maik = new Michael();
Maik.name = "Michael";
Maik.id = "114060502";
Maik.number = "83290910";
string Url = "http://my-api.com/testing/give-your-data";
using (var client = new HttpClient())
using (var request = new HttpRequestMessage(HttpMethod.Post, Url))
{
var json = JsonConvert.SerializeObject(Maik);
using (var stringContent = new StringContent(
json,
Encoding.UTF8,
"application/json"))
{
request.Content = stringContent;
using (var response = await client
.SendAsync(request,
HttpCompletionOption.ResponseHeadersRead,
cancellationToken)
.ConfigureAwait(false))
{
response.EnsureSuccessStatusCode();
}
}
}
}
我的代码有什么问题? 它应该返回如下内容:
{
"message": "Hi Michael we have received your data succesfully!",
"data": {
"name": "Michael",
"id": "114060502",
"number": "83290910"
}
}
【问题讨论】:
标签: c# asp.net json asynchronous http-post