程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c# 獲取HTTP狀態碼

c# 獲取HTTP狀態碼

編輯:C#入門知識

測試兩種方法獲取狀態碼 第一種 HttpWebRequest

public void GetHead(string url)
{
WebRequest Http = WebRequest.Create(url);
Http.Method="HEAD"//設置Method為HEAD
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());
            }
} 

 

  第二種 socket    
 public class SocketGetHead
public string GetHtml(string strUrl)
        {

            string strHost = GetHost(strUrl);
            int port = 80;
            if (strHost.IndexOf(":") != -1)
            {
                port = int.Parse(strHost.Substring(strHost.IndexOf(":") + 1));
                strHost = strHost.Substring(0, strHost.IndexOf(":"));
            }
            string strGetHeader = "";
            strGetHeader += "HEAD / HTTP/1.0\n";
            strGetHeader += "Host: " + strHost + "\n";
            strGetHeader += "\n";

            byte[] getBytes = Encoding.ASCII.GetBytes(strGetHeader);
            try
            {
                int iTotalCount;
                byte[] responseBytes = GetHtmlOriginByte(strHost, port, getBytes, out iTotalCount); //得到網頁   
                string strResponse = Encoding.UTF8.GetString(responseBytes, 0, iTotalCount);

                StringReader str = new StringReader(strResponse);
                string line = str.ReadLine();

                //分析此行字符串,獲取服務器響應狀態碼
                Match M = RE.Match(line, @"\d\d\d");
                return M.Value;


            }
            catch (System.Exception ex)
            {
                return "error";
            }
        }
private byte[] GetHtmlOriginByte(string strHost, int port, byte[] getBytes, out int iTotalCount)
        {
            int iReponseByteSize = 400 * 1024;
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Connect(strHost, port);
            socket.Send(getBytes);
            byte[] buffer = new byte[256];
            byte[] responseBytes = new byte[iReponseByteSize];
            int iNumber = socket.Receive(buffer, buffer.Length, SocketFlags.None);
            iTotalCount = iNumber;
            buffer.CopyTo(responseBytes, 0);
            while (iNumber > 0)
            {
                iNumber = socket.Receive(buffer, buffer.Length, SocketFlags.None);
                if (iTotalCount + iNumber >= responseBytes.Length)
                {
                    //重新生成個更大的數組   
                    byte[] temp = new byte[responseBytes.Length * 2];
                    //原數據copy到新數組中   
                    responseBytes.CopyTo(temp, 0);
                    buffer.CopyTo(temp, iTotalCount - 1);
                    responseBytes = temp; //引用變更   
                }
                else
                {
                    buffer.CopyTo(responseBytes, iTotalCount - 1);
                }
                iTotalCount += iNumber; //索引位置增加   
            }
            return responseBytes;
        }
} 

 

  時間耗時代碼
Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            //代碼。。。
              stopwatch.Stop();
            long time = stopwatch.ElapsedMilliseconds;
            Console.WriteLine("代碼執行時間為:" + time.ToString()+"ms"); 

 

  對比發現HttpWebRequest 耗時2330ms,socket耗時608ms,相差差不多4倍.over  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved