Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

HttpRequestCachePolicy Class

Defines an application's caching requirements for resources obtained by using HttpWebRequest objects.

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

public class HttpRequestCachePolicy : RequestCachePolicy

You can specify a default cache policy for your application by using the DefaultCachePolicy property or the application or machine configuration files. For more information, see <requestCaching> Element (Network Settings).

You can specify the cache policy for an individual request by using the CachePolicy property.

Caching for Web services is not supported.

The following code example creates a default cache policy for the application domain, and overrides it for a 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;
}
    // 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(S"IsFromCache? {0}", __box(response->IsFromCache));  
        return response;
    }

System.Object
  System.Net.Cache.RequestCachePolicy
    System.Net.Cache.HttpRequestCachePolicy

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

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

Community Additions

ADD
Show:
© 2015 Microsoft