Export (0) Print
Expand All

WebRequest.CachePolicy Property

Gets or sets the cache policy for this request.

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

public:
virtual property RequestCachePolicy^ CachePolicy {
	RequestCachePolicy^ get ();
	void set (RequestCachePolicy^ value);
}
/** @property */
public RequestCachePolicy get_CachePolicy ()

/** @property */
public void set_CachePolicy (RequestCachePolicy value)

public function get CachePolicy () : RequestCachePolicy

public function set CachePolicy (value : RequestCachePolicy)

Not applicable.

Property Value

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.

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

// 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;
}

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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft