c# 获取HTTP状态码 – C#编程语言程序开发技术文章_C#编程 – 红黑联盟

测试两种方法获取状态码,对比发现HttpWebRequest 耗时2330ms,socket耗时608ms,相差差不多4倍.over

第一种 HttpWebRequest

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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;
        }
}

 

 

时间耗时代码

 

?
1
2
3
4
5
6
Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            //代码。。。
              stopwatch.Stop();
            long time = stopwatch.ElapsedMilliseconds;
            Console.WriteLine("代码执行时间为:" + time.ToString()+"ms");

 

 

来源URL:http://www.2cto.com/kf/201307/226788.html