Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting ip from specific URL

Tags:

c#

networking

ip

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.

like image 503
Starstepper Avatar asked Jun 19 '16 18:06

Starstepper


1 Answers

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.

like image 118
prospector Avatar answered Sep 30 '22 21:09

prospector