HttpRequest rewrite to resolve resource WINS/link timeout/block download event notification issues.

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.IO;usingSystem.IO.Compression;usingSystem.Linq;usingSystem.Net;usingSystem.Text;usingsystem.web;namespacemyapi{ Public classHttpRequest { PublicHttpRequest () {//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; }         PublicHttpRequest (stringUrlstringMETHOD):Base() {SetUrl=URL; Setmethod=method; }        /// <summary>        ///Service Request Addresshttp://www.aaa.com/        /// </summary>         Public stringSetUrl {Get;Set; } /// <summary>        ///Service Request method: Get/post/put/delete/// </summary>         Public stringSetmethod {Get;Set; } /// <summary>        ///Set the service request data type/// </summary>[DefaultValue ("text/html")]         Public stringsetContentType {Get;Set; } /// <summary>        ///Set the service page encoding/// </summary>         PublicEncoding setencoding {Get;Set; }  Public Delegate voidDownloadstartdelegate (intHttpStatusCode);  Public Delegate voidDownloadprocessdelegate (LongTotallength,LongDownloadedbyte,floatpercent);  Public Delegate voidDownloadenddelegate (Longtotallength);  Public Eventdownloadstartdelegate Downloadstart;  Public Eventdownloadprocessdelegate downloadprocess;  Public Eventdownloadenddelegate Downloadend;  Public stringGet () {returnGet (SetUrl); }         Public stringGet (stringSetUrl) {            returnGet (SetUrl,"GET",NULL, setencoding); }         Public stringGet (stringSetUrl,stringhttpcontent) {            returnGet (SetUrl,"POSE", Httpcontent, setencoding); }         Public stringGet (stringUrlstringHttpMethod,stringhttpcontent, Encoding httpcode) {            if(string. Isnullorwhitespace (URL) | |string. Isnullorwhitespace (HttpMethod)) {Throw NewArgumentException ("the URL or HttpMethod parameter cannot be empty! "); } GC.            Collect (); StringBuilder content=NewStringBuilder (); HttpWebRequest Request=NULL; HttpWebResponse Response=NULL; StreamWriter SW=NULL; Try{servicepointmanager.defaultconnectionlimit= +; Request= WebRequest.Create (URL) asHttpWebRequest; 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 =NewStreamWriter (Request. GetRequestStream ())) {SW.                    Write (httpcontent); }                }                using(response = Request.) GetResponse () asHttpWebResponse) {                    if( This. Downloadstart! =NULL)                         This. Downloadstart ((int) Response.                    StatusCode); Stream Stream=Response.                    GetResponseStream (); if(Response. Contentencoding.tolower (). Contains ("gzip")) Stream=NewGZipStream (stream, compressionmode.decompress); floatPercent =0; LongTotaldownloadedbyte =0; LongTotalBytes =Response.                    ContentLength;  while(stream. CanRead) {byte[] buffer =New byte[1024x768]; intCanrds = stream. Read (Buffer,0,1024x768); Totaldownloadedbyte= Canrds +Totaldownloadedbyte; Percent= (float) Totaldownloadedbyte/(float) TotalBytes * -; 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); returncontent.            ToString (); /*Response.Write (Guid.NewGuid ().            ToString () + "*/        }    }}

HttpRequest rewrite to resolve resource WINS/link timeout/block download event notification issues.

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.