RequestCachePolicy 构造函数

定义

初始化 RequestCachePolicy 类的新实例。

重载

RequestCachePolicy()

初始化 RequestCachePolicy 类的新实例。

RequestCachePolicy(RequestCacheLevel)

初始化 RequestCachePolicy 类的新实例。 使用指定的缓存策略。

RequestCachePolicy()

Source:
RequestCachePolicy.cs
Source:
RequestCachePolicy.cs
Source:
RequestCachePolicy.cs

初始化 RequestCachePolicy 类的新实例。

public:
 RequestCachePolicy();
public RequestCachePolicy ();
Public Sub New ()

示例

下面的示例演示如何调用此构造函数。

static WebResponse^ GetResponseUsingCacheDefault( Uri^ uri )
{
   // Set  the default cache policy level for the "http:" scheme.
   RequestCachePolicy^ policy = gcnew RequestCachePolicy;

   // Create the request.
   WebRequest^ request = WebRequest::Create( uri );
   request->CachePolicy = policy;
   WebResponse^ response = request->GetResponse();
   Console::WriteLine( L"Policy level is {0}.", policy->Level );
   Console::WriteLine( L"Is the response from the cache? {0}", response->IsFromCache );
   return response;
}
public static WebResponse GetResponseUsingCacheDefault(Uri uri)
{
    // Set  the default cache policy level for the "http:" scheme.
    RequestCachePolicy policy = new RequestCachePolicy();
    // Create the request.
    WebRequest request = WebRequest.Create(uri);
    request.CachePolicy = policy;
    WebResponse response = request.GetResponse();
    Console.WriteLine("Policy level is {0}.", policy.Level.ToString());
    Console.WriteLine("Is the response from the cache? {0}", response.IsFromCache);

    return response;
}

注解

此构造函数将 Level 属性初始化为 Default

另请参阅

适用于

RequestCachePolicy(RequestCacheLevel)

Source:
RequestCachePolicy.cs
Source:
RequestCachePolicy.cs
Source:
RequestCachePolicy.cs

初始化 RequestCachePolicy 类的新实例。 使用指定的缓存策略。

public:
 RequestCachePolicy(System::Net::Cache::RequestCacheLevel level);
public RequestCachePolicy (System.Net.Cache.RequestCacheLevel level);
new System.Net.Cache.RequestCachePolicy : System.Net.Cache.RequestCacheLevel -> System.Net.Cache.RequestCachePolicy
Public Sub New (level As RequestCacheLevel)

参数

level
RequestCacheLevel

一个 RequestCacheLevel,它为使用 WebRequest 对象获取的资源指定缓存行为。

例外

级别不是有效的 RequestCacheLevel 值。

示例

下面的代码示例创建一个 Level 设置为 CacheOnly的策略,并使用它来设置 的 WebRequest缓存策略。

static WebResponse^ GetResponseFromCache( Uri^ uri )
{
   RequestCachePolicy^ policy = gcnew RequestCachePolicy( RequestCacheLevel::CacheOnly );
   WebRequest^ request = WebRequest::Create( uri );
   request->CachePolicy = policy;
   WebResponse^ response = request->GetResponse();
   Console::WriteLine( L"Policy level is {0}.", policy->Level );
   Console::WriteLine( L"Is the response from the cache? {0}", response->IsFromCache );
   return response;
}
public static WebResponse GetResponseFromCache(Uri uri)
{
     RequestCachePolicy policy =
        new  RequestCachePolicy( RequestCacheLevel.CacheOnly);
    WebRequest request = WebRequest.Create(uri);
    request.CachePolicy = policy;
    WebResponse response = request.GetResponse();
    Console.WriteLine("Policy level is {0}.", policy.Level.ToString());
    Console.WriteLine("Is the response from the cache? {0}", response.IsFromCache);
    return response;
}

注解

此构造函数将 Level 属性初始化为 level

RequestCacheLevel 控制是否启用缓存以及何时可以使用缓存。 有关其他信息,请参阅 RequestCacheLevel 文档。

另请参阅

适用于