WebRequest.DefaultCachePolicy WebRequest.DefaultCachePolicy WebRequest.DefaultCachePolicy WebRequest.DefaultCachePolicy Property

定义

获取或设置此请求的默认缓存策略。Gets or sets the default cache policy for this request.

public:
 static property System::Net::Cache::RequestCachePolicy ^ DefaultCachePolicy { System::Net::Cache::RequestCachePolicy ^ get(); void set(System::Net::Cache::RequestCachePolicy ^ value); };
public static System.Net.Cache.RequestCachePolicy DefaultCachePolicy { get; set; }
member this.DefaultCachePolicy : System.Net.Cache.RequestCachePolicy with get, set
Public Shared Property DefaultCachePolicy As RequestCachePolicy

属性值

一个 HttpRequestCachePolicy,它指定当没有其他策略适用时对此请求有效的缓存策略。A HttpRequestCachePolicy that specifies the cache policy in effect for this request when no other policy is applicable.

示例

下面的代码示例演示如何为 Web 请求设置默认缓存策略。The following code example demonstrates setting the default cache policy for Web requests.

static WebResponse^ GetResponseFromServer2( Uri^ uri )
{
   RequestCachePolicy^ policy = gcnew RequestCachePolicy( RequestCacheLevel::NoCacheNoStore );
   WebRequest^ request = WebRequest::Create( uri );
   WebRequest::DefaultCachePolicy = policy;
   WebResponse^ response = request->GetResponse();
   Console::WriteLine( L"Policy is {0}.", policy );
   Console::WriteLine( L"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("Policy is {0}.", policy.ToString());
    Console.WriteLine("Is the response from the cache? {0}", response.IsFromCache);  
    return response;
} 

注解

如果满足以下条件,则此策略用于此请求:This policy is used for this request if the following conditions exist:

  • 没有DefaultCachePolicy为此请求指定属性。There is no DefaultCachePolicy property specified for this request.

  • 计算机和应用程序配置文件不指定适用于用于创建此请求的统一资源标识符(URI)的缓存策略。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.

安全性

WebPermission
用于对网络资源进行无限制的访问。For unrestricted access to network resources. 关联的枚举:UnrestrictedAssociated enumeration: Unrestricted

适用于

另请参阅