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 〃