C # simulates HTTP request Web page data [web crawler]

Source: Internet
Author: User

Using system;using system.collections.specialized;using system.io;using system.linq;using System.Net;using System.text;using system.text.regularexpressions;using FTE. Framework.log4netservice;namespace proxy.bllservices{//<summary>//HTTP access class///</summary> Pub Lic class Httphelper {///<summary>///access failed unified return character////</summary> public Stri        ng Errorreturn {get; private set;} = "Httphelper access error!"; <summary>////cookies///</summary> private Cookiecontainer cookie = new Cookie        Container (); <summary>///HTTP POST access webpage///</summary>//<param name= "URL" ></param&gt        ; <param name= "Postdatastr" ></param>///<returns></returns> public string Httppos Tstring (String Url, String postdatastr) {try {HttpWebRequest request = (HttPwebrequest) WebRequest.Create (URL); Request.                Method = "POST"; Request.                ContentType = "application/x-www-form-urlencoded"; Request.                ContentLength = Encoding.UTF8.GetByteCount (POSTDATASTR); Request.                Cookiecontainer = Cookie; Stream Myrequeststream = Request.                GetRequestStream ();                StreamWriter mystreamwriter = new StreamWriter (Myrequeststream, encoding.getencoding ("gb2312"));                Mystreamwriter.write (POSTDATASTR);                Mystreamwriter.close (); HttpWebResponse response = (HttpWebResponse) request.                GetResponse (); Response. Cookies = cookie.getcookies (response.                ResponseUri); Stream Myresponsestream = Response.                GetResponseStream ();                StreamReader Mystreamreader = new StreamReader (Myresponsestream, encoding.getencoding ("Utf-8"));                String retstring = Mystreamreader.readtoend ();            Mystreamreader.close ();    Myresponsestream.close ();            return retstring;            } catch (Exception ex) {LoggerManagerSingle.Instance.Error ("HTTP Post site Error", ex);        } return Errorreturn; public string HttpGet (string Url, String postdatastr) {try {HttpWeb                Request Request = (HttpWebRequest) webrequest.create (Url + (Postdatastr = = ""? "": "?") + Postdatastr); Request.                Method = "GET"; Request.                ContentType = "Text/html;charset=utf-8"; Request.                Cookiecontainer = Cookie; HttpWebResponse response = (HttpWebResponse) request.                GetResponse (); Stream Myresponsestream = Response.                GetResponseStream ();                StreamReader Mystreamreader = new StreamReader (Myresponsestream, encoding.getencoding ("Utf-8"));                String retstring = Mystreamreader.readtoend ();           Mystreamreader.close ();     Myresponsestream.close ();            return retstring;            } catch (Exception ex) {LoggerManagerSingle.Instance.Error ("HTTP Get site Error", ex);        } return Errorreturn; }///<summary>///Use form to post data [not including files]///</summary>//<param name= "url" &        gt;</param>//<param name= "stringdict" ></param>///<returns></returns>                public string httppostform (string url, NameValueCollection stringdict) {try {                String responsecontent;                var memstream = new MemoryStream ();                var webRequest = (HttpWebRequest) webrequest.create (URL);                The boundary character var boundary = "---------------" + DateTime.Now.Ticks.ToString ("x");                Boundary character var beginboundary = Encoding.ASCII.GetBytes ("--" + boundary + "\ r \ n"); At lastTerminator var endboundary = Encoding.ASCII.GetBytes ("--" + Boundary + "--\r\n");                Set Property Webrequest.cookiecontainer = Cookie;                Webrequest.method = "POST"; Webrequest.contenttype = "Multipart/form-data;                boundary= "+ boundary; Write String key var Stringkeyheader = "\r\n--" + Boundary + "\r\nconte Nt-disposition:form-data;                Name=\ "{0}\" "+" \r\n\r\n{1}\r\n ";  foreach (byte[] formitembytes in from string key in Stringdict.keys Select String.                                                 Format (Stringkeyheader, Key, Stringdict[key]) into Formitem Select Encoding.UTF8.GetBytes (Formitem)) {Memstre Am. Write (formitembytes, 0, Formitembytes.                Length);          }      Writes the last end of the bounding character Memstream.write (endboundary, 0, endboundary.length);                Webrequest.contentlength = Memstream.length;                var requeststream = Webrequest.getrequeststream ();                memstream.position = 0;                var tempbuffer = new Byte[memstream.length];                Memstream.read (tempbuffer, 0, tempbuffer.length);                Memstream.close ();                requestStream.Write (tempbuffer, 0, tempbuffer.length);                Requeststream.close ();                var HttpWebResponse = (HttpWebResponse) webrequest.getresponse ();                                                                Using var httpstreamreader = new StreamReader (Httpwebresponse.getresponsestream (), Encoding.GetEncoding ("Utf-8"))) {responsecontent = Httpstreamr Eader.                ReadToEnd ();                } httpwebresponse.close ();                Webrequest.abort (); Return responSecontent; } catch (Exception ex) {LoggerManagerSingle.Instance.Error ("HTTP form Post site error", ex            );        } return Errorreturn; }    }}

Invocation Example:

1.helper. Httppoststring ("HTTP://192.168.1.1/", "Luci_username=root&luci_password=password");//2.namevaluecollection Stringdict = new NameValueCollection (); Stringdict.add ("token", token); Stringdict.add ("Cbid.wireless.default_ Radio1.ssid "," Evertestwifi "); helper. Httppostdata ("Http://192.168.1.1/cgi-bin/luci/admin/network/wireless/radio1.network2", stringdict);

Reference connection:

Http://www.cnblogs.com/xssxss/archive/2012/07/03/2574554.html

http://blog.csdn.net/flymorn/article/details/6769722

C # simulates HTTP request Web page data [web crawler]

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.