C # several examples of getting webpage source code

Source: Internet
Author: User

Method 1: Copy codeThe Code is as follows: using System. Text;
Using System. Net;
Private string getHtml (string url)
{
WebClient myWebClient = new WebClient ();
Byte [] myDataBuffer = myWebClient. DownloadData (url );
Return Encoding. Default. GetString (myDataBuffer );
}

Method 2:Copy codeThe Code is as follows: public string getHttp (string HttpUrl, string RefererUrl)
{
String html = "";
Try
{
MSXML2.XMLHTTP Http = new MSXML2.XMLHTTPClass ();
Xmlhttp. open ("GET", Url, false, null, null );
Http. open ("GET", HttpUrl, false, null, null );
Http. setRequestHeader ("Referer", RefererUrl );
// Http. setRequestHeader ("Referer", RefererUrl );
Http. setRequestHeader ("Content-Type", "text/html; charset = gb2312 ");
Http. send ("");
Html = Encoding. Default. GetString (byte []) Http. responseBody );
Http = null;
}
Catch
{
}
Return html;
}
Public bool getweb (string strURL, out string buf)
{
Buf = "";
Try
{
// Uri url = new Uri (strURL, false );
HttpWebRequest request;
Request = (HttpWebRequest) WebRequest. Create (strURL );
Request. Method = "POST"; // Post request Method
Request. ContentType = "text/html; charset = gb2312"; // content type
String paraUrlCoded = System. Web. HttpUtility. UrlEncode (""); // The parameter is URL encoded.
Byte [] payload;
Payload = System. Text. Encoding. GetEncoding ("GB2312"). GetBytes (paraUrlCoded); // convert the URL-encoded string into bytes.
Request. ContentLength = payload. Length; // you can specify the request ContentLength.
Stream writer = request. GetRequestStream (); // get the request Stream
Writer. Write (payload, 0, payload. Length); // Write request parameters to the stream
Writer. Close (); // Close the request stream
HttpWebResponse response;
Response = (HttpWebResponse) request. GetResponse (); // get the response stream
Stream s;
S = response. GetResponseStream ();
StreamReader objReader = new StreamReader (s, System. Text. Encoding. GetEncoding ("GB2312 "));
String HTML = "";
String sLine = "";
Int I = 0;
While (sLine! = Null)
{
I ++;
SLine = objReader. ReadLine ();
If (sLine! = Null)
HTML + = sLine;
}
// HTML = HTML. Replace ("<", "<");
// HTML = HTML. Replace (">", "> ");
Buf = HTML;
Return true;
}
Catch (Exception x)
{
Buf = x. Message. ToString ();
Return false;
}
}

Cookie:Copy codeThe Code is as follows: CookieContainer cc = new CookieContainer ();
Public bool getweb (string strURL, out string buf)
{
Buf = "";
Try
{
HttpWebRequest request;
Request = (HttpWebRequest) WebRequest. Create (strURL );
Request. Method = "POST"; // Post request Method
Request. ContentType = "text/html; charset = gb2312"; // content type
String paraUrlCoded = System. Web. HttpUtility. UrlEncode (""); // The parameter is URL encoded.
Byte [] payload;
Payload = System. Text. Encoding. GetEncoding ("GB2312"). GetBytes (paraUrlCoded); // convert the URL-encoded string into bytes.
Request. ContentLength = payload. Length; // you can specify the request ContentLength.
Stream writer = request. GetRequestStream (); // get the request Stream
Writer. Write (payload, 0, payload. Length); // Write request parameters to the stream
Writer. Close (); // Close the request stream
HttpWebResponse response;
Response = (HttpWebResponse) request. GetResponse (); // get the response stream
Stream s;
S = response. GetResponseStream ();
StreamReader objReader = new StreamReader (s, System. Text. Encoding. GetEncoding ("GB2312 "));
String HTML = "";
String sLine = "";
Int I = 0;
While (sLine! = Null)
{
I ++;
SLine = objReader. ReadLine ();
If (sLine! = Null)
HTML + = sLine;
}
Buf = HTML;
Return true;
}
Catch (Exception x)
{
Buf = x. Message. ToString ();
Return false;
}
}
Public bool getweb (string strURL, out string buf, string postData)
{
Buf = "";
Try
{
ASCIIEncoding encoding = new ASCIIEncoding ();
Byte [] data = encoding. GetBytes (postData );
HttpWebRequest request = (HttpWebRequest) WebRequest. Create (strURL );
Request. Method = "POST ";
Request. ContentType = "application/x-www-form-urlencoded ";
Request. ContentLength = data. Length;
Stream newStream = request. GetRequestStream ();
NewStream. Write (data, 0, data. Length );
NewStream. Close ();
Request. CookieContainer = cc;
HttpWebResponse response = (HttpWebResponse) request. GetResponse ();
Cc. Add (response. Cookies );
Stream stream = response. GetResponseStream ();
String sHtml = new StreamReader (stream, System. Text. Encoding. Default). ReadToEnd ();
Buf = sHtml;
Return true;
}
Catch (Exception x)
{
Buf = x. Message. ToString ();
Return false;
}
}
Private string getWebresourceFile1 (string url)
{
WebClient myWebClient = new WebClient ();
Byte [] myDataBuffer = myWebClient. DownloadData (url );
String SourceCode = Encoding. Default. GetString (myDataBuffer );
SaveSourceCode (SourceCode );
Return SourceCode;
}

Method 2Copy codeThe Code is as follows: private string getWebresourceFile2 (string url)
{
HttpWebRequest request = (HttpWebRequest) WebRequest. Create (url );
HttpWebResponse response = (HttpWebResponse) request. GetResponse ();
Request. Method = "GET ";
Stream receiveStream = response. GetResponseStream ();
StreamReader readStream = new StreamReader (receiveStream, Encoding. Default );
String SourceCode = readStream. ReadToEnd ();
SaveSourceCode (SourceCode );
Response. Close ();
ReadStream. Close ();
Return SourceCode;
}

Method 3Copy codeThe Code is as follows: private string getWebresourceFile3 (string url)
{
WebClient wc = new WebClient ();
Wc. Credentials = CredentialCache. DefaultCredentials;
Byte [] pageData = wc. DownloadData (url );
String SourceCode = Encoding. Default. GetString (pageData );
SaveSourceCode (SourceCode );
Wc. Dispose ();
Return SourceCode;
}

Method 4Copy codeThe Code is as follows: private string getWebresourceFile4 (string url)
{
WebClient wc = new WebClient ();
Wc. Credentials = CredentialCache. DefaultCredentials;
Stream resStream = wc. OpenRead (url );
StreamReader sr = new StreamReader (resStream, System. Text. Encoding. Default );
String SourceCode = sr. ReadToEnd ();
SaveSourceCode (SourceCode );
ResStream. Close ();
Wc. Dispose ();
Return SourceCode;
}

Method 5Copy codeThe Code is as follows: private string getWebresourceFile5 (string url)
{
WebRequest request = WebRequest. Create (url );
WebResponse response = request. GetResponse ();
Stream resStream = response. GetResponseStream ();
StreamReader sr = new StreamReader (resStream, System. Text. Encoding. Default );
String SourceCode = sr. ReadToEnd ();
SaveSourceCode (SourceCode );
ResStream. Close ();
Sr. Close ();
Return SourceCode;
}

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.