gpt4 book ai didi

c# - RestSharp + Server Down - 您如何知道服务器是否已关闭?

转载 作者:行者123 更新时间:2023-11-30 18:30:05 25 4
gpt4 key购买 nike

我想知道如何检查我发出的 RestSharp 请求是否因为服务器宕机而失败。

当我关闭我的服务器时,我得到一个状态代码“NotFound”,但这可能是没有找到特定的记录(如果他们说他们试图找到最近可能被删除的记录,我会在我的网站上这样做)。

我怎样才能确定服务器确实已关闭?

编辑

这是我的代码

   private readonly RestClient client = new RestClient(GlobalVariables.ApiUrl);
var request = new RestRequest("MyController", Method.POST);
request.AddParameter("UserId", "1");
request.AddParameter("Name", name.Trim());

var asyncHandle = client.ExecuteAsync(request, response =>
{
var status = response.StatusCode;
});

最佳答案

当服务器关闭时,它不应返回“404 NotFound”错误。

在这种情况下最合适的是HTTP Error 503 - Service unavailable

The Web server (running the Web site) is currently unable to handle the HTTP request due to a temporary overloading or maintenance of the server. The implication is that this is a temporary condition which will be alleviated after some delay. Some servers in this state may also simply refuse the socket connection, in which case a different error may be generated because the socket creation timed out.

通过这种方式检查 RestResponse.StatusCode 是否为 503 它会告诉您服务器已关闭。

关于c# - RestSharp + Server Down - 您如何知道服务器是否已关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21960188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com