0

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;
        }
    }
stuartd
  • 70,509
  • 14
  • 132
  • 163
Max Jacobi
  • 163
  • 11
  • 1
    What exactly do you mean by ‘doesn’t work’? – stuartd Feb 04 '22 at 22:40
  • 1
    You might want to look at https://stackoverflow.com/questions/6114793/binding-an-ip-address-just-works-for-the-first-time – Progman Feb 04 '22 at 22:43
  • @stuartd by "not working" I meant it doesn't use different ip addresses. All requests are made from one ip address only. Console output must show the same addresses on the left column and on the right one and it does but only if you use .NET Framework 4. I want the same behavior on the .NET Core and and newer versions. P.S. Of course you need a server with multiple ip addresses if you want to run my example. – Max Jacobi Feb 05 '22 at 09:10
  • @Progman I knew about that trick but it also works in .NET Framework 4 only. Once you switch on .NET Core 1/2/3/5/6 it doesn't make any difference. – Max Jacobi Feb 05 '22 at 09:13
  • Looks like the answer to my question is here: https://stackoverflow.com/questions/65930192/how-to-bind-httpclient-to-a-specific-source-ip-address-in-net-5?noredirect=1&lq=1 – Max Jacobi Feb 05 '22 at 11:59

1 Answers1

-2

Something like this works in .Net 5

private static readonly HttpClient client = new HttpClient();    
static async Task Main(string[] args)
    {
        var server = "https://localhost:44352";
        client.BaseAddress = new Uri(server);
        client.DefaultRequestHeaders
              .Accept
              .Add(new MediaTypeWithQualityHeaderValue("application/json"));

        
        var endpoint = "api/endpoint";

        var builder = new UriBuilder(client.BaseAddress + endpoint);
        var uri = builder.Uri;

        var getStuff = client.GetAsync(uri);
        var res = await getStuff;

        return;
    }

Of course, there are other ways to write it. The client has other methods you can use for PutAsync, PostAsync, etc. "https://localhost:44352" can be replaced with ip and port. Just make sure you have something listening on it for requests.

Tom B.
  • 55
  • 2
  • 7
  • Sorry but your answer isn't related to my question. I want to make requests to https://api.myip.com and see responses with different ip addresses in it. This site shows what ip was used to make request. So I want to be able to make requests from different ips. – Max Jacobi Feb 05 '22 at 09:25