我想解碼發送到web服務與Content-Type:application/x-gzip的GZipped請求。webservice無法獲得gzip請求數據
我已經實現了一個HttpHandler,它添加了一個GZip解壓縮過濾器,它似乎正在工作。下面的代碼:
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
HttpContext ctx = app.Context;
if (!ctx.Request.Url.PathAndQuery.ToLower().Contains(".asmx"))
return;
// test
if ("gzip" == ctx.Request.Headers["Content-encoding"])
{
app.Request.Filter = new GZipStream(app.Request.Filter,
CompressionMode.Decompress);
}
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.Current.Response.AppendHeader("Content-encoding", encoding);
}
不幸的是,我不斷收到此錯誤所有的時間:
System.ServiceModel.ProtocolException: Content Type application/x-gzip was not supported by service https://127.0.0.1/ModuloWS.asmx. The client and service bindings may be mismatched. ---> System.Net.WebException: The remote server returned an error: (415) Unsupported Media Type.
在System.Net.HttpWebRequest.GetResponse() 在System.ServiceModel.Channels.HttpChannelFactory`1 .HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan超時) ---內部異常堆棧跟蹤結束---
我一直在這一整天都在破壞我的堅果。
P.S.該請求是使用從MSDN網站上的WCF示例項目中取出的GZipMessageEncoder進行GZip編碼的。
我認爲的問題是該服務是一個常規的.NET Web服務,但客戶端是一個WCF客戶端。我需要找到一種方法來在服務器端(常規的WebService)進行配置...想一想嗎? –