Asp.net c # three methods to obtain webpage source code

Source: Internet
Author: User

C # you can use WebClient WebRequest HttpWebRequest to obtain the original HTML code of a specified webpage.
WebClient
Private string GetWebClient (string url)
{
String strHTML = "";
WebClient myWebClient = new WebClient ();
Stream myStream = myWebClient. OpenRead (url );
StreamReader sr = new StreamReader (myStream, System. Text. Encoding. GetEncoding ("UTF-8 "));
StrHTML = sr. ReadToEnd ();
MyStream. Close ();
Return strHTML;
}


WebRequest


Private string GetWebRequest (string url)
{
Uri uri = new Uri (url );
WebRequest myReq = WebRequest. Create (uri );
WebResponse result = myReq. GetResponse ();
Stream receviceStream = result. GetResponseStream ();
StreamReader readerOfStream = new StreamReader (receviceStream, System. Text. Encoding. GetEncoding ("UTF-8 "));
String strHTML = readerOfStream. ReadToEnd ();
ReaderOfStream. Close ();
ReceviceStream. Close ();
Result. Close ();
Return strHTML;
}


HttpWebRequest


Private string GetHttpWebRequest (string url)
{
Uri uri = new Uri (url );
HttpWebRequest myReq = (HttpWebRequest) WebRequest. Create (uri );
MyReq. UserAgent = "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;. net clr 1.0.3705 ";
MyReq. Accept = "*/*";
MyReq. KeepAlive = true;
MyReq. Headers. Add ("Accept-Language", "zh-cn, en-us; q = 0.5 ");
HttpWebResponse result = (HttpWebResponse) myReq. GetResponse ();
Stream receviceStream = result. GetResponseStream ();
StreamReader readerOfStream = new StreamReader (receviceStream, System. Text. Encoding. GetEncoding ("UTF-8 "));
String strHTML = readerOfStream. ReadToEnd ();
ReaderOfStream. Close ();
ReceviceStream. Close ();
Result. Close ();
Return strHTML;
}
Note that "UTF-8" should correspond to the encoding of the specified webpage.
Summary
We can see that the HttpWebRequest method is the most complex, but it does provide more options.

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.