gpt4 book ai didi

c# - 获取 0 的 HttpStatusCode

转载 作者:可可西里 更新时间:2023-11-01 07:52:30 25 4
gpt4 key购买 nike

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

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