我有 ac# 客户端与cherrypy(http/rest) 网络服务交谈。问题是我不能同时打开压缩和缓存。
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
通过省略上面的行,我得到了正确的缓存头(If-None-Math,If-Modified-Since),而注释掉它得到了压缩头(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;
}