My server has multiple ip addresses. I want to make GET/POST requests from all of them like in the example below. The thing is the example works in .NET Framework 4 but doesn't work in .NET core 1/2/3, .NET 5 and .NET 6. How to make it work in .NET 6? If it will be completely different approach I take it. It's not required to use specifically WebRequest class it's just the only way I know how to do it.
public static class Program
{
public static void Main()
{
string url = "https://api.myip.com";
List<string> list = GetLocalIpV4();
foreach (string ip in list)
Console.WriteLine($"{ip} - {Get(url, ip)}");
Console.ReadLine();
}
static List<string> GetLocalIpV4()
{
List<string> result = new List<string>();
foreach (IPAddress ip in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
result.Add(ip.ToString());
}
}
return result;
}
static string Get(string url, string ip)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ServicePoint.BindIPEndPointDelegate += (x, y, z) => new IPEndPoint(IPAddress.Parse(ip), 0);
string result;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream dataStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(dataStream);
result = reader.ReadToEnd();
}
}
return result;
}
}