Quasar/Client/Core/Information/GeoIP.cs

49 lines
2.1 KiB
C#
Raw Normal View History

2014-07-08 12:58:53 +00:00
using System.IO;
using System.Net;
2015-02-24 11:04:07 +00:00
using System.Xml;
2014-07-08 12:58:53 +00:00
2015-02-24 18:21:01 +00:00
namespace xClient.Core.Information
2014-07-08 12:58:53 +00:00
{
2015-02-24 11:04:07 +00:00
class GeoIP
2014-07-08 12:58:53 +00:00
{
public string WANIP { get; private set; }
public string Country { get; private set; }
public string CountryCode { get; private set; }
public string Region { get; private set; }
public string City { get; private set; }
public GeoIP()
{
try
{
2015-02-24 11:04:07 +00:00
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://freegeoip.net/xml/");
2014-07-08 12:58:53 +00:00
request.Proxy = null;
2014-12-02 20:10:27 +00:00
request.Timeout = 5000;
2014-07-08 12:58:53 +00:00
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseString = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
2015-02-24 11:04:07 +00:00
XmlDocument doc = new XmlDocument();
doc.LoadXml(responseString);
2014-07-08 12:58:53 +00:00
2015-02-24 11:04:07 +00:00
WANIP = doc.SelectSingleNode("Response//IP").InnerXml;
Country = (!string.IsNullOrEmpty(doc.SelectSingleNode("Response//CountryName").InnerXml)) ? doc.SelectSingleNode("Response//CountryName").InnerXml : "Unknown";
CountryCode = (!string.IsNullOrEmpty(doc.SelectSingleNode("Response//CountryCode").InnerXml)) ? doc.SelectSingleNode("Response//CountryCode").InnerXml : "-";
Region = (!string.IsNullOrEmpty(doc.SelectSingleNode("Response//RegionName").InnerXml)) ? doc.SelectSingleNode("Response//RegionName").InnerXml : "Unknown";
City = (!string.IsNullOrEmpty(doc.SelectSingleNode("Response//City").InnerXml)) ? doc.SelectSingleNode("Response//City").InnerXml : "Unknown";
2014-07-08 12:58:53 +00:00
}
catch
{
WANIP = "-";
Country = "Unknown";
CountryCode = "-";
Region = "Unknown";
City = "Unknown";
}
}
}
2015-02-24 11:04:07 +00:00
}