C# Http Response Header 打印

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("****************************************************************\n");
            Console.WriteLine("Http Response Header Resolver V1.00.00\n");
            Console.WriteLine("input a url for example:www.google.com\n");
            Console.WriteLine("input q to quit the program\n");
            Console.WriteLine("****************************************************************\n");

            while(true)
            {
                Console.Write(">");
                string url = Console.ReadLine();

                if(url=="q")
                {
                    break;
                }

                try
                {

                    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://" + url);
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                    //协议版本号,状态码,状态描述
                    Console.WriteLine(String.Format("{0,-20}HTTP/{1} {2:d} {3}","(Status-Line)",response.ProtocolVersion,response.StatusCode,response.StatusDescription));
                    
                    //Http response头
                    for(int i=0;i<response.Headers.Keys.Count;i++)
                    {
                        Console.WriteLine(String.Format("{0,-20}{1}",response.Headers.Keys[i],response.Headers.Get(i)));
                    }

                    foreach(Cookie c in response.Cookies)
                    {
                        Console.WriteLine(c.ToString());
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }
    }

  

posted @ 2011-09-02 22:54  OYJJ  阅读(5084)  评论(0编辑  收藏  举报