System.Net.Cache.RequestCacheValidator.FetchRequest C# (CSharp) Method

FetchRequest() private method

private FetchRequest ( Uri uri, WebRequest request ) : void
uri System.Uri
request System.Net.WebRequest
return void
        internal void FetchRequest(Uri uri, WebRequest request)
        {
            _Request = request;
            _Policy  = request.CachePolicy;
            _Response = null;
            _ResponseCount = 0;
            _ValidationStatus     = CacheValidationStatus.DoNotUseCache;
            _CacheFreshnessStatus = CacheFreshnessStatus.Undefined;
            _CacheStream          = null;
            _CacheStreamOffset    = 0L;
            _CacheStreamLength    = 0L;

            if (!uri.Equals(_Uri))
            {
                // it's changed from previous call
                _CacheKey = uri.GetParts(UriComponents.AbsoluteUri, UriFormat.Unescaped);
            }
            _Uri = uri;
        }
        //

Usage Example

        //
        internal CacheValidationStatus  GetRetrieveStatus(Uri cacheUri, WebRequest request)
        {
            if (cacheUri == null)
            {
                throw new ArgumentNullException("cacheUri");
            }

            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            if (!_CanTakeNewRequest || _ProtocolStatus == CacheValidationStatus.RetryResponseFromServer)
            {
                return(CacheValidationStatus.Continue);
            }
            _CanTakeNewRequest = false;


            // Reset protocol state
            _ResponseStream       = null;
            _ResponseStreamLength = 0L;
            _ProtocolStatus       = CacheValidationStatus.Continue;
            _ProtocolException    = null;

            if (Logging.On)
            {
                Logging.Enter(Logging.RequestCache, this, "GetRetrieveStatus", request);
            }
            try {
                if (request.CachePolicy == null || request.CachePolicy.Level == RequestCacheLevel.BypassCache)
                {
                    _ProtocolStatus = CacheValidationStatus.DoNotUseCache;
                    return(_ProtocolStatus);
                }

                if (_RequestCache == null || _Validator == null)
                {
                    _ProtocolStatus = CacheValidationStatus.DoNotUseCache;
                    return(_ProtocolStatus);
                }

                _Validator.FetchRequest(cacheUri, request);

                switch (_ProtocolStatus = ValidateRequest())
                {
                case CacheValidationStatus.Continue:            // This is a green light for cache protocol
                    break;

                case CacheValidationStatus.DoNotTakeFromCache:  // no cache but response can be cached
                case CacheValidationStatus.DoNotUseCache:       // ignore cache entirely
                    break;

                case CacheValidationStatus.Fail:
                    _ProtocolException = new InvalidOperationException(SR.GetString(SR.net_cache_validator_fail, "ValidateRequest"));
                    break;

                default:
                    _ProtocolStatus    = CacheValidationStatus.Fail;
                    _ProtocolException = new InvalidOperationException(SR.GetString(SR.net_cache_validator_result, "ValidateRequest", _Validator.ValidationStatus.ToString()));
                    if (Logging.On)
                    {
                        Logging.PrintError(Logging.RequestCache, SR.GetString(SR.net_log_cache_unexpected_status, "ValidateRequest()", _Validator.ValidationStatus.ToString()));
                    }
                    break;
                }

                if (_ProtocolStatus != CacheValidationStatus.Continue)
                {
                    return(_ProtocolStatus);
                }

                //
                // Proceed with validation
                //
                CheckRetrieveBeforeSubmit();
            }
            catch (Exception e) {
                _ProtocolException = e;
                _ProtocolStatus    = CacheValidationStatus.Fail;
                if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException)
                {
                    throw;
                }

                if (Logging.On)
                {
                    Logging.PrintError(Logging.RequestCache, SR.GetString(SR.net_log_cache_object_and_exception, "CacheProtocol#" + this.GetHashCode().ToString(NumberFormatInfo.InvariantInfo), (e is WebException? e.Message: e.ToString())));
                }
            }
            finally {
                if (Logging.On)
                {
                    Logging.Exit(Logging.RequestCache, this, "GetRetrieveStatus", "result = " + _ProtocolStatus.ToString());
                }
            }
            return(_ProtocolStatus);
        }