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

标签 c# restsharp

我想知道如何检查我发出的 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/

相关文章:

authentication - RestSharp 中的 OAuth 2.0 身份验证

c# - OpenGL 中的纹理显示为单色

c# - 计时器类和 C#

c# - 将 LINQ-to-Entities 查询组合成单个查询

c# - C# 和 SQL Server 中的 DateTimeOffset 解析

c# - 如何将 JSON 反序列化为从 RestSharp 调用 API 返回的对象数组?

c# - 'RestClient' 不包含 'JsonSerializer' 的定义并且没有扩展方法 'JsonSerializer'

c# - IEnumerable 和 IEnumerable<T> 之间的区别?

C# RestSharp 阻止 302 上的请求重定向