gpt4 book ai didi

c# - 由于 SocketException : An existing connection was forcibly closed by the remote host,简单的内部 HTTP GET 请求失败

转载 作者:行者123 更新时间:2023-12-03 20:45:39 25 4
gpt4 key购买 nike

我有一个简单的健康检查系统,它向一个内部 URL 发送一个简单的 HTTP GET 请求,这是一个需要身份验证的 MVC Web 应用程序。例如,如果您向 https://{{IPAddress}}/MyMvcApp 发送获取请求,应用程序会将您重定向到 https://{{LB Host}}/MyMvcAppAuth .

private static void UsingHttpGetRequest(string uri, Action<HttpWebResponse> action)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback
(
delegate { return true; }
);

Log("Sending the HTTP Get request...");
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
Log($"Got a response! Status: {response.StatusCode}");
action(response);
}
}
我的农场中有两台服务器。当此代码在其中一台服务器上运行时,它工作正常,但另一台服务器出现此问题:

Exception: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote hostat System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)


  • 我比较了服务器之间的 IIS 配置设置,没有发现显着差异。
  • 我比较了注册表项,发现两台服务器都没有注册表项“SchUseStrongCrypto”,但两台服务器上肯定都启用了 TLS 1.2。
  • 验证两者都安装了 .NET v4.0.30319。

  • 我对此考虑得越多,我就越得出结论,即 F5 负载平衡器拒绝来自服务器场中其中一台服务器的请求的 302 重定向。你们有什么感想?拒绝这些请求的负载均衡器上潜在的防火墙/错误配置问题?

    最佳答案

    另一种方法。

           public static bool CheckPageExists(string url)
    {
    //checks only the headers to be very quick
    bool pageExists = false;
    try
    {
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = WebRequestMethods.Http.Head;
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    pageExists = response.StatusCode == HttpStatusCode.OK;
    }
    catch
    {
    return false;
    }
    return pageExists;
    }
    就这样称呼它 CheckPageExists("https://www.google.com")引用 using System.Net;

    关于c# - 由于 SocketException : An existing connection was forcibly closed by the remote host,简单的内部 HTTP GET 请求失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65834875/

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