首页 > 代码库 > 负载均衡代码
负载均衡代码
string[] ips = new string[] { "192.168.1.98", "192.168.1.99" };
for (var i = 0; i < 50; i++)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://" + ips[i % 2] + "/server.html");
request.Host = "serveriphost.demo.com";
using (WebResponse response = request.GetResponse())
{
Console.Write($"{i.ToString().PadLeft(2, ‘ ‘)}:");
using (TextReader reader = new StreamReader(response.GetResponseStream()))
{
var result = reader.ReadToEnd();
if (result != ips[i % 2])
Console.WriteLine($"Host:{ips[i % 2]},Real:{result}");
else
Console.WriteLine("same.");
}
}
}
Console.WriteLine("End.");
Console.ReadKey();
负载均衡代码