How to clear the cache of HttpWebRequest

前端 未结 5 2182
说谎
说谎 2020-11-29 09:05

I am developing against a proprietary library and I\'m experiencing some issues with the cache of the HttpWebRequest. The library is using code equivalent to th

5条回答
  •  情歌与酒
    2020-11-29 09:34

    public static WebResponse GetResponseNoCache(Uri uri)
    {
            // Set a default policy level for the "http:" and "https" schemes.
            HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Default);
            HttpWebRequest.DefaultCachePolicy = policy;
            // Create the request.
            WebRequest request = WebRequest.Create(uri);
            // Define a cache policy for this request only. 
            HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
            request.CachePolicy = noCachePolicy;
            WebResponse response = request.GetResponse();
            Console.WriteLine("IsFromCache? {0}", response.IsFromCache);            
            return response;
    }
    

    You can set the Cache Policy to the request to NoCacheNoStore to the HttpWebRequest.

提交回复
热议问题