C # download the webpage source code to obtain the http status code

Source: Internet
Author: User

 

C # download the webpage source code to obtain the http status code

HttpWebRequest hwr = (HttpWebRequest) WebRequest. Create ("http://www.baidu.com ");

Hwr. AllowAutoRedirect = false; // redirection not allowed

Hwr. Timeout = 10000; // connection Timeout setting

Hwr. Method = "GET"; // Protocol: GET, HEAD, POST, PUT, DELETE, TRACE, or OPTIONS.

Try

{

HttpWebResponse hwrs = (HttpWebResponse) hwr. GetResponse ();

MessageBox. Show (int) hwrs. StatusCode). ToString (); // obtain the http Status Code, for example, 200, but 404 cannot be captured.

Stream stream = hwrs. GetResponseStream ();

MessageBox. Show (hwrs. CharacterSet); // get the character encoding of the returned result

StreamReader sr = new StreamReader (stream, Encoding. GetEncoding (hwrs. CharacterSet); // note that the read text Encoding format must be the same as the text Encoding format of the written file.

StreamWriter sw = new StreamWriter ("c: \ B .html", false, Encoding. GetEncoding (hwrs. CharacterSet); // The Encoding format of the written text is the same as that of the read text.

Sw. Write (sr. ReadToEnd ());

Sw. Flush ();

Sw. Close ();

Sr. Close ();

 

 

}

Catch (Exception ex)

{

MessageBox. Show (ex. ToString ());

}

From Shine's holy heaven-Min Chen 〃

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.