HttpRequest rewriting solves the problem of resource overlay, link timeout, and multipart download Event Notifications ., Httprequest Rewriting
Using System; using System. collections. generic; using System. componentModel; using System. IO; using System. IO. compression; using System. linq; using System. net; using System. text; using System. web; namespace MyApi {public class HttpRequest {public HttpRequest () {// string Url = "http: // localhost: 5192/api/Cab/5 "; // string httpMethod = "PUT"; // string httpContent = "{'name': 'b90', 'color': 'green', 'height ': 1590, 'width': 4500} "; // Encoding httpCode = Encoding. default; // Console. writeLine (Get (Url, httpMethod, httpContent, httpCode); setEncoding = Encoding. default;} public HttpRequest (string url, string method): base () {setUrl = url; setMethod = method ;} /// <summary> /// service request address http://www.aaa.com //// </summary> public string setUrl {get; set ;} /// <summary> /// Service Request Method: GET/POST/PUT/DELETE // /</Summary> public string setMethod {get; set ;} /// <summary> /// set the service request data type /// </summary> [DefaultValue ("text/html")] public string setContentType {get; set ;} /// <summary> /// set the Service Page Encoding /// </summary> public Encoding setEncoding {get; set;} public delegate void DownloadStartDelegate (int httpStatusCode ); public delegate void DownloadProcessDelegate (long totalLength, long DownloadedByte, fl Oat percent); public delegate void DownloadEndDelegate (long totalLength); public event DownloadStartDelegate DownloadStart; public event DownloadProcessDelegate DownloadProcess; public event DownloadEndDelegate DownloadEnd; public string Get () {return Get (setUrl);} public string Get (string setUrl) {return Get (setUrl, "GET", null, setEncoding);} public string Get (string setUrl, string httpConte Nt) {return Get (setUrl, "POSE", httpContent, setEncoding);} public string Get (string Url, string httpMethod, string httpContent, Encoding httpCode) {if (string. isNullOrWhiteSpace (Url) | string. isNullOrWhiteSpace (httpMethod) {throw new ArgumentException ("Url or HttpMethod parameter cannot be blank! ");} GC. collect (); StringBuilder content = new StringBuilder (); HttpWebRequest request = null; HttpWebResponse response = null; StreamWriter sw = null; try {ServicePointManager. defaultConnectionLimit = 512; request = WebRequest. create (Url) as HttpWebRequest; request. method = httpMethod; request. allowAutoRedirect = true; request. keepAlive = false; request. accept = "Mozilla/4.0 (compatible; MSIE 7. 0; Windows NT 5.1 ;. net clr 2.0.50727; Maxthon 2.0) "; request. contentType = setContentType; // "application/octet-stream"; request. proxy = null; if (httpContent! = Null &&! String. isNullOrWhiteSpace (httpContent) {using (sw = new StreamWriter (request. getRequestStream () {sw. write (httpContent) ;}} using (response = request. getResponse () as HttpWebResponse) {if (this. downloadStart! = Null) this. downloadStart (int) response. statusCode); Stream stream = response. getResponseStream (); if (response. contentEncoding. toLower (). contains ("gzip") stream = new GZipStream (stream, CompressionMode. decompress); float percent = 0; long totalDownloadedByte = 0; long totalBytes = response. contentLength; while (stream. canRead) {byte [] buffer = new byte [1024]; int canrds = stream. read (buffer, 0, 1024); totalDownloadedByte = canrds + totalDownloadedByte; percent = (float) totalDownloadedByte/(float) totalBytes * 100; if (DownloadProcess! = Null) DownloadProcess (totalBytes, totalDownloadedByte, percent); if (canrds = 0) break; content. Append (httpCode. GetString (buffer);} if (DownloadEnd! = Null) DownloadEnd (totalBytes); stream. Dispose () ;}} finally {if (request! = Null) request. Abort (); if (response! = Null) response. Dispose (); if (sw! = Null) sw. dispose ();} GC. suppressFinalize (this); return content. toString ();/* Response. write (Guid. newGuid (). toString () + "