示例#1
0
        static void Main(string[] args)
        {
            //////////////////////////
            // Fill in your details //
            //////////////////////////
            string username = "******";
            string password = "******";
            string domain   = "google.com";

            /////////////////////////
            // Use a JSON resource //
            /////////////////////////
            string format = "JSON";
            string url    = "http://www.whoisxmlapi.com/whoisserver/WhoisService?domainName=" + domain + "&username="******"&password="******"&outputFormat=" + format;

            // Create our JSON parser
            JavaScriptSerializer jsc = new JavaScriptSerializer();

            jsc.RegisterConverters(new JavaScriptConverter[] { new DynamicJsonConverter() });

            // Download and parse the JSON into a dynamic object
            dynamic result = jsc.Deserialize(new System.Net.WebClient().DownloadString(url), typeof(object)) as dynamic;

            // Print a nice informative string
            try
            {
                Console.WriteLine("JSON:\n");
                result.PrintPairs();
            }
            catch (Exception e)
            {
                try
                {
                    Console.WriteLine("JSON:\nErrorMessage:\n\t{0}", result.ErrorMessage.msg);
                }
                catch (Exception e2)
                {
                    Console.WriteLine("An unkown error has occurred!");
                }
            }

            /////////////////////////
            // Use an XML resource //
            /////////////////////////
            format = "XML";
            url    = "http://www.whoisxmlapi.com/whoisserver/WhoisService?domainName=" + domain + "&username="******"&password="******"&outputFormat=" + format;

            var         settings = new XmlReaderSettings();
            var         reader   = XmlReader.Create(url, settings);
            WhoisRecord record   = new WhoisRecord();

            try
            {
                var serializer = new System.Xml.Serialization.XmlSerializer(typeof(WhoisRecord));
                record = (WhoisRecord)serializer.Deserialize(reader);

                reader.Close();

                // Print a nice informative string
                Console.WriteLine("XML:");
                record.PrintToConsole();
            }
            catch (Exception e)
            {
                try
                {
                    var          serializer   = new System.Xml.Serialization.XmlSerializer(typeof(ErrorMessage));
                    ErrorMessage errorMessage = (ErrorMessage)serializer.Deserialize(reader);

                    reader.Close();

                    // Print a nice informative string
                    Console.WriteLine("XML:\nErrorMessage:\n\t{0}", errorMessage.msg);
                }
                catch (Exception e2)
                {
                    Console.WriteLine("XML:\nException: {0}", e2.Message);
                }
            }

            // Prevent command window from automatically closing during debugging
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            //////////////////////////
            // Fill in your details //
            //////////////////////////
            string username = "******";
            string password = "******";
            string domain = "google.com";

            /////////////////////////
            // Use a JSON resource //
            /////////////////////////
            string format = "JSON";
            string url = "http://www.whoisxmlapi.com/whoisserver/WhoisService?domainName=" + domain + "&username="******"&password="******"&outputFormat=" + format;
            
            // Create our JSON parser
            JavaScriptSerializer jsc = new JavaScriptSerializer();
            jsc.RegisterConverters(new JavaScriptConverter[] { new DynamicJsonConverter() });        

            // Download and parse the JSON into a dynamic object
            dynamic result = jsc.Deserialize(new System.Net.WebClient().DownloadString(url), typeof(object)) as dynamic;

            // Print a nice informative string
            try
            {
                Console.WriteLine("JSON:\n");
                result.PrintPairs();
            }
            catch (Exception e)
            {
                try
                {
                    Console.WriteLine("JSON:\nErrorMessage:\n\t{0}", result.ErrorMessage.msg);
                }
                catch (Exception e2)
                {
                    Console.WriteLine("An unkown error has occurred!");
                }
            }

            /////////////////////////
            // Use an XML resource //
            /////////////////////////
            format = "XML";
            url = "http://www.whoisxmlapi.com/whoisserver/WhoisService?domainName=" + domain + "&username="******"&password="******"&outputFormat=" + format;
            
            var settings = new XmlReaderSettings();
            var reader = XmlReader.Create(url, settings);
            WhoisRecord record = new WhoisRecord();
            
            try
            {
                var serializer = new System.Xml.Serialization.XmlSerializer(typeof(WhoisRecord));
                record = (WhoisRecord)serializer.Deserialize(reader);

                reader.Close();

                // Print a nice informative string
                Console.WriteLine("XML:");
                record.PrintToConsole();
            }
            catch (Exception e)
            {
                try
                {
                    var serializer = new System.Xml.Serialization.XmlSerializer(typeof(ErrorMessage));
                    ErrorMessage errorMessage = (ErrorMessage)serializer.Deserialize(reader);

                    reader.Close();

                    // Print a nice informative string
                    Console.WriteLine("XML:\nErrorMessage:\n\t{0}", errorMessage.msg);
                }
                catch (Exception e2)
                {
                    Console.WriteLine("XML:\nException: {0}", e2.Message);
                }
            }

            // Prevent command window from automatically closing during debugging
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }