WebRequest.DefaultCachePolicy Property

Gets or sets the default cache policy for this request.

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

public static RequestCachePolicy DefaultCachePolicy { get; set; }

Property Value

Type: System.Net.Cache.RequestCachePolicy
A HttpRequestCachePolicy that specifies the cache policy in effect for this request when no other policy is applicable.

This policy is used for this request if the following conditions exist:

  • There is no DefaultCachePolicy property specified for this request.

  • The machine and application configuration files do not specify a cache policy that is applicable to the Uniform Resource Identifier (URI) used to create this request.

The cache policy determines whether the requested resource can be taken from a cache instead of sending the request to the resource host computer.

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 default cache policy for Web requests.

        public static WebResponse GetResponseFromServer2(Uri uri)
{
     RequestCachePolicy policy = 
        new  RequestCachePolicy( RequestCacheLevel.NoCacheNoStore);
    WebRequest request = WebRequest.Create(uri);
    WebRequest.DefaultCachePolicy = policy;
    WebResponse response = request.GetResponse();
    Console.WriteLine("Policy is {0}.", policy.ToString());
    Console.WriteLine("Is the response from the cache? {0}", response.IsFromCache);  
    return response;
} 
public:
            static WebResponse* GetResponseFromServer2(Uri* uri)
    {
            RequestCachePolicy* policy = 
            new  RequestCachePolicy( RequestCacheLevel::NoCacheNoStore);
        WebRequest* request = WebRequest::Create(uri);
        WebRequest::DefaultCachePolicy = policy;
        WebResponse* response = request->GetResponse();
        Console::WriteLine(S"Policy is {0}.", policy);
        Console::WriteLine(S"Is the response from the cache? {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
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft