如何为上传到.NET WebService(SOAP,而不是WCF)的POSTed数据启用压缩?我认为在IIS中enabling dynamic compression只是一个简单的问题,但启用后,它只压缩响应,而不是POST 请求。
我已将其添加为服务参考,但我无法在生成的SOAPClient上找到任何设置来启用压缩请求。
在将请求发送到服务器之前,似乎我可能缺少客户端的配置设置或代码来压缩请求?或者我正在尝试做什么(GZipping POST数据)甚至不支持?
更多信息:我在客户端和服务器上使用.NET 4.0。
答案 0 :(得分:13)
我4年前发表过博客
http://netpl.blogspot.com/2009/07/aspnet-webservices-two-way-response-and.html
我想知道为什么你没有通过谷歌搜索找到这个。无论如何,这应该对你有用,我们现在在生产环境中工作了4年。
答案 1 :(得分:6)
最后,我使用了Wiktor Zychla的回答,但遇到了一些错误(在他的文章的评论中也提到过)。为了完整起见,我将在此处发布我的固定版本的HttpCompression模块,但您需要的其余代码(和实施指南)位于Wiktor's article。
更新:
我实际上不得不停止使用此代码,因为它有一个我无法修复的错误(即使我的改进版本如下)。对于代理服务器背后的许多人,它会出现一个错误,上面写着“gzip标头中的幻数不正确”,我无法弄清楚如何解决这个问题。我认为这是因为代理服务器解压缩GZIP,并且此代码不允许以当前形式接收压缩和非压缩响应。
using System;
using System.IO.Compression;
using System.Web;
using System.Web.Security;
/// <summary>
/// Implement two way HTTP compression for the SOAP API
/// Code taken from: http://netpl.blogspot.co.uk/2009/07/aspnet-webservices-two-way- response-and.html
/// Fix: Set Content-encoding: gzip header when an Exception occurs on the server.
/// Fix: Do not attempt to decrypt GZIP request stream when request was not GZIPed.
/// </summary>
public class HttpCompressionModule : IHttpModule
{
private bool isDisposed = false;
~HttpCompressionModule()
{
Dispose(false);
}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(Context_BeginRequest);
context.PreSendRequestHeaders += new EventHandler(context_PreSendRequestHeaders);
}
void context_PreSendRequestHeaders(object sender, EventArgs e)
{
// Fix headers having been lost if an exception occurred.
HttpApplication app = sender as HttpApplication;
HttpContext ctx = app.Context;
if (app.Response.Filter is GZipStream) SetEncoding("gzip");
else if (app.Response.Filter is DeflateStream) SetEncoding("deflate");
// Fix double header
if (ctx.Response.Headers["Content-encoding"] == "gzip,gzip")
ctx.Response.Headers.Set("Content-encoding", "gzip");
}
public void Context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
HttpContext ctx = app.Context;
// Currently only enable for the Uploader API webservice
if (!ctx.Request.Url.PathAndQuery.ToLower().Contains("uploaderapi.asmx"))
{
return;
}
// Add request filter if request was GZIP encoded
string requestEncoding = ctx.Request.Headers["Content-encoding"];
if (requestEncoding != null && requestEncoding == "gzip")
{
app.Request.Filter =
new System.IO.Compression.GZipStream(app.Request.Filter, CompressionMode.Decompress);
}
// Add response compression filter if the client accepts compressed responses
if (IsEncodingAccepted("gzip"))
{
app.Response.Filter = new GZipStream(app.Response.Filter, CompressionMode.Compress);
SetEncoding("gzip");
}
else if (IsEncodingAccepted("deflate"))
{
app.Response.Filter = new DeflateStream(app.Response.Filter, CompressionMode.Compress);
SetEncoding("deflate");
}
}
private bool IsEncodingAccepted(string encoding)
{
return HttpContext.Current.Request.Headers["Accept-encoding"] != null &&
HttpContext.Current.Request.Headers["Accept-encoding"].Contains(encoding);
}
private void SetEncoding(string encoding)
{
HttpContext ctx = HttpContext.Current;
string responseEncodings = ctx.Response.Headers.Get("Content-encoding");
if (responseEncodings == null || !responseEncodings.Contains(encoding))
HttpContext.Current.Response.AppendHeader("Content-encoding", encoding);
}
public void Dispose()
{
Dispose(true);
}
private void Dispose(bool dispose)
{
isDisposed = dispose;
}
}