This documentation is archived and is not being maintained.

WebRequest.CachePolicy Property

Gets or sets the cache policy for this request.

Namespace:  System.Net
Assembly:  System (in System.dll)

public function get CachePolicy () : RequestCachePolicy 
public function set CachePolicy (value : RequestCachePolicy)

Property Value

Type: System.Net.Cache.RequestCachePolicy
A RequestCachePolicy object that defines a cache policy.

The current cache policy and the presence of the requested resource in the cache determine whether a response can be retrieved from the cache. Using cached responses usually improves application performance, but there is a risk that the response in the cache does not match the response on the server.

Default cache policy can be specified in the Machine.config configuration file or by setting the DefaultCachePolicy property for requests that use the Hypertext Transfer Protocol (HTTP) or Secure Hypertext Transfer Protocol (HTTPS) URI scheme.

A copy of a resource is only added to the cache if the response stream for the resource is retrieved and read to the end of the stream. So another request for the same resource could use a cached copy, depending on the cache policy level for this request.

The following code example demonstrates setting the cache policy for a Web request.

No code example is currently available or this language may not be supported.
    // The following method demonstrates overriding the
    // caching policy for a request.
    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(S"IsFromCache? {0}", __box(response->IsFromCache));  
        return response;

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0