Created
October 7, 2018 20:38
-
-
Save progress44/1657072c55b9645e7c4e588fdfa0ab37 to your computer and use it in GitHub Desktop.
Geolocation class in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GeoLocation | |
{ | |
#region Properties | |
string latitude; | |
string lognitude; | |
string city; | |
string country; | |
string host; | |
string ip; | |
string code; | |
public string Latitude | |
{ | |
get { return latitude; } | |
} | |
public string Lognitude | |
{ | |
get { return lognitude; } | |
} | |
public string City | |
{ | |
get { return city; } | |
} | |
public string Country | |
{ | |
get { return country; } | |
} | |
public string Host | |
{ | |
get { return host; } | |
} | |
public string Ip | |
{ | |
get { return ip; } | |
} | |
public string Code | |
{ | |
get { return code; } | |
} | |
#endregion | |
#region Methods | |
private GeoLocation() { } | |
public GeoLocation(string latitude, string lognitude, string city, string country, string host, string ip, string code) | |
{ | |
this.latitude = latitude; | |
this.lognitude = lognitude; | |
this.city = city; | |
this.country = country; | |
this.host = host; | |
this.ip = ip; | |
this.code = code; | |
} | |
public static GeoLocation whatIsMyGeoLocation() | |
{ | |
HttpWebRequest myWebRequest = (HttpWebRequest)HttpWebRequest.Create(new Uri("http://geoiptool.com/data.php")); | |
myWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727)"; | |
using (HttpWebResponse webResponse = (HttpWebResponse)(myWebRequest.GetResponse())) | |
{ | |
using (StreamReader reader = new StreamReader(webResponse.GetResponseStream())) | |
{ | |
XmlDocument document = new XmlDocument(); | |
document.Load(reader); | |
XmlNodeList nodes = document.GetElementsByTagName("marker"); | |
if (nodes.Count > 0) | |
{ | |
XmlElement marker = nodes[0] as XmlElement; | |
if (marker != null) | |
{ | |
GeoLocation response = new GeoLocation(); | |
response.city = marker.GetAttribute("city"); | |
response.country = marker.GetAttribute("country"); | |
response.code = marker.GetAttribute("code"); | |
response.host = marker.GetAttribute("host"); | |
response.ip = marker.GetAttribute("ip"); | |
response.latitude = marker.GetAttribute("lat"); | |
response.lognitude = marker.GetAttribute("lng"); | |
} | |
} | |
} | |
} | |
return null; | |
} | |
public static string whatIsMyIp() | |
{ | |
Uri myUri = new Uri("http://www.whatismyip.org"); | |
HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(myUri); | |
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727)"; | |
using (WebResponse webResponse = webRequest.GetResponse()) | |
{ | |
using (StreamReader reader = new StreamReader(webResponse.GetResponseStream())) | |
{ | |
return reader.ReadLine(); | |
} | |
} | |
return string.Empty; | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment