Test two methods to obtain the status code. The first method is HttpWebRequest.
Public void GetHead (string url) {WebRequest Http = WebRequest. create (url); Http. method = "HEAD" // set Method to HEADtry {HttpWebResponse response = (HttpWebResponse) http. getResponse (); Console. write (Convert. toInt32 (response. statusCode) + "" + response. statusCode. toString (); // Statuscode is of the enumeration type, 200 is normal, and other outputs are abnormal. Status Code response must be converted to int type. close ();} catch (WebException ex) {Console. writeLine (Convert. toInt32 (HttpWebResponse) ex. response ). statusCode) + "" + (HttpWebResponse) ex. response ). statusCode. toString ());}}
Socket 2
Public class SocketGetHeadpublic 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); // obtain the webpage string strResponse = Encoding. UTF8.GetString (responseBytes, 0, iTotalCount); StringReader str = new StringReader (strResponse); string line = str. readLine (); // analyze this line string and obtain the server response status code Match M = RE. match (line, @ "\ 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) {// regenerate a larger array byte [] temp = new byte [responseBytes. length * 2]; // copy the original data to the responseBytes in the new array. copyTo (temp, 0); buffer. copyTo (temp, iTotalCount-1); responseBytes = temp; // reference change} else {buffer. copyTo (responseBytes, iTotalCount-1);} iTotalCount + = iNumber; // Add index position} return responseBytes ;}}
Time-consuming code
Stopwatch stopwatch = new Stopwatch (); stopwatch. Start (); // code... Stopwatch. Stop (); long time = stopwatch. ElapsedMilliseconds; Console. WriteLine ("code execution time:" + time. ToString () + "ms ");
We found that HttpWebRequest took 2330 ms, socket took 608 ms, and the difference was almost 4 times. over