代码之家  ›  专栏  ›  技术社区  ›  AngelBlaZe

如何在httpwebrequest中混合压缩和缓存?

  •  2
  • AngelBlaZe  · 技术社区  · 15 年前

    我有一个C客户机正在与一个Cherrypy(HTTP/REST)Web服务交谈。 问题是我不能同时打开压缩和缓存。

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

    通过去掉上面的行,我得到了正确的缓存头(如果没有数学,如果修改自),同时注释它得到了压缩头(accept encodig:gzip),但没有缓存头。在我看来,这就像一只虫子,但也许我做错了什么。

    [完整代码]

            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;
        }
    
    2 回复  |  直到 15 年前
        1
  •  2
  •   AngelBlaZe    15 年前

    我已经实现了一个变通方法,请参见下面的代码。我认为处理压缩比处理缓存更容易,所以我自己实现了压缩部分。非常简单,这要归功于博客: HttpWebRequest and GZip Http Responses 我仍然认为这是.NET中的一个bug。

            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;
        }
    
        2
  •  0
  •   david valentine    15 年前

    这是政策的副作用吗? 如果只使用默认策略或其他策略,会发生什么情况?

    另一种选择是自己管理缓存。