C# 使用C检查负载平衡服务器#

C# 使用C检查负载平衡服务器#,c#,load-balancing,C#,Load Balancing,我的应用程序有四台应用程序服务器。应用程序正在使用负载平衡在所有服务器上运行。如果我的一台服务器出现故障,我必须使用我的系统主机文件手动检查。为了避免此手动过程,我使用C#创建了一个程序。我在主机文件中逐个写入服务器IP地址,并删除以前的一个 private void RunWithUAC() { List<string> lstIPAddress = new List<string>(); lstIPAddress.Add("1

我的应用程序有四台应用程序服务器。应用程序正在使用负载平衡在所有服务器上运行。如果我的一台服务器出现故障,我必须使用我的系统主机文件手动检查。为了避免此手动过程,我使用C#创建了一个程序。我在主机文件中逐个写入服务器IP地址,并删除以前的一个

 private void RunWithUAC()
    {
        List<string> lstIPAddress = new List<string>();
        lstIPAddress.Add("1.1.1.1 example.com");
        lstIPAddress.Add("1.1.1.1 example.com");
        lstIPAddress.Add("1.1.1.1 example.com");
        lstIPAddress.Add("1.1.1.1 example.com");
        var systemPath = Environment.GetFolderPath(Environment.SpecialFolder.System);
        Console.WriteLine(systemPath);
        var path = @"C:\Windows\System32\drivers\etc\hosts";

        foreach (var item in lstIPAddress)
        {
            System.IO.File.WriteAllText(path, string.Empty);
            try
            {
                File.WriteAllText(path, item);

                WebRequest request = WebRequest.Create("https://example.com");
                request.Timeout = 10000;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            }
            catch (Exception)
            {
                MessageBox.Show(item);
            }
            Thread.Sleep(1000);
        }
    }
private void RunWithUAC()
{
List lstIPAddress=新列表();
lstIPAddress.Add(“1.1.1.1 example.com”);
lstIPAddress.Add(“1.1.1.1 example.com”);
lstIPAddress.Add(“1.1.1.1 example.com”);
lstIPAddress.Add(“1.1.1.1 example.com”);
var systemPath=Environment.GetFolderPath(Environment.SpecialFolder.System);
Console.WriteLine(系统路径);
var path=@“C:\Windows\System32\drivers\etc\hosts”;
foreach(lstIPAddress中的var项)
{
System.IO.File.WriteAllText(路径,string.Empty);
尝试
{
File.WriteAllText(路径,项);
WebRequest=WebRequest.Create(“https://example.com");
请求超时=10000;
HttpWebResponse=(HttpWebResponse)request.GetResponse();
}
捕获(例外)
{
MessageBox.Show(项目);
}
睡眠(1000);
}
}
但当第二台服务器宕机时,它会给我第三台服务器的超时错误。
请检查代码并告诉我此代码的错误。

可能是某种连接池、HTTP管道或保持活动状态。这首先是错误的方法


直接连接到正确的IP(
WebRequest.Create(“https://1.1.1.1)
)。如果您需要发送
主机
标题。

我有一个代码,但这个边距太小,无法放在这里。你可能需要使用搜索引擎。