简单的内部HTTP GET请求失败,由于SocketException:一个现有的连接被远程主机强制关闭



我有一个简单的健康检查系统,它向一个内部URL发送一个简单的HTTP GET请求,这是一个需要身份验证的MVC web应用程序。例如,如果你发送一个get请求到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:底层连接已关闭:发送时发生意外错误。——比;System.IO.IOException: cannot to read data from the transport connection:一个已存在的连接被远程主机强制关闭。——比;System.Net.Sockets.SocketException:已存在的连接被远程主机强制关闭在System.Net.Sockets.Socket。接收(Byte[] buffer, Int32偏移量,Int32大小,SocketFlags SocketFlags)在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;

这原来是DNS问题。文件中的一些服务器主机未包含在负载均衡器的hosts文件中。最后固定!

最新更新