Feedback

GeoLocation einer IP Adresse ermitteln

Sprache: C#

Der Dienst ipinfodb.com bietet eine kostenlose Lokalisierung der übergebenen IP Adresse an. Dieser Snippet zeigt, wie die Informationen mit .NET abgerufen werden können. Exceptionhandling ist nicht implementiert.
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Xml.Linq;
 
namespace IpLoc
{
    internal class IpInfo
    {
        public IpInformation GetIpInformation(string ip)
        {
            WebClient webClient = new WebClient();
            webClient.Encoding = Encoding.UTF8;
            XDocument xDocument = XDocument.Parse(webClient.DownloadString(string.Format("http://ipinfodb.com/ip_query.php?ip={0}&timezone=false", ip)));
            IpInformation ipInformation = new IpInformation();
 
            ipInformation.Ip = xDocument.Elements().Select(a => a.Element("Ip").Value).FirstOrDefault();
            ipInformation.CountryCode = xDocument.Elements().Select(a => a.Element("CountryCode").Value).FirstOrDefault();
            ipInformation.CountryName = HttpUtility.HtmlDecode(xDocument.Elements().Select(a => a.Element("CountryName").Value).FirstOrDefault());
            ipInformation.RegionCode = xDocument.Elements().Select(a => a.Element("RegionCode").Value).FirstOrDefault();
            ipInformation.RegionName = HttpUtility.HtmlDecode(xDocument.Elements().Select(a => a.Element("RegionName").Value).FirstOrDefault());
            ipInformation.City = HttpUtility.HtmlDecode(xDocument.Elements().Select(a => a.Element("City").Value).FirstOrDefault());
            ipInformation.ZipPostalCode = xDocument.Elements().Select(a => a.Element("ZipPostalCode").Value).FirstOrDefault();
            ipInformation.Latitude = xDocument.Elements().Select(a => a.Element("Latitude").Value).FirstOrDefault();
            ipInformation.Longitude = xDocument.Elements().Select(a => a.Element("Longitude").Value).FirstOrDefault();
 
            return ipInformation;
        }
 
        #region Nested type: IpInformation
 
        public class IpInformation
        {
            public string Ip { get; set; }
            public string CountryCode { get; set; }
            public string CountryName { get; set; }
            public string RegionCode { get; set; }
            public string RegionName { get; set; }
            public string City { get; set; }
            public string ZipPostalCode { get; set; }
            public string Latitude { get; set; }
            public string Longitude { get; set; }
        }
 
        #endregion
    }
}
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Xml.Linq;
 
namespace IpLoc
{
    internal class IpInfo
    {
        public IpInformation GetIpInformation(string ip)
        {
            WebClient webClient = new WebClient();
            webClient.Encoding = Encoding.UTF8;
            XDocument xDocument = XDocument.Parse(webClient.DownloadString(string.Format("http://ipinfodb.com/ip_query.php?ip={0}&timezone=false", ip)));
            IpInformation ipInformation = new IpInformation();
 
            ipInformation.Ip = xDocument.Elements().Select(a => a.Element("Ip").Value).FirstOrDefault();
            ipInformation.CountryCode = xDocument.Elements().Select(a => a.Element("CountryCode").Value).FirstOrDefault();
            ipInformation.CountryName = HttpUtility.HtmlDecode(xDocument.Elements().Select(a => a.Element("CountryName").Value).FirstOrDefault());
            ipInformation.RegionCode = xDocument.Elements().Select(a => a.Element("RegionCode").Value).FirstOrDefault();
            ipInformation.RegionName = HttpUtility.HtmlDecode(xDocument.Elements().Select(a => a.Element("RegionName").Value).FirstOrDefault());
            ipInformation.City = HttpUtility.HtmlDecode(xDocument.Elements().Select(a => a.Element("City").Value).FirstOrDefault());
            ipInformation.ZipPostalCode = xDocument.Elements().Select(a => a.Element("ZipPostalCode").Value).FirstOrDefault();
            ipInformation.Latitude = xDocument.Elements().Select(a => a.Element("Latitude").Value).FirstOrDefault();
            ipInformation.Longitude = xDocument.Elements().Select(a => a.Element("Longitude").Value).FirstOrDefault();
 
            return ipInformation;
        }
 
        #region Nested type: IpInformation
 
        public class IpInformation
        {
            public string Ip { get; set; }
            public string CountryCode { get; set; }
            public string CountryName { get; set; }
            public string RegionCode { get; set; }
            public string RegionName { get; set; }
            public string City { get; set; }
            public string ZipPostalCode { get; set; }
            public string Latitude { get; set; }
            public string Longitude { get; set; }
        }
 
        #endregion
    }
}

3 Kommentare

  1. Eine kleine Anpassung an die neuen Gegebenheiten bei ipinfodb.com:

    [code]
    public class IpInformation
    {
    public string Ip { get; set; }
    public string CountryCode { get; set; }
    public string CountryName { get; set; }
    public string RegionName { get; set; }
    public string City { get; set; }
    public string ZipPostalCode { get; set; }
    public string Latitude { get; set; }
    public string Longitude { get; set; }
    public string TimeZone { get; set; }
    }
    [/code]
    [code]
    public IpInformation GetIpInformation(string ip)
    {
    WebClient webClient = new WebClient();
    webClient.Encoding = Encoding.UTF8;
    string test = webClient.DownloadString(string.Format(„http://api.ipinfodb.com/v3/ip-city/?key=&ip={0}&format=xml“, ip));

    XDocument xDocument = XDocument.Parse(test);
    IpInformation ipInformation = new IpInformation();
    if (xDocument.Elements().Select(a => a.Element(„statusCode“).Value).FirstOrDefault() == „OK“)
    {
    ipInformation.Ip = xDocument.Elements().Select(a => a.Element(„ipAddress“).Value).FirstOrDefault();
    ipInformation.CountryCode = xDocument.Elements().Select(a => a.Element(„countryCode“).Value).FirstOrDefault();
    ipInformation.CountryName = xDocument.Elements().Select(a => a.Element(„countryName“).Value).FirstOrDefault();
    ipInformation.RegionName = xDocument.Elements().Select(a => a.Element(„regionName“).Value).FirstOrDefault();
    ipInformation.City = xDocument.Elements().Select(a => a.Element(„cityName“).Value).FirstOrDefault();
    ipInformation.ZipPostalCode = xDocument.Elements().Select(a => a.Element(„zipCode“).Value).FirstOrDefault();
    ipInformation.Latitude = xDocument.Elements().Select(a => a.Element(„latitude“).Value).FirstOrDefault();
    ipInformation.Longitude = xDocument.Elements().Select(a => a.Element(„longitude“).Value).FirstOrDefault();
    ipInformation.TimeZone = xDocument.Elements().Select(a => a.Element(„timeZone“).Value).FirstOrDefault();
    }
    return ipInformation;
    }
    [/code]