Ejemplo n.º 1
0
        public static GeoIpData GetCurrentLocation()
        {
            GeoIpData currentGeoIpData;


            try

            {
                var interactionData = Tracker.Current.Interaction;

                if (interactionData.HasGeoIpData && interactionData.GeoData.Latitude != null)
                {
                    currentGeoIpData = new GeoIpData(interactionData.GeoData.City, interactionData.GeoData.Region, interactionData.GeoData.Latitude, interactionData.GeoData.Longitude);
                }
                else
                {
                    currentGeoIpData = new GeoIpData();
                }
            }
            catch (Exception)
            {
                currentGeoIpData = new GeoIpData();
                //TODO: Improve this patch to possible write to logs
            }

            return(currentGeoIpData);
        }
Ejemplo n.º 2
0
    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);
    }
Ejemplo n.º 3
0
        public async Task <ActionResult <GeoIpData> > Get(string host)
        {
            var validatedIp = NetUtils.GetIpAddress(host);

            if (validatedIp == null)
            {
                var msg = "Invalid host or host could not be reached";
                _logger.LogError(msg);
                return(BadRequest(msg));
            }

            var ipAddress = validatedIp.ToString();
            var client    = _clientFactory.CreateClient();

            var freeGeoIpRepository = new FreeGeoIpRepository(client);

            _logger.LogDebug("Getting GeoIp data...");
            var geoIpData = await freeGeoIpRepository.GetGeoIpData(ipAddress);

            _logger.LogDebug("GeoIp data result: {1}", geoIpData);

            var output = new GeoIpData
            {
                City        = geoIpData.City,
                CountryName = geoIpData.CountryName,
                CountryCode = geoIpData.CountryCode,
                Host        = geoIpData.Ipv4,
                RegionName  = geoIpData.RegionName,
                RegionCode  = geoIpData.RegionCode,
                Latitude    = geoIpData.Latitude,
                Longitude   = geoIpData.Longitude,
                ZipCode     = geoIpData.ZipCode
            };

            return(Ok(output));
        }