共用方式為


RequestCachePolicy 建構函式

定義

初始化 RequestCachePolicy 類別的新執行個體。

多載

RequestCachePolicy()

初始化 RequestCachePolicy 類別的新執行個體。

RequestCachePolicy(RequestCacheLevel)

初始化 RequestCachePolicy 類別的新執行個體。 使用指定的快取原則。

RequestCachePolicy()

來源:
RequestCachePolicy.cs
來源:
RequestCachePolicy.cs
來源:
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)

來源:
RequestCachePolicy.cs
來源:
RequestCachePolicy.cs
來源:
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 值。

範例

下列程式代碼範例會建立設定 LevelCacheOnly的原則,並使用它來設定的 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 檔。

另請參閱

適用於