由于不能直接访问指定数据库,只能通过跳板机查询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) 压缩请求数据的更多相关文章

  1. 对ashx请求用Gzip,Deflated压缩

    //GZIP压缩 //查看请求头部 string acceptEncoding = context.Request.Headers["Accept-Encoding"].ToStr ...

  2. Asp.net WebAPi gzip压缩和json格式化

    现在webapi越来越流行了,很多时候它都用来做接口返回json格式的数据,webapi原本是根据客户端的类型动态序列化为json和xml的,但实际很多时候我们都是序列化为json的,所以webapi ...

  3. Web服务器处理HTTP压缩之gzip、deflate压缩

    现如今在处理http请求的时候,由于请求的资源较多,如果不启用压缩的话,那么页面请求的流量将会非常大.启用gzip压缩,在一定程度上会大大的提高页面性能.   目录 一.什么是gzip 二.什么是de ...

  4. 从python爬虫引发出的gzip,deflate,sdch,br压缩算法分析

    今天在使用python爬虫时遇到一个奇怪的问题,使用的是自带的urllib库,在解析网页时获取到的为b'\x1f\x8b\x08\x00\x00\x00\x00...等十六进制数字,尝试使用chard ...

  5. 61、请求数据进行gizp压缩

    1.请求时进行头部处理 /** * 设置通用消息头 * * @param request */ public void setHeader(HttpUriRequest request) { // r ...

  6. php curl采集,服务器gzip压缩返回数据怎么办

    一般服务器不会胡乱返回gzip压缩的数据,一般是客户端请求的头部里包含你浏览器能接受的压缩方式, Accept-Encoding:gzip,deflate,sdch   这里是gzip .deflat ...

  7. 笔记:服务器压缩方案 来源于 Accept-Encoding: gzip, deflate 问题

    笔记:服务器压缩方案 来源于 Accept-Encoding: gzip, deflate 问题 事情起因:odoo demo 没有启动web 压缩 目前流行的 web 压缩技术 gzip br 支持 ...

  8. Apache 使用gzip、deflate 压缩页面加快网站访问速度

    Apache 使用gzip 压缩页面加快网站访问速度 介绍: 网页压缩来进一步提升网页的浏览速度,它完全不需要任何的成本,只不过是会让您的服务器CPU占用率稍微提升一两个百分点而已或者更少.   原理 ...

  9. ASP.NET Web API中使用GZIP 或 Deflate压缩

    对于减少响应包的大小和响应速度,压缩是一种简单而有效的方式. 那么如何实现对ASP.NET Web API 进行压缩呢,我将使用非常流行的库用于压缩/解压缩称为DotNetZip库.这个库可以使用Nu ...

随机推荐

  1. mapreduce 读写lzo文件

    1.读lzo文件 需要添加以下代码,并导入lzo相关的jar包  job.setInputFormatClass(LzoTextInputFormat.class); 2.写lzo文件 lzo格式默认 ...

  2. 【Android - 框架】之GreenDao的使用

    上一篇博客([Android - 框架]之ORMLite的使用)中介绍了ORMLite的基本使用,今天我们来研究以下GreenDao的使用. GreenDao和ORMLite一样,都是基于ORM(Ob ...

  3. (转)LINUX的端口和服务

    一.端口和服务的关系 端口号与相应服务的对应关系存放在/etc/services文件中,这个文件中可以找到大部分端口.使用netstat命令显示的服务名称也是从这个文件中找的.有人说将这个文件中的相应 ...

  4. 学习了几天的jQuery Mobile的一点感受

    一.Write less  Do more! 这好像就是jQuery Mobile的口号吧.基于jQuery类库的jQuery Mobile移动web插件,有了这个轻量级插件后,排版方便了许多,很多样 ...

  5. libcurl使用演示样例

    简要说明:C++使用libcurl訪问"www.baidu.com".获取返回码和打印出http文件 /* * @ libcurl使用演示样例 * @ 2014.04.29 * @ ...

  6. [AngularJS + Webpack] Production Setup

    Using Angular with webpack makes the production build a breeze. Simply alter your webpack configurat ...

  7. android 51 有序广播

    无序广播:一条广播发送出去,多个接收者接收没有顺序.有序广播:广播接收者可以设置优先级,优先级高的先收到广播.有序广播可以设置优先级. mainActivity.java package com.sx ...

  8. Java语言基础(五)

    Java语言基础(五) 一.浮点数 浮点数就是小数,其标准是IEEE 754,用指数和尾数表示 例如30000=3*10000=3*10^4  其中4是指数,3是尾数 Java中,浮点数有float ...

  9. Linux下使用w命令和uptime命令查看系统负载

    在Linux系统中查询系统CPU和内存的负载(使用率)时,我们通常习惯于使用top.atop或者ps,这篇文章将要给大家介绍如何使用w命令和uptime命令来查看系统的负载情况,对于uptime命令, ...

  10. block没那么难(二):block和变量的内存管理

    本系列博文总结自<Pro Multithreading and Memory Management for iOS and OS X with ARC> 了解了 block的实现,我们接着 ...