public static GeoLocationInfo GetLocationInfo(string ipaddress) { var request = (HttpWebRequest)WebRequest.Create(String.Format("http://freegeoip.net/json/{0}", ipaddress)); request.Method = "GET"; request.Accept = "application/json"; #if PFX_LEGACY_3_5 var response = request.GetResponse(); using (var sr = new StreamReader(response.GetResponseStream())) #else var response = request.GetResponseAsync(); if (!response.Wait(TimeSpan.FromSeconds(5))) { return(null); } if (!response.IsCompleted || response.Result == null) { return(null); } using (var sr = new StreamReader(response.Result.GetResponseStream())) #endif { var data = sr.ReadToEnd(); var s = new System.Web.Script.Serialization.JavaScriptSerializer(); var dict = s.Deserialize <Dictionary <string, string> >(data); if (dict == null) { return(null); } var location = new GeoLocationInfo { City = dict["city"], RegionName = dict["region_name"], RegionCode = dict["region_code"], CountryCode = dict["country_code"], CountryName = dict["country_name"] }; return(location); } }
public static GeoLocationInfo GetLocationInfo(string ipaddress) { var request = (HttpWebRequest)WebRequest.Create(String.Format("http://freegeoip.net/json/{0}", ipaddress)); request.Method = "GET"; request.Accept = "application/json"; #if PFX_LEGACY_3_5 var response = request.GetResponse(); using (var sr = new StreamReader(response.GetResponseStream())) #else var response = request.GetResponseAsync(); if (!response.Wait(TimeSpan.FromSeconds(5))) return null; if (!response.IsCompleted || response.Result == null) return null; using (var sr = new StreamReader(response.Result.GetResponseStream())) #endif { var data = sr.ReadToEnd(); var s = new System.Web.Script.Serialization.JavaScriptSerializer(); var dict = s.Deserialize<Dictionary<string, string>>(data); if (dict == null) return null; var location = new GeoLocationInfo { City = dict["city"], RegionName = dict["region_name"], RegionCode = dict["region_code"], CountryCode = dict["country_code"], CountryName = dict["country_name"] }; return location; } }