The beginning of the new year, busy to busy, or to write something, or more lazy, sharing my written Httptooler
public delegate void Requestcompleted (object sender, string html); public enum RequestType {WebRequest, HttpClient, WebClient} public class Httptooler { private static byte[] Postbuffer; private HttpWebRequest request; Private Const string ContentType = "Application/x-www-form-urlencoded;charset=utf-8"; Private Const string multiparttype = "Multipart/form-data; Boundary= "; Private Const string formboundary = "----Webkitformboundary"; Private Const string Accept = "text/html, Application/xhtml+xml, */*"; Private Const string useragent = "mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/45.0.2454.101 safari/537.36 "; Private Const string cachectr = "Cache-control"; Private Const string NoCache = "No-cache"; private bool Changeflag; private string Olddomain; private string NewDomain; Public Cookiecontainer container;Public requestcompleted requestcompleted {private get; Set } public Httptooler () {container = new Cookiecontainer (); } public Httptooler (Cookiecontainer container) {This.container = container; The public void Fixcookiedomain (string oldstr, String newstr) {Changeflag = true; Olddomain = Oldstr; NewDomain = Newstr; } private void getrequesthtml (string url) {HttpWebRequest request = (HttpWebRequest) webrequest.c reate (URL); Request. Method = "GET"; Request. KeepAlive = true; Request. Timeout = 30000; Request. ContentType = ContentType; Request. Accept = accept; Request. useragent = useragent; Request. Headers.add (cachectr, NoCache); Request. AllowAutoRedirect = false; Request. Cookiecontainer = container; RequesT.keepalive = true; HttpWebResponse response = (HttpWebResponse) request. GetResponse (); String html = string. Empty; using (Stream SW = response. GetResponseStream ()) {using (StreamReader reader = new StreamReader (sw)) { HTML = reader. ReadToEnd (); } if (Changeflag) fixcookiedomain (response); } if (response! = NULL) response. Close (); docompleted (HTML); } public void gethtml (RequestType type, string url) {if (type = = Requesttype.webrequest) {getrequesthtml (URL); } else if (type = = Requesttype.webclient) {getwebclienthtml (URL); } else {gethttpclienthtml (URL); }} private void getwebclienthtml (string url) {WebClient WC = new WebClient (); Wc. Credentials = CredentialCache.DefaultCredentials; byte[] data = WC. Downloaddata (URL); String html = Encoding.UTF8.GetString (data); docompleted (HTML); } private void gethttpclienthtml (string url) {HttpClient HC = new HttpClient (container); Hc. Credentials = CredentialCache.DefaultCredentials; byte[] data = HC. Downloaddata (URL); String html = Encoding.UTF8.GetString (data); docompleted (HTML); } public void PostData (string url, string param, string referer) {HttpWebRequest request = (Http WebRequest) webrequest.create (URL); Request. Method = "POST"; if (string. IsNullOrEmpty (Referer)) request. Referer = Referer; Request. KeepAlive = true; Request. Timeout = 30000; Request. ContentType = ContentType; Request. Accept = accept; Request. useragent = useragent; RequeSt. Headers.add (cachectr, NoCache); Request. AllowAutoRedirect = false; Request. Cookiecontainer = container; Request. KeepAlive = true; byte[] buff = Encoding.UTF8.GetBytes (param); Stream Reqstream = Request. GetRequestStream (); Reqstream.write (buff, 0, buff.) Length); Reqstream.close (); HttpWebResponse response = (HttpWebResponse) request. GetResponse (); String html = string. Empty; using (Stream SW = response. GetResponseStream ()) {using (StreamReader reader = new StreamReader (sw)) { HTML = reader. ReadToEnd (); } string Strcook = Response. headers["Set-cookie"]; if (Changeflag) fixcookiedomain (response); } if (response! = NULL) response. Close (); docompleted (HTML); } public void Postdataasync (string url, string param) {HttpWebRequest request = (HttpWebRequest) webrequest.create (URL); Request. Method = "POST"; Request. KeepAlive = true; Request. Timeout = 30000; Request. ContentType = ContentType; Request. Cookiecontainer = container; Postbuffer = Encoding.UTF8.GetBytes (param); Request. BeginGetRequestStream (New AsyncCallback (Requeststreamasync), request); } private void Docompleted (string rlt) {if (requestcompleted! = null) { Requestcompleted (this, RLT); }} private void Requeststreamasync (IAsyncResult result) {HttpWebRequest request = (HTTPW Ebrequest) result. asyncstate; Stream Reqstream = Request. EndGetRequestStream (result); Reqstream.write (postbuffer, 0, postbuffer.length); Reqstream.close (); Request. BeginGetResponse (New AsyncCallback (Responseasync), request); } Private VOID Responseasync (IAsyncResult result) {HttpWebRequest req = (HttpWebRequest) result. asyncstate; HttpWebResponse response = (HttpWebResponse) req. EndGetResponse (result); String html = string. Empty; using (Stream SW = response. GetResponseStream ()) {using (StreamReader reader = new StreamReader (sw)) { HTML = reader. ReadToEnd (); }} if (response! = NULL) response. Close (); docompleted (HTML); } public void Multipostdata (string url, string referer, dictionary<string, string> postdata) { HttpWebRequest request = (HttpWebRequest) webrequest.create (URL); Request. Method = "POST"; Request. KeepAlive = true; Request. Timeout = 30000; String boundary = Formboundary + DateTime.Now.Ticks.ToString ("x"); Request. ContentType = string. Format ("{0}{1}", multipartType, boundary); Request. Accept = accept; Request. useragent = useragent; Request. Headers.add (cachectr, NoCache); Request. AllowAutoRedirect = false; Request. Cookiecontainer = container; Request. Servicepoint.expect100continue = false; if (referer! = string. Empty) Request. Referer = Referer; byte[] Buff = buildmultipostdata (boundary, postdata); BinaryWriter bw = new BinaryWriter (Request. GetRequestStream ()); Bw. Write (Buff); Bw. Close (); HttpWebResponse response = (HttpWebResponse) request. GetResponse (); String html = string. Empty; using (Stream SW = response. GetResponseStream ()) {using (StreamReader reader = new StreamReader (sw)) { HTML = reader. ReadToEnd (); }//The first time to commit the change cookie if (Changeflag) {fixcookiedomain (response);} } if (response! = NULL) response. Close (); docompleted (HTML); } private byte[] Buildmultipostdata (string boundary, dictionary<string, string> postdata) { StringBuilder sb = new StringBuilder (); Append access token. Sb. Appendline ("--" + boundary); Sb. Append (Environment.NewLine); Append form part. if (postdata! = null && postdata.count > 0) {foreach (keyvaluepair<string, string > Httppostdataitem in PostData) {sb. Appendline ("--" + boundary); Sb. Appendline (String. Format ("content-disposition:form-data;name=\" {0}\ "", Httppostdataitem.key)); Sb. Append (Environment.NewLine); Sb. Appendline (Httppostdataitem.value); }} MemoryStream ms = new MemoryStream (); BinaryWriter bw = new BinaryWriter (MS); Bw. Write (Encoding.UTF8.GetBytes (sb.) ToString ())); Bw. Write (Encoding.UTF8.GetBytes (Environment.NewLine)); Bw. Write (Encoding.UTF8.GetBytes ("--" + Boundary + "--")); Bw. Write (Encoding.UTF8.GetBytes (Environment.NewLine)); Ms. Flush (); Ms. Position = 0; Byte[] result = Ms. ToArray (); Bw. Close (); return result; private void Fixcookiedomain (HttpWebResponse response) {string Strcookie = response. headers["Set-cookie"]; Cookiecollection collect = Getcookiesfromreponsehead (Strcookie, Olddomain); foreach (Cookie cook in collect) {Cookie Ncook = new Cookie (); Ncook. Domain = Olddomain; Ncook. Name = Cook. Name; Ncook. Value = Cook. Value; Cookie Pcook = Findcookie (NewDomain, Cook. Name); if (Pcook = = null) {container. AdD (Ncook); } else {pcook = Ncook; }} Changeflag = false; } Private Cookiecollection Getcookiesfromreponsehead (string setcookieheadstr, String defaultdomain) { Cookiecollection cookies = new Cookiecollection (); if (!string. IsNullOrEmpty (SETCOOKIEHEADSTR)) {setcookieheadstr = Setcookieheadstr.replace ("HttpOnly,", Stri Ng. Empty). Replace ("HttpOnly", String. Empty); foreach (String str in setcookieheadstr.split (new string[] {"path=/,", "path=/,", "path=/;", "path=/;", "path=/;"}, Stri Ngsplitoptions.none)) {string cookiestring = str. Trim (); if (Cookiestring.indexof (', ') = = 0) {cookiestring = cookiestring.substring (1); } try {Cookie cookiefromstring = getcookiefromstring (cookiestring, DefaultDomain); if (cookiefromstring! = null) {cookies. ADD (cookiefromstring); }} catch (Exception Exception) {//logman Ager. Error ("Getcookiesfromreponsehead", exception); }}} return cookies; } private static Cookie Getcookiefromstring (string cookiestring, string defaultdomain) {Cookie C Ookie = new Cookie (); string[] Strarray = Cookiestring.split (new char[] {'; ', ', '}); int index = strarray[0]. IndexOf ("="); if (index = =-1) {return null; } if (!strarray[0]. ToLower (). StartsWith ("domain=") &&!strarray[0]. ToLower (). StartsWith ("expires=")) {cookie. Name = strArray[0]. Substring (0, index); Cookies. Value = strarray[0]. Substring (index + 1); } else {BOOL flag = false; if (Strarray[0]. ToLower (). StartsWith ("domain=") && strarray[0]. Contains (",")) {strarray[0] = strarray[0]. Substring (Strarray[0]. IndexOf (', ') + 1); index = strarray[0]. IndexOf ("="); if (Index >-1) {cookie. Name = strarray[0]. Substring (0, index); Cookies. Value = strarray[0]. Substring (index + 1); Flag = true; }} if (!flag) {for (int j = 1; j < Strarray.length; j + +) {index = Strarray[j]. IndexOf ("="); if (Index > 0) {string str = StrarraY[J]. Substring (0, index); if (!str. ToLower (). StartsWith ("domain=") &&!str. ToLower (). StartsWith ("expires=")) &&!str. ToLower (). StartsWith ("version=")) {string str2 = Strarray[j]. Substring (index + 1); Cookies. Name = str. Replace (",", ""). Trim (); Cookies. Value = str2; Flag = true; Break }}}} if (!flag) { return null; }} Hashtable Hashtable = new Hashtable (); for (int i = 0; i < strarray.length; i++) {if (!string. IsNullOrEmpty (Strarray[i])) {string str3 = Strarray[i]. Trim (); int length = Str3. IndexOf ("="); if (length > 0) {string key = Str3. Substring (0, length); if (!hashtable. ContainsKey (Key)) {string STR5 = Str3. Substring (length + 1); Hashtable. ADD (key, STR5); }}}} foreach (Object Obj2 in Hashtable. Keys) {if (obj2. ToString (). ToLower () = = "path") {cookie. Path = Hashtable[obj2]. ToString (); } else if (obj2. ToString (). ToLower () = = "Expires") {DateTime time; if (Datetime.tryparse (HASHTABLE[OBJ2). ToString (), out time)) {cookie. Expires = time; }} else if (obj2. ToString (). ToLower () = = "Domain") { if (Hashtable[obj2]. ToString (). ToLower () = = "Koubei.com") {cookie. Domain = "." + Hashtable[obj2]. ToString (); } else if (Hashtable[obj2]. ToString (). ToLower () = = "58.com") {cookie. Domain = "." + Hashtable[obj2]. ToString (); } else {cookie. Domain = Hashtable[obj2]. ToString (); }} else if (obj2. ToString (). ToLower () = = "Version") {cookie. Version = Int. Parse (Hashtable[obj2]. ToString ()); } else if (! ( Obj2. ToString (). ToLower () = = "Max-age")) {cookie. Expires = new DateTime (0x802, 1, 1); Cookies. Expired = false; }} if (cookie. Name = = "") {return null; } if (cookie. Domain = = "") {cookie. Domain = DefaultDomain; } return cookie; The private Cookie Findcookie (string Url, string name) {if (url.substring (0, 5). ToLower ()! = "http:") {URL + = "/http" + URL; } cookiecollection cookies = container. GetCookies (New Uri (URL)); if (cookies = null) {foreach (cookie cookie in cookies) {if (cookies.) name = = name) {return cookie; }}} return null; }} class Httpclient:webclient {//Cookie container private Cookiecontainer cookiecontainer; /**////<summary>///Create a new WebClient instance. </summary> public HttpClient () {This.cookiecontainer = nEW Cookiecontainer (); }/**////<summary>///Create a new WebClient instance. </summary>//<param name= "cookies" >cookie containers </param> public HttpClient (cookiecontaine R cookies) {This.cookiecontainer = cookies; }/**////<summary>//Cookies container///</summary> public Cookiecontainer Co Okies {get {return this.cookiecontainer;} set {This.cookiecontainer = value;} }/**///<summary>///return HttpWebRequest with cookies. </summary>//<param name= "Address" ></param>//<returns></returns> protected override WebRequest GetWebRequest (Uri address) {WebRequest request = base. GetWebRequest (address); If (request is HttpWebRequest) {HttpWebRequest HttpRequest = Request as HttpWebRequest; Httprequest.cookiecontainer = Cookiecontainer; } return request; } }
With WebClient downloaddata, larger data pages can be downloaded using it, much faster than request requests.
Support for cookie modification, domain redirection, rich media forms, asynchronous requests, event completion. To be a 58-city log-in minute thing.
Httptooler Tooler = new Httptooler (); Tooler. requestcompleted = Onrequestcompleted;tooler. Gethtml (Requesttype.webrequest, "www.58.com"); void onrequestcompleted (object sender, string html) { //parameter HTML Is the result of the return //Do you want ...}
That's all enjoy ...
Self-packaged HttpRequest, personal feel of the comparison httphelper easy to use