System.Net.HttpWebRequest.Abort C# (CSharp) Метод

Abort() публичный Метод

public Abort ( ) : void
Результат void
        public override void Abort()
        {
            if (Interlocked.Exchange(ref _abortCalled, 1) != 0)
            {
                return;
            }

            // .NET Desktop behavior requires us to invoke outstanding callbacks
            // before returning if used in either the BeginGetRequestStream or
            // BeginGetResponse methods.
            //
            // If we can transition the task to the canceled state, then we invoke
            // the callback. If we can't transition the task, it is because it is
            // already in the terminal state and the callback has already been invoked
            // via the async task continuation.

            if (_responseOperation != null)
            {
                if (_responseOperation.TrySetCanceled() && _responseCallback != null)
                {
                    _responseCallback(_responseOperation.Task);
                }

                // Cancel the underlying send operation.
                Debug.Assert(_sendRequestCts != null);
                _sendRequestCts.Cancel();
            }
            else if (_requestStreamOperation != null)
            {
                if (_requestStreamOperation.TrySetCanceled() && _requestStreamCallback != null)
                {
                    _requestStreamCallback(_requestStreamOperation.Task);
                }
            }
        }

Usage Example

Пример #1
0
        // Simple WebRequest
        public string ExecuteWebRequest(HttpWebRequest webRequest)
        {
            try
            {
                var webRequestResult = GetWebRequestResultFromHttpClient(webRequest);

                if (!webRequestResult.IsSuccessStatusCode)
                {
                    throw _exceptionHandler.TryLogFailedWebRequestResult(webRequestResult);
                }

                var stream = webRequestResult.ResultStream;

                if (stream != null)
                {
                    // Getting the result
                    var responseReader = new StreamReader(stream);
                    return responseReader.ReadLine();
                }

                // Closing the connection
                webRequest.Abort();
            }
            catch (AggregateException aex)
            {
                var webException = aex.InnerException as WebException;
                var httpRequestMessageException = aex.InnerException as HttpRequestException;

                if (httpRequestMessageException != null)
                {
                    webException = httpRequestMessageException.InnerException as WebException;
                }

                if (webException != null)
                {
                    if (webRequest != null)
                    {
                        webRequest.Abort();

                        throw _exceptionHandler.TryLogWebException(webException, webRequest.RequestUri.AbsoluteUri);
                    }

                    throw webException;
                }

                throw;
            }
            catch (TimeoutException)
            {
                var twitterTimeoutException = _twitterTimeoutExceptionFactory.Create(new ConstructorNamedParameter("url", webRequest.RequestUri.AbsoluteUri));
                if (_exceptionHandler.LogExceptions)
                {
                    _exceptionHandler.AddTwitterException(twitterTimeoutException);
                }

                throw (Exception)twitterTimeoutException;
            }

            return null;
        }
All Usage Examples Of System.Net.HttpWebRequest::Abort