A smart way to get a public IP address / geographic location

I have a computer on the local network behind a NAT router. I have the addresses 192.168.0.x, but I really want to find out my public IP address, and not something mentioned in

How to get the IP address of the server my C # application is running on?

or

How to get the IP address of a machine in C #

I need C # code.

Is it possible? If so, how?

+3
source share
9 answers

After some searching, and expanding my requirements, I found out that this will give me not only an IP, but also a GEO location:

class GeoIp { static public GeoIpData GetMy() { string url = "http://freegeoip.net/xml/"; WebClient wc = new WebClient(); wc.Proxy = null; MemoryStream ms = new MemoryStream(wc.DownloadData(url)); XmlTextReader rdr = new XmlTextReader(url); XmlDocument doc = new XmlDocument(); ms.Position = 0; doc.Load(ms); ms.Dispose(); GeoIpData retval = new GeoIpData(); foreach (XmlElement el in doc.ChildNodes[1].ChildNodes) { retval.KeyValue.Add(el.Name, el.InnerText); } return retval; } } 

The XML is returned, and thus the key / value dictionary will be populated as such:

 <Response> <Ip>93.139.127.187</Ip> <CountryCode>HR</CountryCode> <CountryName>Croatia</CountryName> <RegionCode>16</RegionCode> <RegionName>Varazdinska</RegionName> <City>Varazdinske Toplice</City> <ZipCode/> <Latitude>46.2092</Latitude> <Longitude>16.4192</Longitude> <MetroCode/> </Response> 

And for convenience, we return the class:

 class GeoIpData { public GeoIpData() { KeyValue = new Dictionary<string, string>(); } public Dictionary<string, string> KeyValue; } 
+6
source

I prefer http://icanhazip.com . It returns a simple text string. No HTML parsing needed.

 string myIp = new WebClient().DownloadString(@"http://icanhazip.com").Trim(); 
+24
source

The problem is that the IP address you are looking for does not belong to your computer. It belongs to your NAT router. The only way I can think of getting it is to use an external server or request some kind of router.

If your router supports SNMP, you can get it this way.

+5
source

I believe that you really need to connect to some server in order to get an external IP address.

+2
source

Depending on the router you are using, the likelihood that you can get it directly from the router is very good. Most of them have a web interface, so it would be necessary to go to the correct web page (for example, "192.168.0.1/whatever") and "scrape" the external IP address from this page. The problem with this, of course, is that it is rather fragile - if you change (or even reconfigure) your router, it will most likely break.

+2
source

you can use uPNP and go back to whatsmyip.com if that fails.

+2
source

If you are concerned about the loss of connection or site availability, you can also try this method to avoid this problem by including the above suggestions.

  using System.Threading; Task<string>[] tasks = new[] { Task<string>.Factory.StartNew( () => new System.Net.WebClient().DownloadString(@"http://icanhazip.com").Trim() ), Task<string>.Factory.StartNew( () => new System.Net.WebClient().DownloadString(@"http://checkip.dyndns.org").Trim() ) }; int index = Task.WaitAny( tasks ); string ip = tasks[index].Result; 

Hope this helps too.

0
source

I do it like this: I create a class containing geodata data

 [Serializable] public class LocationData { public string IP { get; set; } public string CountryCode { get; set; } public string CountryName { get; set; } public string RegionCode { get; set; } public string City { get; set; } public string ZipCode { get; set; } public string TimeZone { get; set; } public string Latitude { get; set; } public string Longitude { get; set; } public string MetroCode { get; set; } } 

then I use the following code to call the geodata and populate the class.

 public static LocationData GetLocation(string ip= "") { using (var client = new System.Net.WebClient()) { XmlRootAttribute xRoot = new XmlRootAttribute(); xRoot.ElementName = "Response"; string downloadedString = client.DownloadString("http://freegeoip.net/xml/" + ip); XmlSerializer mySerializer = new XmlSerializer(typeof(LocationData), xRoot) ; using (XmlReader xmlReader = XmlReader.Create(new System.IO.StringReader(downloadedString))) { return mySerializer.Deserialize(xmlReader)as LocationData; } } } 

since the answer is "bad" xml, you need to specify the xRoot element or get an error.

Happy coding

Walter

0
source

Below code will help you publish IP address

  string _externalIP; _externalIP = (new WebClient()).DownloadString("http://http://icanhazip.com/"); _externalIP = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")) .Matches(externalIP)[0].ToString(); 
0
source

All Articles