WebRequest.CachePolicy 속성

정의

이 요청에 대한 캐시 정책을 가져오거나 설정합니다.

public:
 virtual property System::Net::Cache::RequestCachePolicy ^ CachePolicy { System::Net::Cache::RequestCachePolicy ^ get(); void set(System::Net::Cache::RequestCachePolicy ^ value); };
public virtual System.Net.Cache.RequestCachePolicy? CachePolicy { get; set; }
public virtual System.Net.Cache.RequestCachePolicy CachePolicy { get; set; }
member this.CachePolicy : System.Net.Cache.RequestCachePolicy with get, set
Public Overridable Property CachePolicy As RequestCachePolicy

속성 값

캐시 정책을 정의하는 RequestCachePolicy 개체입니다.

예제

다음 코드 예제에서는 웹 요청에 대 한 캐시 정책을 설정 하는 방법을 보여 줍니다.

// The following method demonstrates overriding the
// caching policy for a request.
static WebResponse^ GetResponseNoCache( Uri^ uri )
{
   // Set a default policy level for the "http:" and "https" schemes.
   HttpRequestCachePolicy^ policy = gcnew HttpRequestCachePolicy( HttpRequestCacheLevel::Default );
   HttpWebRequest::DefaultCachePolicy = policy;

   // Create the request.
   WebRequest^ request = WebRequest::Create( uri );

   // Define a cache policy for this request only. 
   HttpRequestCachePolicy^ noCachePolicy = gcnew HttpRequestCachePolicy( HttpRequestCacheLevel::NoCacheNoStore );
   request->CachePolicy = noCachePolicy;
   WebResponse^ response = request->GetResponse();
   Console::WriteLine( L"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("IsFromCache? {0}", response.IsFromCache);
    return response;
}

설명

현재 캐시 정책 및 캐시에 요청된 된 리소스의 현재 상태 응답을 캐시에서 검색할 수 있는지 여부를 결정 합니다. 일반적으로 캐시 된 응답을 사용 하 여 애플리케이션 성능이 향상 됩니다. 하지만 캐시에서 응답 서버의 응답에 맞지 않습니다 위험이 있습니다.

기본 캐시 정책은 Machine.config 구성 파일에서 지정하거나 HTTP(Hypertext Transfer Protocol) 또는 보안 HTTPS(Hypertext Transfer Protocol) URI 체계를 사용하는 요청에 대한 속성을 설정 DefaultCachePolicy 하여 지정할 수 있습니다.

응답 스트림에 리소스를 검색 하 고 스트림의 끝까지 읽는 경우에 리소스의 복사본이 캐시에 추가 됩니다. 이므로 동일한 리소스에 대 한 다른 요청에는이 요청에 대 한 캐시 정책 수준에 따라 캐시 된 복사본을 사용할 수 있습니다.

적용 대상

추가 정보