private void SetTaskCompleted(HttpRequestMessage request, CancellationTokenSource cancellationTokenSource, TaskCompletionSource <HttpResponseMessage> tcs, HttpResponseMessage response) { if (HttpEventSource.Log.IsEnabled()) { HttpEventSource.ClientSendCompleted(this, response, request); } tcs.TrySetResult(response); cancellationTokenSource.Dispose(); }
private async Task <HttpResponseMessage> FinishSendAsync( Task <HttpResponseMessage> sendTask, HttpRequestMessage request, CancellationTokenSource linkedCts, bool bufferResponseContent) { HttpResponseMessage response = null; try { try { // Wait for the send request to complete, getting back the response. response = await sendTask.ConfigureAwait(false); } finally { // When a request completes, dispose the request content so the user doesn't have to. This also // ensures that a HttpContent object is only sent once using HttpClient (similar to HttpRequestMessages // that can also be sent only once). request.Content?.Dispose(); } if (response == null) { throw new InvalidOperationException(SR.net_http_handler_noresponse); } // Buffer the response content if we've been asked to and we have a Content to buffer. if (bufferResponseContent && response.Content != null) { await response.Content.LoadIntoBufferAsync(_maxResponseContentBufferSize).ConfigureAwait(false); } if (HttpEventSource.Log.IsEnabled()) { HttpEventSource.ClientSendCompleted(this, response, request); } return(response); } catch (Exception e) { response?.Dispose(); // If the cancellation token was canceled, we consider the exception to be caused by the // cancellation (e.g. WebException when reading from canceled response stream). if (linkedCts.IsCancellationRequested && e is HttpRequestException) { LogSendError(request, linkedCts, nameof(SendAsync), null); throw new OperationCanceledException(linkedCts.Token); } else { LogSendError(request, linkedCts, nameof(SendAsync), e); if (NetEventSource.Log.IsEnabled()) { NetEventSource.Exception(NetEventSource.ComponentType.Http, this, nameof(SendAsync), e); } throw; } } finally { linkedCts.Dispose(); } }