Пример #1
0
        /// <summary>
        /// Performes a query on the provided IP address
        ///
        /// If there is no internet connectio or any other error an exception will be thrown.
        /// This exception will need to be handeled by the application
        /// </summary>
        /// <param name="ip">IP address to look up in the form x.x.x.x or host name</param>
        /// <returns>A IPQuery object of the results</returns>
        public static async Task <IPQuery> IPLookupAsync(string ip)
        {
            //create a clinet and set the base IP
            HttpClient client = new HttpClient();

            client.BaseAddress = new Uri("http://ip-api.com/xml/");
            client.DefaultRequestHeaders.Accept.Clear();

            //call the request with the supplied IP with all fields
            HttpResponseMessage response = await client.GetAsync(ip + "?fields=262143");

            //read the response as a string
            string data = await response.Content.ReadAsStringAsync();

            //convert the XML to a IPQuery object
            XmlSerializer serializer = new XmlSerializer(typeof(IPQuery));

            using (TextReader reader = new StringReader(data))
            {
                IPQuery result = (IPQuery)serializer.Deserialize(reader);

                //return the result
                return(result);
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            //print out the help msg
            if (args.Length == 1 && args[0] == "help")
            {
                Console.WriteLine("\nPlease specify an IP address in the form of x.x.x.x, a host name, or leave blank to use your IP\n");
                return;
            }

            //print out  please wait msg
            Console.WriteLine();
            Console.Write("Please wait...");

            string ip = "";

            //if a IP was supplied set IP to that IP
            if (args.Length == 1)
            {
                ip = args[0];
            }

            //get the IP data
            Task <IPQuery> t = IPQuery.IPLookupAsync(ip);

            //display a progress while we wait
            while (!t.IsCompleted)
            {
                Console.Write(".");
                System.Threading.Thread.Sleep(500);
            }

            //Add some blank lines
            Console.WriteLine();
            Console.WriteLine();

            //get results
            IPQuery data = t.Result;

            //print out the results
            if (data.Status == "success")
            {
                Console.WriteLine(data);
            }
            //there was a error
            else
            {
                Console.WriteLine("Error: {0}", data.Error);
                Console.WriteLine("Please specify an IP address in the form of x.x.x.x, a host name, or leave blank to use your IP");
            }

            //add a blank line for easy reading
            Console.WriteLine();
        }