• web api 压缩


     1 using System.Web.Http.Filters;
     2 using System;
     3 using System.Net.Http.Headers;
     4 using System.Text;
     5 using System.Collections.Generic;
     6 using System.Web.Http;
     7 using System.Web.Http.Controllers;
     8 using System.Linq.Expressions;
     9 using System.Linq;
    10 using System.Reflection;
    11 using System.Collections;
    12 using System.Threading;
    13 using System.Net.Http.Formatting;
    14 using System.Net;
    15 using System.Threading.Tasks;
    16 using System.Runtime.Caching;
    17 using System.IO;
    18 using Ionic.Zip;
    19 using System.Net.Http;
    20 
    21 
    22 namespace Compress
    23 {
    24     public class DeflateCompressionAttribute : System.Web.Http.Filters.ActionFilterAttribute
    25     {
    26         public override void OnActionExecuted(HttpActionExecutedContext actContext)
    27         {
    28             var content = actContext.Response.Content;
    29             var bytes = content == null ? null : content.ReadAsByteArrayAsync().Result;
    30             var zlibbedContent = bytes == null ? new byte[0] :
    31             CompressionHelper.DeflateByte(bytes);
    32             actContext.Response.Content = new ByteArrayContent(zlibbedContent);
    33             actContext.Response.Content.Headers.Remove("Content-Type");
    34             actContext.Response.Content.Headers.Add("Content-encoding", "gzip");
    35             actContext.Response.Content.Headers.Add("Content-Type", "application/json");
    36             base.OnActionExecuted(actContext);
    37         }
    38     }
    39 
    40     public class CompressionHelper
    41     {
    42         public static byte[] DeflateByte(byte[] str)
    43         {
    44             if (str == null)
    45             {
    46                 return null;
    47             }
    48 
    49             using (var output = new MemoryStream())
    50             {
    51                 using (
    52                     var compressor = new Ionic.Zlib.GZipStream(
    53                     output, Ionic.Zlib.CompressionMode.Compress,
    54                     Ionic.Zlib.CompressionLevel.BestSpeed))
    55                 {
    56                     compressor.Write(str, 0, str.Length);
    57                 }
    58 
    59                 return output.ToArray();
    60             }
    61         }
    62     }
    63 
    64     // [DeflateCompressionAttribute]
    65         //public ActionResult Index()
    66         //{
    67         //    return View();
    68         //}
    69 
    70 
    71 }
  • 相关阅读:
    setlocale set the current locale
    测试一个目录下的文件共有多少行
    ping中用到的校验和算法
    atomic integer operations P176
    我要理解1为什么是0xffffffff,所以写了下面的程序理解。
    贝叶斯网络中一个节点的类
    bash 的浮点除法
    shell(1)
    AndroidBroadcast详解与汇总
    AndroidActivity详解与汇总
  • 原文地址:https://www.cnblogs.com/hbsfgl/p/5076679.html
Copyright © 2020-2023  润新知