[英]How can I get a Lambda using AWS Gateway to return HTTP error status code in C#
[英]How do I return the HTTP status code when I'm making a JSON post in C#?
我不了解变量类型以及如何利用客户端检索 http 状态代码。 client 变量是一个标准的 HttpClient 对象。
附图是我试图检索状态码的功能。 任何帮助将不胜感激。 [1]: https ://i.stack.imgur.com/9iR3g.png
它应该很容易
var client = new HttpClient();
var results = await client.GetAsync("https://stackoverflow.com");
Console.WriteLine(results.StatusCode);
你的问题是你没有得到响应对象。 您正在获取响应正文的内容。
这是一个示例代码:
void SimpleApiCall()
{
Uri endpoint = new Uri("https://www.7timer.info/bin/");
using var client = new HttpClient();
client.BaseAddress = endpoint;
// Get the response only here, and then get the content
// I'm using GetAwaiter().GetResult() because client.GetAsync() returns a Task and you're not using the async await since this is a button click event
var response = client.GetAsync("astro.php?lon=113.2&lat=23.1&ac=0&unit=metric&output=json&tzshift=0").GetAwaiter().GetResult();
// Status code will be available in the response
Console.WriteLine($"Status code: {response.StatusCode}");
// For the Reason phrase, it will be Ok for 200, Not Found for a 404 response...
Console.WriteLine($"Reason Phrase: {response.ReasonPhrase}");
// read the content of the response without the await keyword, use the .GetAwaiter().GetResult()
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
Console.WriteLine("Content:");
Console.WriteLine(content);
}
PostAsync 和所有其他操作也是如此......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.