C# :: Aufgabe #158 :: Lösung #1
4 Lösungen

#158
Standort einer beliebigen Ip-Adresse
Anfänger - C#
von ZRX88
- 30.12.2016 um 13:04 Uhr
In Python Aufgabe 101 ( https://trainyourprogrammer.de/python-101-ermitteln-der-ip-klasse.html)
war die Aufgabenstellung für die eingegebene IP die Klasse zu bestimmen.
Viel spannender ist ( aus meiner Sicht) der Standort des Servers:
Nutzt die API https://freegeoip.net/ um den Standort der eingebenen Ip-Adresse ausgeben wird.
Beispiel:
Eingabe = 66.249.66.1
Ausgabe = Die Ip 66.249.66.1 befindet sich in dem Land US in der Stadt Mountain View, die Geo Koordinaten sind 37.4192,-122.0574
p.s. Die Beispiel Ip ist die Ip des Google Bots ;)
war die Aufgabenstellung für die eingegebene IP die Klasse zu bestimmen.
Viel spannender ist ( aus meiner Sicht) der Standort des Servers:
Nutzt die API https://freegeoip.net/ um den Standort der eingebenen Ip-Adresse ausgeben wird.
Beispiel:
Eingabe = 66.249.66.1
Ausgabe = Die Ip 66.249.66.1 befindet sich in dem Land US in der Stadt Mountain View, die Geo Koordinaten sind 37.4192,-122.0574
p.s. Die Beispiel Ip ist die Ip des Google Bots ;)
#1

von DBqFetti (2480 Punkte)
- 01.01.2017 um 14:29 Uhr

using System; using System.Globalization; using System.IO; using System.Net; using System.Xml; namespace IpStandort { class Program { static LocationStrategy LocStrat = new XmlLocation(); static void Main() { Console.Write("IP oder Hostname>"); string input = Console.ReadLine(); Location Location = LocStrat.GetLocation(input); string format = "Die Ip {0} befindet sich in dem Land {1} in der Stadt {2}, die Geo Koordinaten sind {3:0.####}, {4:0.####}"; object[] arg = new object[] { Location.IP, Location.CountryCode, Location.City, Location.Latitude, Location.Longitude, }; Console.WriteLine(format, arg); Console.ReadKey(true); } } interface LocationStrategy { Location GetLocation(string IP_or_hostname); } class XmlLocation : LocationStrategy { public Location GetLocation(string IP_or_hostname) { // Xml manuell holen anstatt mit XmlDocument.Load da dieses unangenehm lange nach einem Proxy sucht. HttpWebRequest Request = WebRequest.CreateHttp("http://freegeoip.net/xml/" + IP_or_hostname) as HttpWebRequest; Request.Proxy = null; string xmlString = ""; using(WebResponse Response = Request.GetResponse()) { using(Stream st = Response.GetResponseStream()) { using(StreamReader sr = new StreamReader(st)) { xmlString = sr.ReadToEnd(); } } } XmlDocument Xml = new XmlDocument(); Xml.LoadXml(xmlString); return Location.FromXml(Xml); } } // Auf Implementierung von Json und Csv (Aufblähung/Fleißarbeit) verzichtet. class JsonLocation : LocationStrategy { public Location GetLocation(string IP_or_hostname) { throw new NotImplementedException(); } } class CsvLocation : LocationStrategy { public Location GetLocation(string IP_or_hostname) { throw new NotImplementedException(); } } class Location { IPAddress _ip; string _countryCode, _countryName, _regionCode, _regionName, _city, _zipCode, _timeZone; float _latitude, _longitude; int _metroCode; public IPAddress IP { get { return _ip; } } public string CountryCode { get { return _countryCode; } } public string CountryName { get { return _countryName; } } public string RegionCoe { get { return _regionCode; } } public string RegionName { get { return _regionName; } } public string City { get { return _city; } } public string ZipCode { get { return _zipCode; } } public string TimeZone { get { return _timeZone; } } public float Latitude { get { return _latitude; } } public float Longitude { get { return _longitude; } } public int MetroCode { get { return _metroCode; } } private Location() { } static public Location FromXml(XmlDocument Xml) { Location Loc = new Location(); XmlNode Root = Xml.SelectSingleNode("Response"); IFormatProvider enUS = new CultureInfo("en-US"); Loc._ip = IPAddress.Parse(Root.SelectSingleNode("IP").InnerText); Loc._countryCode = Root.SelectSingleNode("CountryCode").InnerText; Loc._countryName = Root.SelectSingleNode("CountryName").InnerText; Loc._regionCode = Root.SelectSingleNode("RegionCode").InnerText; Loc._regionName = Root.SelectSingleNode("RegionName").InnerText; Loc._city = Root.SelectSingleNode("City").InnerText; Loc._zipCode = Root.SelectSingleNode("ZipCode").InnerText; Loc._timeZone = Root.SelectSingleNode("TimeZone").InnerText; Loc._latitude = Convert.ToSingle(Root.SelectSingleNode("Latitude").InnerText, enUS); Loc._longitude = Convert.ToSingle(Root.SelectSingleNode("Longitude").InnerText, enUS); Loc._metroCode = Convert.ToInt32(Root.SelectSingleNode("MetroCode").InnerText); return Loc; } static public Location FromJson(string json) { throw new NotImplementedException(); } static public Location FromCsv(string csv) { throw new NotImplementedException(); } } }
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1