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

4 Lösungen Lösungen öffentlich
#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 ;)

#4
vote_ok
von KawaiiShox (330 Punkte) - 11.07.2017 um 10:24 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Net;
using System.IO;

namespace ConsoleApplication3
{
    class Program
    {     
        static void Main(string[] args)
        {
            Console.WriteLine("Geben Sie eine IP-Adresse oder Web-Adresse ein:");
            locationResult location = getLocationByIP(Console.ReadLine());
            if(location.Equals(new locationResult()) == false)
            {
                Console.WriteLine(String.Format("Die Ip {0} befindet sich in dem Land {1} in der Stadt {2}, die Geo Koordinaten sind {3},{4}",location.ip,location.country_code,location.city,location.longitude,location.latitude));
            }
            Console.ReadLine();
        }        
        
        
        public static locationResult getLocationByIP(string ip)
        {
            try
            {
                WebRequest request = WebRequest.Create(String.Format("https://freegeoip.net/json/{0}", ip));
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream dataStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(dataStream);
                string responseFromServer = reader.ReadToEnd();
                reader.Close();
                dataStream.Close();
                response.Close();
                return Newtonsoft.Json.JsonConvert.DeserializeObject<locationResult>(responseFromServer);
            }
            catch(Exception ex)
            {
                Console.WriteLine("Es ist ein Fehler aufgetreten: " + ex.Message);
                return new locationResult();
            }	
            
        }
           
    }

    public class locationResult
    {
        public string ip { get; set; }
        public string country_code { get; set; }
        public string country_name { get; set; }
        public string region_code { get; set; }
        public string region_name { get; set; }
        public string city { get; set; }
        public string zip_code { get; set; }
        public string time_zone { get; set; }
        public double latitude { get; set; }
        public double longitude { get; set; }
        public int metro_code { get; set; }
    }


}

Kommentare:

Für diese Lösung gibt es noch keinen Kommentar

Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben