如果我将客户端连接到的服务指向一个错误的 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/