C # Several ways to get the specified Web page source code

Source: Internet
Author: User

 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 ("gb2312"));            String strhtml = Readerofstream.readtoend ();            Readerofstream.close ();            Recevicestream.close (); Result.            Close ();        return strhtml; }        HttpWebRequest private string gethttpwebrequest (string url) {try {                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 ("gb2312"));                String strhtml = Readerofstream.readtoend ();                Readerofstream.close ();                Recevicestream.close (); Result.                                Close ();            return strhtml;      }      catch (Exception ex) {throw new Exception ("Collect specified URL exception," + ex.)            Message); }        }

Get the Web source, if the gzip compression enabled page gets garbled, this method can be used to solve the garbled situation of gzip compression private string Gethtmlcode (string url) {Stri           Ng Htmlcode;            HttpWebRequest webRequest = (System.Net.HttpWebRequest) System.Net.WebRequest.Create (URL);            Webrequest.timeout = 30000;            Webrequest.method = "GET";            Webrequest.useragent = "mozilla/4.0";            WEBREQUEST.HEADERS.ADD ("accept-encoding", "gzip, deflate");            HttpWebResponse WebResponse = (System.Net.HttpWebResponse) webrequest.getresponse (); if (webResponse.ContentEncoding.ToLower () = = "gzip")//If gzip is used, first unzip the {using (System.IO.Stream St                        Reamreceive = WebResponse.GetResponseStream ()) {using (var ZipStream =                    New System.IO.Compression.GZipStream (Streamreceive, System.IO.Compression.CompressionMode.Decompress)) {using (StreamReader sr = new System.IO.StreamReader (ZipStream, ENcoding. Default) {Htmlcode = Sr.                        ReadToEnd (); }}}} else {using (System.IO.Stream s  Treamreceive = WebResponse.GetResponseStream ()) {using (System.IO.StreamReader sr = new System.IO.StreamReader (streamreceive, Encoding.default)) {Htmlcode = Sr.                    ReadToEnd ();        }}} return htmlcode; }

  

C # Several ways to get the specified Web page source code

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.