internal void ReadAll() { WebConnectionStream wce = stream as WebConnectionStream; if (wce == null) { return; } try { wce.ReadAll(); } catch { } }
// Returns true if redirected async Task<bool> CheckFinalStatusAsync() { //if (result.IsFaulted) //{ // _bodyBuffer = null; // throw result.Exception; //} Exception throwMe = null; //var resp = result.Result; var protoError = WebExceptionStatus.ProtocolError; HttpStatusCode code = 0; if (throwMe == null && _webResponse != null) { code = _webResponse.StatusCode; if ((!_authState.IsCompleted && code == HttpStatusCode.Unauthorized && _credentials != null) || (ProxyQuery && !_proxyAuthState.IsCompleted && code == HttpStatusCode.ProxyAuthenticationRequired)) { if (!_usedPreAuth && CheckAuthorization(_webResponse, code)) { // Keep the written body, so it can be rewritten in the retry if (InternalAllowBuffering) { // NTLM: This is to avoid sending data in the 'challenge' request // We save it in the first request (first 401), don't send anything // in the challenge request and send it in the response request along // with the buffers kept form the first request. if (_authState.NtlmAuthState == NtlmAuthState.Challenge || _proxyAuthState.NtlmAuthState == NtlmAuthState.Challenge) { _bodyBuffer = _writeStream.WriteBuffer; _bodyBufferLength = _writeStream.WriteBufferLength; } return true; } if (_method != "PUT" && _method != "POST") { _bodyBuffer = null; return true; } if (!ThrowOnError) return false; _writeStream.InternalClose(); _writeStream = null; _webResponse.Close(); _webResponse = null; _bodyBuffer = null; throw new WebException("This request requires buffering " + "of data for authentication or " + "redirection to be sucessful."); } } _bodyBuffer = null; if ((int)code >= 400) { var err = String.Format("The remote server returned an error: ({0}) {1}.", (int)code, _webResponse.StatusDescription); throwMe = new WebException(err, null, protoError, _webResponse); await _webResponse.ReadAllAsync().ConfigureAwait(false); } else if ((int)code == 304 && AllowAutoRedirect) { var err = String.Format("The remote server returned an error: ({0}) {1}.", (int)code, _webResponse.StatusDescription); throwMe = new WebException(err, null, protoError, _webResponse); } else if ((int)code >= 300 && AllowAutoRedirect && _redirects >= _maxAutoRedirect) { throwMe = new WebException("Max. redirections exceeded.", null, protoError, _webResponse); await _webResponse.ReadAllAsync().ConfigureAwait(false); } } _bodyBuffer = null; if (throwMe == null) { var b = false; var c = (int)code; if (AllowAutoRedirect && c >= 300) { b = Redirect(code); if (InternalAllowBuffering && _writeStream.WriteBufferLength > 0) { _bodyBuffer = _writeStream.WriteBuffer; _bodyBufferLength = _writeStream.WriteBufferLength; } if (b && !UnsafeAuthenticatedConnectionSharing) { _authState.Reset(); _proxyAuthState.Reset(); } } if (_previousWebResponse != null && c >= 300 && c != 304) await _previousWebResponse.ReadAllAsync().ConfigureAwait(false); return b; } if (!ThrowOnError) return false; if (_writeStream != null) { _writeStream.InternalClose(); _writeStream = null; } _webResponse = null; throw throwMe; }
public override void Abort() { if (Interlocked.CompareExchange(ref _aborted, 1, 0) == 1) return; if (_haveResponse && FinishedReading) return; _haveResponse = true; if (_abortHandler != null) { try { _abortHandler(this, EventArgs.Empty); } catch (Exception) { } _abortHandler = null; } if (_asyncWrite != null) { var r = _asyncWrite; if (!r.Task.IsCompleted) { try { var wexc = new WebException("Aborted.", WebExceptionStatus.RequestCanceled); r.TrySetException(wexc); } catch { } } _asyncWrite = null; } if (_asyncRead != null) { var r = _asyncRead; if (!r.Task.IsCompleted) { try { var wexc = new WebException("Aborted.", WebExceptionStatus.RequestCanceled); r.TrySetException(wexc); } catch { } } _asyncRead = null; } if (_writeStream != null) { try { _writeStream.Close(); _writeStream = null; } catch { } } if (_webResponse != null) { try { _webResponse.Close(); _webResponse = null; } catch { } } }
internal async Task SetWriteStreamAsync(WebConnectionStream stream) { if (Aborted) return; _writeStream = stream; if (_bodyBuffer != null) { _webHeaders.RemoveInternal("Transfer-Encoding"); _contentLength = _bodyBufferLength; _writeStream.SendChunked = false; } try { await _writeStream.SetHeadersAsync(false).ConfigureAwait(false); await SetWriteStreamCB().ConfigureAwait(false); } catch (Exception exc) { SetWriteStreamErrorCB(exc); } }