Httpsend webpage get/post gadgets released

Source: Internet
Author: User

It's okay to write your own tools.

The core code is as follows: it uses httpwebrequest to capture website analysis data and automatically capture website code.
Public static string postdata (string data, string method, string posturl) <br/>{< br/> httpwebrequest request; <br/> httpwebresponse response; <br/> asciiencoding encoding = new asciiencoding (); <br/> request = webrequest. create (posturl) as httpwebrequest; <br/> request. protocolversion = httpversion. version10; <br/> request. allowautoredirect = true; <br/> request. keepalive = true; <br/> request. he Aders. add ("Accept-language", "ZH-CN"); <br/> request. accept = "image/GIF, image/JPEG, image/pjpeg, image/pjpeg, application/XAML + XML, application/vnd. MS-xpsdocument, application/X-MS-xbap, application/X-MS-application, application/X-Shockwave-flash, application/vnd. MS-Excel, application/vnd. MS-PowerPoint, application/MSWord, */* "; <br/> request. useragent = "Mozilla/4.0 (compatible; MSIE 8.0; Windo Ws NT 5.2; Trident/4.0 ;. net CLR 2.0.50727 ;. net CLR 3.0.04506.30 ;. net CLR 3.0.20.6.2152 ;. net CLR 3.5.30729; infopath.2; CBA ;. net4.0c ;. net4.0e) "; <br/> request. method = "get"; <br/> If (Method = "Post") <br/>{< br/> byte [] B = encoding. getbytes (data); <br/> request. contenttype = "application/X-WWW-form-urlencoded"; <br/> request. contentlength = B. length; <br/> request. method = "Post"; <br/> Using (Stream stream = request. getrequeststream () <br/>{< br/> stream. write (B, 0, B. length); <br/>}</P> <p> string html = string. empty; </P> <p> try <br/> {<br/> // obtain the resources returned by the server <br/> using (response = request. getresponse () as httpwebresponse) <br/>{< br/> using (streamreader reader = new streamreader (response. getresponsestream (), encoding. getencoding ("UTF-8") <br/>{< br/> html = Reader. Readtoend (); <br/> RegEx reg_charset = new RegEx (@ "charset/B/S * =/S *(? <Charset> [^ ""] *) "); <br/> If (reg_charset.ismatch (HTML) <br/>{< br/> return postdata (data, method, posturl, reg_charset.match (HTML ). groups ["charset"]. value); <br/>}< br/> else if (response. characterset! = String. empty) <br/>{< br/> return postdata (data, method, posturl, response. characterset); <br/>}< br/> else <br/> return postdata (data, method, posturl, encoding. default. bodyname); </P> <p >}< br/> catch (webexception Wex) <br/> {<br/> webresponse wR = Wex. response; <br/> using (Stream ST = Wr. getresponsestream () <br/>{< br/> using (streamreader sr = new streamreader (St, system. text. encoding. default) <br/>{< br/> html = sr. readtoend (); <br/>}< br/> catch (exception ex) <br/>{< br/> html = "Exception/n/R" + ex. message; <br/>}< br/> return HTML; <br/>}</P> <p> Public static string postdata (string data, string method, string posturl, string encode) <br/>{< br/> httpwebrequest request; <br/> httpwebresponse response; <br/> asciiencoding encoding = new asciiencoding (); <br/> request = webrequest. create (posturl) as httpwebrequest; <br/> request. protocolversion = httpversion. version10; <br/> request. allowautoredirect = true; <br/> request. keepalive = true; <br/> request. headers. add ("Accept-language", "ZH-CN"); <br/> request. accept = "image/GIF, image/JPEG, image/pjpeg, image/pjpeg, application/XAML + XML, application/vnd. MS-xpsdocument, application/X-MS-xbap, application/X-MS-application, application/X-Shockwave-flash, application/vnd. MS-Excel, application/vnd. MS-PowerPoint, application/MSWord, */* "; <br/> request. useragent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0 ;. net CLR 2.0.50727 ;. net CLR 3.0.04506.30 ;. net CLR 3.0.20.6.2152 ;. net CLR 3.5.30729; infopath.2; CBA ;. net4.0c ;. net4.0e) "; <br/> request. method = "get"; <br/> If (Method = "Post") <br/>{< br/> byte [] B = encoding. getbytes (data); <br/> request. contenttype = "application/X-WWW-form-urlencoded"; <br/> request. contentlength = B. length; <br/> request. method = "Post"; <br/> using (Stream stream = request. getrequeststream () <br/>{< br/> stream. write (B, 0, B. length); <br/>}</P> <p> string html = string. empty; </P> <p> try <br/> {<br/> // obtain the resources returned by the server <br/> using (response = request. getresponse () as httpwebresponse) <br/>{< br/> using (streamreader reader = new streamreader (response. getresponsestream (), encoding. getencoding (encode) <br/>{< br/> html = reader. readtoend (); </P> <p >}< br/> catch (webexception Wex) <br/> {<br/> webresponse wR = Wex. response; <br/> using (Stream ST = Wr. getresponsestream () <br/>{< br/> using (streamreader sr = new streamreader (St, system. text. encoding. default) <br/>{< br/> html = sr. readtoend (); <br/>}< br/> catch (exception ex) <br/>{< br/> html = "Exception/n/R" + ex. message; <br/>}< br/> return HTML; <br/>}

 

 

Http://download.csdn.net/source/2670822

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.