C# 查看HttpClient.PostAsync的响应正文
我在《邮递员》杂志上发表了一篇文章,它产生了以下效果:C# 查看HttpClient.PostAsync的响应正文,c#,.net,httpclient,dotnet-httpclient,C#,.net,Httpclient,Dotnet Httpclient,我在《邮递员》杂志上发表了一篇文章,它产生了以下效果: curl --location --request POST 'https://authqa.cqi.newry.me.com/as/token.oauth2' \ --header 'Authorization: Basic QVBJX0JURUNSRUZEQVRBOlAzNkNEAAE3RVdGUzRPT0NLQlQ0SERaS1pGRTZHTksyQkNJTFVJWT0=' \ --header 'Content-Type: appl
curl --location --request POST 'https://authqa.cqi.newry.me.com/as/token.oauth2' \
--header 'Authorization: Basic QVBJX0JURUNSRUZEQVRBOlAzNkNEAAE3RVdGUzRPT0NLQlQ0SERaS1pGRTZHTksyQkNJTFVJWT0=' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Cookie: PF=mgvtuOC0jyPx3Lbrmw1hTX; BIGipServerauthqa-9031=1948242442.18211.0000' \
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode 'charset=utf-8'
它返回以下正文:
{
"access_token": "generated_token",
"token_type": "Bearer",
"expires_in": 1799
}
我正试图使用HttpClient
在C语言中实现同样的功能
这是完成帖子的地方:
var body = new StringContent("grant_type=client_credentials", Encoding.UTF8,
"application/x-www-form-urlencoded");
var httpResponseMessage = await _client.PostAsync("sa/token.oauth2", body);
我通过httpResponseMessage.ToString()
获得以下信息
我看不到上面发布的响应正文,但我怀疑是
内容:System.Net.Http.HttpConnectionResponseContent
。但我不知道怎么看。有人能告诉我如何查看上述正文吗?httpResponseMessage.Content.ReadAsStringAsync()。结果将输出我想要的内容,但它被阻止
var response = await client.PostAsync("sa/token.oauth2", body);
var content = await response.Content.ReadAsStringAsync();
请注意,您通常应该使用等待
-而不是。结果
请参阅httpResponseMessage.Content.ReadAsStringAsync()。结果将输出所需内容,但它被阻止
var response = await client.PostAsync("sa/token.oauth2", body);
var content = await response.Content.ReadAsStringAsync();
请注意,您通常应该使用等待
-而不是。结果
请参阅HttpClientResponse包含的内容,您可以这样阅读:
var body = new StringContent("grant_type=client_credentials", Encoding.UTF8,
"application/x-www-form-urlencoded");
var httpResponseMessage = await _client.PostAsync("sa/token.oauth2", body);
var responseContent = await httpResponseMessage.Content.ReadAsStringAsync(); // here you can read content as string
...
还要查看更多信息。HttpClientResponse包含内容,您可以这样阅读:
var body = new StringContent("grant_type=client_credentials", Encoding.UTF8,
"application/x-www-form-urlencoded");
var httpResponseMessage = await _client.PostAsync("sa/token.oauth2", body);
var responseContent = await httpResponseMessage.Content.ReadAsStringAsync(); // here you can read content as string
...
还要查看更多信息。请不要主张对异步I/O执行阻塞操作。答案已更新。谢谢@Petercsala我的答案解决了你的问题吗?请不要提倡对异步I/O执行阻塞操作。答案已更新。谢谢@petercsala我的回答解决了你的问题吗?这回答了你的问题吗?这回答了你的问题吗?