0

I'm looking to specify the ip address to connect to when doing a web request. Yahoo.com, for instance, has 6 different ip addresses that I can see when making a request - How do I specify which specific ip address I want to use when making the connection?

I've seen a lot of people ask this or similar questions, but have yet to see a focused answer.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://" + urlTextBox.Text);

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

response.Close();

IPHostEntry host = Dns.GetHostEntry(response.ResponseUri.Host);

IPAddress[] ipAddresses = host.AddressList;

I'm given back an array of ip addresses just fine - Now how do I choose which one of these I make another specific webrequest to? Thanks in advance!

Point of clarification - I want to specify a specific ip to resolve to, not do any calculations on which to choose or anything

  • 1
    I don't think you'll find any RFC or any other authoritative answer to that question. If you check some of the most used software on earth, i.e: Google Chrome, cURL, etc.. you can see how they do it and implement whatever you see fit for your use case. Most will simply start with the first IP, if it timesout go for the second IP and so on.. maybe you can have an exponential back-off retry mechanism, maybe you do subnet calculations to try to find out which IP is closer to your client, maybe you keep performance metrics on each IP and start with the fastest one the next time.. – Alexandre Juma Jun 30 '19 at 02:05
  • @AlexandreJuma What? I'm trying to figure out just how to specify the IP in the first place, not how to do any advanced calculations. Say I want to connect to google at 1.1.1.1 ( easy/fake example ip) - How do I pass this specific ip to my request and force it to connect to google.com but also resolve to 1.1.1.1 ? But I do not want to connect to 1.1.1.1 - That most likely won't be handing out a valid SSL certificate, and won't a secure/proper connection. – Steven Hansen Jun 30 '19 at 03:13
  • Add an entry to your [hosts file](https://en.m.wikipedia.org/wiki/Hosts_(file)) `1.1.1.1 google.com`. That will bypass DNS. – John Wu Jun 30 '19 at 03:57
  • In that case check this link (it's rather involved): [Can I temporarily override DNS resolution within a .NET application?](https://stackoverflow.com/questions/5992700/can-i-temporarily-override-dns-resolution-within-a-net-application) – John Wu Jun 30 '19 at 05:01

0 Answers0