WebApi Gzip (Deflate) Compress request data

Source: Internet
Author: User

Because the specified database cannot be accessed directly, Oracle data can only be queried through a springboard machine, so a data transfer interface is

Query data will be compressed, so look for information, code as follows, which should pay attention to is Response.Headers.Remove ("content-encoding");

This paragraph, the operation of the Response.headrs if IIS6 is not supported,

The following error will appear:

Interface error: "This operation requires the use of IIS integrated pipeline mode. " ()
System.platformnotsupportedexception: This operation requires the use of IIS integrated pipeline mode

In fact, in onactionexecuting this method, the request content-encoding is empty, can not operate,

Add the following class to the project;

Then the data can be compressed by adding corresponding data to the Contoller method.

//<summary>    ///automatically identifies whether the client supports compression and returns the compressed data if supported///Attribute that can is added to controller methods to force content///To be GZIP encoded if the client supports it/// </summary>     Public classCompresscontentattribute:actionfilterattribute {/// <summary>        ///Override to compress the content (generated by)///An action method. /// </summary>        /// <param name= "Filtercontext" ></param>         Public Override voidonactionexecuting (Httpactioncontext filtercontext) {gzipencodepage (); }        /// <summary>        ///determines if GZIP is supported/// </summary>        /// <returns></returns>         Public Static BOOLisgzipsupported () {stringacceptencoding = httpcontext.current.request.headers["accept-encoding"]; if(!string. IsNullOrEmpty (acceptencoding) &&(Acceptencoding.contains ("gzip") || Acceptencoding.contains ("deflate")))                return true; return false; }        /// <summary>        ///Sets up the current page or handler to use GZip through a response.filter///IMPORTANT:///You have the to call this method before any output is generated! /// </summary>         Public Static voidGzipencodepage () {HttpResponse Response=HttpContext.Current.Response; if(isgzipsupported ()) {stringacceptencoding = httpcontext.current.request.headers["accept-encoding"]; if(Acceptencoding.contains ("deflate") ) {Response.filter=NewSystem.IO.Compression.DeflateStream (Response.filter, System.IO.Compress Ion.                    compressionmode.compress); #regionII6 does not support this method, (in fact this value defaults to null and does not need to be removed)//Response.Headers.Remove ("content-encoding");                    #endregionResponse.appendheader ("content-encoding","deflate"); }                Else{Response.filter=NewSystem.IO.Compression.GZipStream (Response.filter, System.IO.Compressi On.                    compressionmode.compress); #regionII6 does not support this method, (in fact this value defaults to null and does not need to be removed)//Response.Headers.Remove ("content-encoding");                    #endregionResponse.appendheader ("content-encoding","gzip"); }            }            //Allow proxy servers to cache encoded and unencoded versions separatelyResponse.appendheader ("Vary","content-encoding"); }    }    /// <summary>    ///Force Defalte Compression///Content-encoding:gzip,content-type:application/json///deflate is a non-patented compression algorithm, it can achieve lossless data compression, there are many open-source implementation algorithms. /// </summary>     Public classDeflatecompressionattribute:actionfilterattribute { Public Override voidonactionexecuting (Httpactioncontext filtercontext) {HttpResponse Response=HttpContext.Current.Response; Response.filter=NewSystem.IO.Compression.DeflateStream (Response.filter, System.IO.Compressi On.            compressionmode.compress); #regionII6 does not support this method, (in fact this value defaults to null and does not need to be removed)//Response.Headers.Remove ("content-encoding");            #endregionResponse.appendheader ("content-encoding","deflate"); }        //Public override void OnActionExecuted (Httpactionexecutedcontext actcontext)//{        //var content = actContext.Response.Content; //var bytes = Content = = null? null:content. Readasbytearrayasync ().        Result; //var zlibbedcontent = bytes = = null? New Byte[0]://APP.        Core.Utility.ZHelper.DeflateByte (bytes); //actContext.Response.Content = new Bytearraycontent (zlibbedcontent); //actContext.Response.Content.Headers.Remove ("Content-type"); //actContext.Response.Content.Headers.Add ("content-encoding", "deflate"); //actContext.Response.Content.Headers.Add ("Content-type", "Application/json"); //base.        OnActionExecuted (Actcontext); //}    }    /// <summary>    ///forced gzip compression, Application/json///Content-encoding:gzip,content-type:application/json///gzip is another compression library that uses deflate to compress data/// </summary>     Public classGzipcompressionattribute:actionfilterattribute { Public Override voidonactionexecuting (Httpactioncontext filtercontext) {HttpResponse Response=HttpContext.Current.Response; Response.filter=NewSystem.IO.Compression.GZipStream (Response.filter, System.IO.Compression.            compressionmode.compress); #regionII6 does not support this method, (in fact this value defaults to null and does not need to be removed)//Response.Headers.Remove ("content-encoding");            #endregionResponse.appendheader ("content-encoding","gzip"); }        //Public override void OnActionExecuted (Httpactionexecutedcontext actcontext)//{        //var content = actContext.Response.Content; //var bytes = Content = = null? null:content. Readasbytearrayasync ().        Result; //var zlibbedcontent = bytes = = null? New Byte[0]://APP.        Core.Utility.ZHelper.GZipByte (bytes); //actContext.Response.Content = new Bytearraycontent (zlibbedcontent); //actContext.Response.Content.Headers.Remove ("Content-type"); //actContext.Response.Content.Headers.Add ("content-encoding", "gzip"); //actContext.Response.Content.Headers.Add ("Content-type", "Application/json"); //base.        OnActionExecuted (Actcontext); //}}

How to use

[Deflatecompression] Public stringgetdeflate () {return "VALUEVALUEVALUEVALUEVALUEVALUEVALUEVALUEVALUEVALUESDFASDFASDFASDFASDFWERFWEFCASDFSDAFASDFASDFASDFSADFSA"; } [Compresscontent] Public stringGetgzip () {return "VALUEVALUEVALUEVALUEVALUEVALUEVALUEVALUEVALUEVALUESDFASDFASDFASDFASDFWERFWEFCASDFSDAFASDFASDFASDFSADFSA"; } [Gzipcompression] Public stringGetraw () {return "VALUEVALUEVALUEVALUEVALUEVALUEVALUEVALUEVALUEVALUESDFASDFASDFASDFASDFWERFWEFCASDFSDAFASDFASDFASDFSADFSA"; }

WebApi Gzip (Deflate) Compress request data

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.