![](/img/trans.png)
[英]How do I disable gzip compression for flv files in IIS 7 with runAllManagedModulesForAllRequests set to true?
[英]How do I enable GZIP compression for POST (upload) requests to a SOAP WebService on IIS 7?
如何為上傳到.NET WebService(SOAP,而不是WCF)的POSTed數據啟用壓縮? 我認為在IIS中啟用動態壓縮是一件簡單的事情,但在啟用后,它只會壓縮響應 ,而不是壓縮POST 請求 。
我已將其添加為服務引用,但我無法在生成的SOAPClient上找到任何設置來啟用請求壓縮。
似乎我可能在客戶端缺少配置設置或代碼來壓縮請求,然后再將其發送到服務器? 或者我正在嘗試做什么(GZipping POST數據)甚至不支持?
更多信息:我在客戶端和服務器上使用.NET 4.0。
我4年前寫過這篇博文
http://netpl.blogspot.com/2009/07/aspnet-webservices-two-way-response-and.html
我想知道為什么你沒有通過谷歌搜索找到這個。 無論如何,這應該對你有用,我們現在在生產環境中工作了4年。
最后,我使用了Wiktor Zychla的回答,但遇到了一些錯誤(在他的文章的評論中也提到過)。 為了完整起見,我將在此處發布我的固定版本的HttpCompression模塊,但您需要的其他代碼(和實現指南)在Wiktor的文章中 。
更新:
我實際上不得不停止使用此代碼,因為它有一個我無法修復的錯誤(即使我的改進版本如下)。 對於代理服務器背后的許多人,它會出現一個錯誤,上面寫着“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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.