C # obtain the HTTP status code

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.