This will translate an URL to an IP address but only the first that is received and without error-checking!! If you don't have an internet connections / no dns server connection this will fail. If the URL doesn't exist it will fail too (of course).
// C#
using System.Net;
//[...]
public static string gethostbyname(string aURL)
{
PHostEntry Hosts = Dns.GetHostEntry(URL);
return Hosts.AddressList[0].ToString();
}
edit
To be more precise: It doesn’t translate an URL, only a domain-name e.g. the host-part of the URL
Dns.GetHostEntry does the job, but it only seems to work with bare urls like www.domain.com, no http://www.domain.com, no www.domain.com/subdir/index.html, etc.
So I’ve written this convenient function that takes pretty much any kind of url, strips away the unwanted parts and returns the IP address.
// C#
using System.Net;
public static string GetIP(string url) {
url = url.Replace("http://", ""); //remove http://
url = url.Replace("https://", ""); //remove https://
url = url.Substring(0, url.IndexOf("/")); //remove everything after the first /
try {
IPHostEntry hosts = Dns.GetHostEntry(url);
if(hosts.AddressList.Length > 0) return hosts.AddressList[0].ToString();
} catch {
Debug.Log ("Could not get IP for URL " + url);
}
return null;
}