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; }
source share