WebApi Gzip(Deflate) 压缩请求数据
由于不能直接访问指定数据库,只能通过跳板机查询Oracle数据,所以要做一个数据中转接口,
查询数据就要压缩,于是就找资料,代码如下,其中要注意的是Response.Headers.Remove("Content-Encoding");
这段,对Response.Headrs的操作如果IIS6是不支持的,
会出现如下错误:
接口出现错误:"此操作要求使用 IIS 集成管线模式。" ()
System.PlatformNotSupportedException: 此操作要求使用 IIS 集成管线模式
其实在OnActionExecuting 这个方法中,请求中Content-Encoding本来就是空的,可以不必操作,
在项目中添加下面的类;
然后在Contoller方法上加上对应数据,即可实现对数据的压缩。
// <summary>
/// 自动识别客户端是否支持压缩,如果支持则返回压缩后的数据
/// Attribute that can be added to controller methods to force content
/// to be GZip encoded if the client supports it
/// </summary>
public class CompressContentAttribute : ActionFilterAttribute
{
/// <summary>
/// Override to compress the content that is generated by
/// an action method.
/// </summary>
/// <param name="filterContext"></param>
public override void OnActionExecuting(HttpActionContext filterContext)
{
GZipEncodePage();
} /// <summary>
/// Determines if GZip is supported
/// </summary>
/// <returns></returns>
public static bool IsGZipSupported()
{
string AcceptEncoding = 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 to call this method before any output is generated!
/// </summary>
public static void GZipEncodePage()
{
HttpResponse Response = HttpContext.Current.Response; if (IsGZipSupported())
{
string AcceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"]; if (AcceptEncoding.Contains("deflate"))
{
Response.Filter = new System.IO.Compression.DeflateStream(Response.Filter,
System.IO.Compression.CompressionMode.Compress);
#region II6不支持此方法,(实际上此值默认为null 也不需要移除)
//Response.Headers.Remove("Content-Encoding");
#endregion
Response.AppendHeader("Content-Encoding", "deflate");
}
else
{
Response.Filter = new System.IO.Compression.GZipStream(Response.Filter,
System.IO.Compression.CompressionMode.Compress);
#region II6不支持此方法,(实际上此值默认为null 也不需要移除)
//Response.Headers.Remove("Content-Encoding");
#endregion
Response.AppendHeader("Content-Encoding", "gzip");
}
} // Allow proxy servers to cache encoded and unencoded versions separately
Response.AppendHeader("Vary", "Content-Encoding");
}
} /// <summary>
/// 强制Defalte压缩
/// Content-encoding:gzip,Content-Type:application/json
/// DEFLATE是一个无专利的压缩算法,它可以实现无损数据压缩,有众多开源的实现算法。
/// </summary>
public class DeflateCompressionAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext filterContext)
{
HttpResponse Response = HttpContext.Current.Response;
Response.Filter = new System.IO.Compression.DeflateStream(Response.Filter,
System.IO.Compression.CompressionMode.Compress);
#region II6不支持此方法,(实际上此值默认为null 也不需要移除)
//Response.Headers.Remove("Content-Encoding");
#endregion
Response.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>
/// 强制GZip压缩,application/json
/// Content-encoding:gzip,Content-Type:application/json
/// GZIP是使用DEFLATE进行压缩数据的另一个压缩库
/// </summary>
public class GZipCompressionAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext filterContext)
{
HttpResponse Response = HttpContext.Current.Response;
Response.Filter = new System.IO.Compression.GZipStream(Response.Filter,
System.IO.Compression.CompressionMode.Compress);
#region II6不支持此方法,(实际上此值默认为null 也不需要移除)
//Response.Headers.Remove("Content-Encoding");
#endregion
Response.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);
//}
}
使用方法
[DeflateCompression]
public string GetDeflate()
{
return "valuevaluevaluevaluevaluevaluevaluevaluevaluevalueSDFASDFASDFASDFASDFWERFWEFCASDFSDAFASDFASDFASDFSADFSA";
} [CompressContent]
public string GetGZip()
{
return "valuevaluevaluevaluevaluevaluevaluevaluevaluevalueSDFASDFASDFASDFASDFWERFWEFCASDFSDAFASDFASDFASDFSADFSA";
} [GZipCompression]
public string GetRaw()
{
return "valuevaluevaluevaluevaluevaluevaluevaluevaluevalueSDFASDFASDFASDFASDFWERFWEFCASDFSDAFASDFASDFASDFSADFSA";
}
WebApi Gzip(Deflate) 压缩请求数据的更多相关文章
- 对ashx请求用Gzip,Deflated压缩
//GZIP压缩 //查看请求头部 string acceptEncoding = context.Request.Headers["Accept-Encoding"].ToStr ...
- Asp.net WebAPi gzip压缩和json格式化
现在webapi越来越流行了,很多时候它都用来做接口返回json格式的数据,webapi原本是根据客户端的类型动态序列化为json和xml的,但实际很多时候我们都是序列化为json的,所以webapi ...
- Web服务器处理HTTP压缩之gzip、deflate压缩
现如今在处理http请求的时候,由于请求的资源较多,如果不启用压缩的话,那么页面请求的流量将会非常大.启用gzip压缩,在一定程度上会大大的提高页面性能. 目录 一.什么是gzip 二.什么是de ...
- 从python爬虫引发出的gzip,deflate,sdch,br压缩算法分析
今天在使用python爬虫时遇到一个奇怪的问题,使用的是自带的urllib库,在解析网页时获取到的为b'\x1f\x8b\x08\x00\x00\x00\x00...等十六进制数字,尝试使用chard ...
- 61、请求数据进行gizp压缩
1.请求时进行头部处理 /** * 设置通用消息头 * * @param request */ public void setHeader(HttpUriRequest request) { // r ...
- php curl采集,服务器gzip压缩返回数据怎么办
一般服务器不会胡乱返回gzip压缩的数据,一般是客户端请求的头部里包含你浏览器能接受的压缩方式, Accept-Encoding:gzip,deflate,sdch 这里是gzip .deflat ...
- 笔记:服务器压缩方案 来源于 Accept-Encoding: gzip, deflate 问题
笔记:服务器压缩方案 来源于 Accept-Encoding: gzip, deflate 问题 事情起因:odoo demo 没有启动web 压缩 目前流行的 web 压缩技术 gzip br 支持 ...
- Apache 使用gzip、deflate 压缩页面加快网站访问速度
Apache 使用gzip 压缩页面加快网站访问速度 介绍: 网页压缩来进一步提升网页的浏览速度,它完全不需要任何的成本,只不过是会让您的服务器CPU占用率稍微提升一两个百分点而已或者更少. 原理 ...
- ASP.NET Web API中使用GZIP 或 Deflate压缩
对于减少响应包的大小和响应速度,压缩是一种简单而有效的方式. 那么如何实现对ASP.NET Web API 进行压缩呢,我将使用非常流行的库用于压缩/解压缩称为DotNetZip库.这个库可以使用Nu ...
随机推荐
- 深入浅出node(4) 异步编程
一)函数式编程基础 二)异步编程的优势和难点 2.1 优势 2.2 难点 2.2.1 异常处理 2.2.2 函数嵌套过深 2.2.3 阻塞 2.2.4 多线程编程 2.2.5 异步转同步 三)异步编程 ...
- uva1620 Lazy Susan
留坑(p.256) 什么找规律啊 坑爹 #include<cstdio> #include<cstring> #include<cstdlib> #include& ...
- 如何正确并完全安装Visual Studio 2015企业版本[转]
http://blog.csdn.net/code_godfather/article/details/47381631 [注意事项]1> 本文描述的是: Visual Studio 2015企 ...
- EXCEL 如何将多个工作表或工作簿合并到一个工作表
在使用Excel 时,我们经常需要将多个工作表或工作簿合并到一个工作表中,这样我们就能快速地对数据进行分析和统计.对于一般用户而言,除了复制每个工作表后再粘贴,没有其他什么方法了.如果只是合并少数几个 ...
- Doctor NiGONiGO’s multi-core CPU(最小费用最大流模板)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=693 题意:有一个 k 核的处理器和 n 个工作,全部的工作都须要在一个核上处理一个单位的 ...
- Delphi中一些常用的组合键值
Delphi中一些常用的组合键值 CTRL+A: #1 CTRL+B: #2 CTRL+C: #3 CTRL+D: #4 CTRL+E: #5 CTRL+F: #6 CTRL+G: #7 ...
- 字符串匹配之KMP---全力解析
近日,一同学面试被问到字符串匹配算法,结果因为他使用了暴力法,直接就跪了(如今想想这种面试官真的是不合格的,陈皓的一篇文章说的非常好,点击阅读).字符串匹配方法大概有:BF(暴力破解法), 简化版的B ...
- [PWA] Keynote: Progressive Web Apps across all frameworks
PWA: Add to home screen Angular Universal Server side rendering: for achieving better proference on ...
- TCP 函数
[root@localhost tt]# man listen LISTEN() Linux Programmer’s Manual LISTEN() NAME listen - listen for ...
- mac 终端常见指令
基本命令 1.列出文件 ls 参数 目录名 例: 看看驱动目录下有什么:ls /System/Library/Extensions参数 -w 显示中文,-l 详细信息, -a 包括隐藏文 ...