I have the need to know the Public IP address of the device on which my app is running. The application is a Windows Store WinRT/Metro app.
Using: NetworkInformation.GetInternetConnectionProfile and NetworkInformation.GetHostNames I am able to retrieve the Local IP Address and not the Public.
In the end i have a found http://freegeoip.net/xml/ which is a public REST API for searching geolocation of IP addresses and host names. But this is not a viable solution, i was wondering if i was missing something? or if there is an easier way?
A simple way to find your external IP address is to use a open service on the internet.
For example this is taken from here: Getting the external IP address but adapted to work in WinRT
Task<string> result = GetExternalIP();
string externalIP = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"))
.Matches(result.Result)[0].ToString();
......
public async Task<string> GetExternalIP()
{
HttpClient http = new System.Net.Http.HttpClient();
HttpResponseMessage response = await http.GetAsync("http://www.realip.info/api/p/realip.php");
return await response.Content.ReadAsStringAsync();
}
If you try that url directly in your browser you will get a simple HTML page with this content
{"IP":"150.37.216.15"}
and then the Regex just extract the IP info 150.37.216.15