示例#1
0
        private static void TryLocateFallback()
        {
            GeoInfo = new GeoInformation();

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://freegeoip.net/xml/");
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; rv:48.0) Gecko/20100101 Firefox/48.0";
                request.Proxy     = null;
                request.Timeout   = 10000;

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream dataStream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(dataStream))
                        {
                            string responseString = reader.ReadToEnd();

                            XmlDocument doc = new XmlDocument();
                            doc.LoadXml(responseString);

                            GeoInfo.Ip          = doc.SelectSingleNode("Response//IP").InnerXml;
                            GeoInfo.Country     = doc.SelectSingleNode("Response//CountryName").InnerXml;
                            GeoInfo.CountryCode = doc.SelectSingleNode("Response//CountryCode").InnerXml;
                            GeoInfo.Region      = doc.SelectSingleNode("Response//RegionName").InnerXml;
                            GeoInfo.City        = doc.SelectSingleNode("Response//City").InnerXml;
                            GeoInfo.Timezone    = doc.SelectSingleNode("Response//TimeZone").InnerXml;
                        }
                    }
                }

                LastLocated       = DateTime.UtcNow;
                LocationCompleted = true;
            }
            catch
            {
                LocationCompleted = false;
            }

            if (string.IsNullOrEmpty(GeoInfo.Ip))
            {
                TryGetWanIp();
            }
        }
        private void TryLocateFallback()
        {
            GeoInfo = new GeoInformation();

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://freegeoip.net/xml/" + IP);
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; rv:48.0) Gecko/20100101 Firefox/48.0";
                request.Proxy     = null;
                request.Timeout   = 10000;

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream dataStream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(dataStream))
                        {
                            string responseString = reader.ReadToEnd();

                            XmlDocument doc = new XmlDocument();
                            doc.LoadXml(responseString);

                            string xmlIp          = doc.SelectSingleNode("Response//IP").InnerXml;
                            string xmlCountry     = doc.SelectSingleNode("Response//CountryName").InnerXml;
                            string xmlCountryCode = doc.SelectSingleNode("Response//CountryCode").InnerXml;
                            string xmlRegion      = doc.SelectSingleNode("Response//RegionName").InnerXml;
                            string xmlCity        = doc.SelectSingleNode("Response//City").InnerXml;
                            string timeZone       = doc.SelectSingleNode("Response//TimeZone").InnerXml;

                            GeoInfo.Ip = (!string.IsNullOrEmpty(xmlIp))
                                ? xmlIp
                                : "";
                            GeoInfo.Country = (!string.IsNullOrEmpty(xmlCountry))
                                ? xmlCountry
                                : "Unknown";
                            GeoInfo.CountryCode = (!string.IsNullOrEmpty(xmlCountryCode))
                                ? xmlCountryCode
                                : "-";
                            GeoInfo.Region = (!string.IsNullOrEmpty(xmlRegion))
                                ? xmlRegion
                                : "Unknown";
                            GeoInfo.City = (!string.IsNullOrEmpty(xmlCity))
                                ? xmlCity
                                : "Unknown";
                            GeoInfo.Timezone = (!string.IsNullOrEmpty(timeZone))
                                ? timeZone
                                : "Unknown";

                            GeoInfo.Isp    = "Unknown"; // freegeoip does not support ISP detection
                            GeoInfo.Status = "success";
                        }
                    }
                }

                //LastLocated = DateTime.UtcNow;
                LocationCompleted = true;
            }
            catch
            {
                GeoInfo.Country     = "Unknown";
                GeoInfo.CountryCode = "-";
                GeoInfo.Region      = "Unknown";
                GeoInfo.City        = "Unknown";
                GeoInfo.Timezone    = "Unknown";
                GeoInfo.Isp         = "Unknown";
                LocationCompleted   = false;
            }

            if (string.IsNullOrEmpty(GeoInfo.Ip))
            {
                TryGetWanIp();
            }
        }
        private static void TryLocateFallback()
        {
            GeoInfo = new GeoInformation();

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://freegeoip.net/xml/");
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0";
                request.Proxy = null;
                request.Timeout = 5000;

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream dataStream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(dataStream))
                        {
                            string responseString = reader.ReadToEnd();

                            XmlDocument doc = new XmlDocument();
                            doc.LoadXml(responseString);

                            string xmlIp = doc.SelectSingleNode("Response//IP").InnerXml;
                            string xmlCountry = doc.SelectSingleNode("Response//CountryName").InnerXml;
                            string xmlCountryCode = doc.SelectSingleNode("Response//CountryCode").InnerXml;
                            string xmlRegion = doc.SelectSingleNode("Response//RegionName").InnerXml;
                            string xmlCity = doc.SelectSingleNode("Response//City").InnerXml;

                            GeoInfo.ip = (!string.IsNullOrEmpty(xmlIp))
                                ? xmlIp
                                : "-";
                            GeoInfo.country = (!string.IsNullOrEmpty(xmlCountry))
                                ? xmlCountry
                                : "Unknown";
                            GeoInfo.country_code = (!string.IsNullOrEmpty(xmlCountryCode))
                                ? xmlCountryCode
                                : "-";
                            GeoInfo.region = (!string.IsNullOrEmpty(xmlRegion))
                                ? xmlRegion
                                : "Unknown";
                            GeoInfo.city = (!string.IsNullOrEmpty(xmlCity))
                                ? xmlCity
                                : "Unknown";
                        }
                    }
                }

                LastLocated = DateTime.UtcNow;
                LocationCompleted = true;
            }
            catch
            {
                GeoInfo.country = "Unknown";
                GeoInfo.country_code = "-";
                GeoInfo.region = "Unknown";
                GeoInfo.city = "Unknown";
                LocationCompleted = false;
            }

            if (string.IsNullOrEmpty(GeoInfo.ip))
                TryGetWanIp();
        }