C # multiple methods for downloading webpage source code

Source: Internet
Author: User

Httprequest:

Static class webfunc <br/>{< br/> Private Static cookiecontainer cookie = new cookiecontainer (); <br/> Private Static string contenttype = "application/X-WWW-form-urlencoded"; <br/> Private Static string accept = "image/GIF, image/X-xbitmap, image/JPEG, image/pjpeg, application/X-Shockwave-flash, application/X-Silverlight, application/vnd. MS-Excel, application/vnd. MS-PowerPoint, application/MSWord, Application/X-MS-application, application/X-MS-xbap, application/vnd. MS-xpsdocument, application/XAML + XML, application/x-silverlight-2-b1, */* "; <br/> Private Static string useragent =" Mozilla/4.0 (compatible; MSIE 7.0; windows NT 5.1 ;. net CLR 2.0.50727 ;. net CLR 3.0.04506.648 ;. net CLR 3.5.21022) "; </P> <p> Public static string gethtmlex (string URL) <br/>{< br/> httpwebrequest request = (httpwe Brequest) webrequest. create (URL); <br/> request. useragent = useragent; <br/> request. contenttype = contenttype; <br/> request. cookiecontainer = cookie; <br/> request. accept = accept; <br/> request. method = "get"; </P> <p> webresponse response = request. getresponse (); <br/> stream responsestream = response. getresponsestream (); <br/> encoding = NULL; <br/> for (INT I = 0; I <response. headers. Count; I ++) <br/>{< br/> Match m = RegEx. Match (response. headers [I]. tostring (),"(? I )(? <= Charset =) [^] + "); <br/> If (! M. success) continue; <br/> encoding = encoding. getencoding (M. value); <br/> break; <br/>}< br/> streamreader reader = new streamreader (responsestream, encoding); <br/> string html = reader. readtoend (); <br/> response. close (); </P> <p> return HTML; <br/>}< br/>}

 

WebClient:

System. net. webClient WC = new system. net. webClient (); <br/> byte [] pagedata = WC. downloaddata ("webpage address"); <br/> string S = system. text. encoding. default. getstring (pagedata); <br/>

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.