HttpWebRequest.Method 属性

        public static void GetHead(string url)
       {
            var http = (HttpWebRequest)WebRequest.Create(url);
            http.Method = "HEAD";//设置Method为HEAD
            http.UserAgent = "Tujia-Auto-Test";
            try
            {
                HttpWebResponse response = (HttpWebResponse)http.GetResponse();
                Console.Write(Convert.ToInt32(response.StatusCode) + " " + response.StatusCode.ToString());//Statuscode 为枚举类型,200为正常,其他输出异常,需要转为int型才会输出状态码
                response.Close();
            }
            catch (WebException ex)
            {
                Console.WriteLine(Convert.ToInt32(((HttpWebResponse)ex.Response).StatusCode)+"  "+((HttpWebResponse)ex.Response).StatusCode.ToString());
            }
        }

可以将 Method 属性设置为任何 HTTP 1.1 协议谓词:GET、HEAD、POST、PUT、DELETE、TRACE 或 OPTIONS。

posted @ 2014-11-18 17:44  TechFans  阅读(691)  评论(0编辑  收藏  举报