WebRequest.CachePolicy 属性   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置此请求的缓存策略。
public:
 virtual property System::Net::Cache::RequestCachePolicy ^ CachePolicy { System::Net::Cache::RequestCachePolicy ^ get(); void set(System::Net::Cache::RequestCachePolicy ^ value); };public virtual System.Net.Cache.RequestCachePolicy? CachePolicy { get; set; }public virtual System.Net.Cache.RequestCachePolicy CachePolicy { get; set; }member this.CachePolicy : System.Net.Cache.RequestCachePolicy with get, setPublic Overridable Property CachePolicy As RequestCachePolicy属性值
定义缓存策略的 RequestCachePolicy 对象。
示例
下面的代码示例演示如何设置 Web 请求的缓存策略。
// The following method demonstrates overriding the
// caching policy for a request.
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;
}
注解
谨慎
              WebRequest、HttpWebRequest、ServicePoint和 WebClient 已过时,不应将其用于新开发。 请改用 HttpClient。
当前缓存策略和缓存中请求的资源的存在决定了是否可以从缓存中检索响应。 使用缓存的响应通常会提高应用程序性能,但缓存中的响应与服务器上的响应不匹配的风险。
可以在 Machine.config 配置文件中指定默认缓存策略,也可以为使用超文本传输协议(HTTP)或安全超文本传输协议(HTTPS)URI 方案的请求设置 DefaultCachePolicy 属性。
仅当检索资源响应流并将其读取到流的末尾时,资源的副本才会添加到缓存中。 因此,针对同一资源的另一个请求可以使用缓存副本,具体取决于此请求的缓存策略级别。