c# - 获取 0 的 HttpStatusCode

标签 c# rest http http-status-code-404 http-status-codes

如果我将客户端连接到的服务指向一个错误的 URL,为什么我得到的 HttpStatusCode 为 0。

我的 statusCodeAsInt 显示为 0。为什么它没有显示为 404 并被处理?

IRestResponse response = client.Execute(restReq);
HttpStatusCode statusCode = response.StatusCode;

var statusCodeAsInt = (int) statusCode;

        if (statusCodeAsInt >= 500)
        {
            throw new InvalidOperationException("A server error occurred: " + response.ErrorMessage, response.ErrorException);
        }
        if (statusCodeAsInt >= 400)
        {
            throw new InvalidOperationException("Request could not be understood by the server: " + response.ErrorMessage,
                response.ErrorException);
        }

处理这个 RestResponse 的正确方法是什么?

最佳答案

在我的案例中,这不是导致 StatusCode 为 0 的防火墙问题。我们使用的旧版应用程序仍在阻止 TLS 1.0 连接的服务器上使用 TLS 1.0。一旦我们启用了 TLS 1.2,我们就得到了我们期望的状态代码。

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

关于c# - 获取 0 的 HttpStatusCode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34795818/

相关文章:

javascript - 通过 Backbone.js 安全访问经过身份验证的 REST 服务器?

java - 为什么使用 POST 方法的 Java HttpURLConnections 会导致返回 Http 500 错误?

javascript - RESTful HTTP 网络的最佳 Javascript 框架

google-chrome - Chrome 尝试通过 https 加载 http 站点

http - Angular 2 : Get JSON content from HTTP response

c# - 自定义绘制控件的糟糕表现

c# - 如何找到 C# 项目中的所有硬编码值(解决方案)?

c# - 如何使用 ISynchronizeInvoke 接口(interface)?

java - 如何通过表单上传文件并让 Java 将其作为输入流处理?

c# - 图表上的 Epplus 误差线