Getting ip from specific URL

14,519

The full url like that will have the same hostname. As easy way to get the hostname is this:

var url = "http://oldschool.runescape.com/game?world=321";
Uri myUri = new Uri(url);
var ip = Dns.GetHostAddresses(myUri.Host)[0];

Simply extract the host out of the url and that should prevent your error giving you the correct Ip address of the host.

Share:
14,519
Starstepper
Author by

Starstepper

Updated on July 02, 2022

Comments

  • Starstepper
    Starstepper almost 2 years

    I'm trying to get the IP address of a specific url using:

    var ip = Dns.GetHostAddresses("http://oldschool.runescape.com/game?world=321")[0];
    

    The problem is that this function only seems to work with the normal host adress (as the function tells, ex: www.runescape.com) which creates an exception when using this specific more deeper URL. Is there any function to get the IP of a more specific URL?

    Thanks in advance.

  • jdweng
    jdweng almost 8 years
    This only works if there is only one Ethernet card. When you have multiple Ethernet card you have to check for IP. There is also an issue with loopback address 127.0.0.1 (localhost). Each computer has a Host file and which contains every Ethernet card and loopback address. Some computers have the local host and other do not. Some have Ethernet card first and others have localhost first. The GetHostAddress looks up entries in the Host file (in windows folder). So the index may not be zero.