如何在HttpWebRequest中混合使用压缩和缓存? [英] How to mix compression and caching in HttpWebRequest?

查看:109
本文介绍了如何在HttpWebRequest中混合使用压缩和缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#客户端正在与cherrypy(http/rest)网络服务通信. 问题是我不能同时打开压缩和缓存.

I have a c# client talking to a cherrypy(http/rest) webservice. The problem is i can't both turn on compression and caching at the same time.

request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

通过省略上面的行,我得到了正确的缓存头(If-None-Math,If-Modified-Since),而注释掉了却得到了压缩头(Accept-Encodig:gzip),但没有缓存头.在我看来,这似乎是个错误,但也许我做错了.

By leaving out the above line I get the correct caching headers (If-None-Math,If-Modified-Since) while commenting it out gets me the compression headers (Accept-Encodig:gzip) but not the caching headers. It seems to me like a bug but maybe i'm doing something wrong.

[完整代码]

        public static string GET(string URL)
    {
        string JSON;
        // Create the web request  
        HttpWebRequest request = WebRequest.Create(URL) as HttpWebRequest;
        HttpRequestCachePolicy cPolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Revalidate);

        request.Accept = "application/json";            
        request.CachePolicy = cPolicy;
        request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
        request.Pipelined = false;

        // Get response  
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {                
            // Get the response stream  
            StreamReader readerF = new StreamReader(response.GetResponseStream());

            JSON = readerF.ReadToEnd();
            // Console application output  
            //Console.WriteLine(JSON);
            if (response.IsFromCache )
                Console.WriteLine("Request not from cache");
        }

        return JSON;
    }

推荐答案

我已经实现了一种解决方法,请参见下面的代码.我认为处理压缩比处理缓存更容易,因此我自己实现了压缩部分.非常感谢博客文章: HttpWebRequest和GZip Http响应;我仍然认为这是.net中的错误.

I have implemented a workaround, see code below. I judged handling the compression easier than handling the cacheing so I implemented the compression part myself. Quite easy thanks to a blog post: HttpWebRequest and GZip Http Responses; I still think this is a bug in .net.

        public static string GET(string URL)
    {
        string JSON;
        // Create the web request  
        HttpWebRequest request = WebRequest.Create(URL) as HttpWebRequest;
        HttpRequestCachePolicy cPolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Revalidate);

        request.Accept = "application/json";
        request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
        request.CachePolicy = cPolicy;
        request.Pipelined = false;

        // Get response  
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            //From http://www.west-wind.com/WebLog/posts/102969.aspx
            Stream responseStream = responseStream = response.GetResponseStream();
            if (response.ContentEncoding.ToLower().Contains("gzip"))
                responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
            else if (response.ContentEncoding.ToLower().Contains("deflate"))
                responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);


            // Get the response stream  
            StreamReader readerF = new StreamReader(responseStream);
            JSON = readerF.ReadToEnd();
        }

        return JSON;
    }

这篇关于如何在HttpWebRequest中混合使用压缩和缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆