/// <summary> /// Sends the asynchronous request to the server. /// </summary> /// <param name="request">The request to be send.</param> /// <param name="cancellationToken">The operation cancellation token.</param> /// <returns>The Task representing the asynchronous operation.</returns> protected override async Task <HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { try { // Validate that the object is no disposed this.EnsureNotDisposed(); // Execute the request var convertedRequest = await WindowsHttpMessageHandler.ConvertRequest(request, cancellationToken).ConfigureAwait(false); var response = await this.client.SendRequestAsync(convertedRequest).AsTask(cancellationToken).ConfigureAwait(false); var convertedResponse = await WindowsHttpMessageHandler.ConvertResponse(response, cancellationToken).ConfigureAwait(false); // Return the converted response return(convertedResponse); } catch (Exception ex) { throw new WebException(ex.Message, ex, WebExceptionStatus.ConnectFailure, null); } }
/// <summary> /// Converts the received response message. /// </summary> /// <param name="response">The request message to be transformed.</param> /// <param name="cancellationToken">The operation cancellation token.</param> /// <returns>The Task representing the asynchronous operation.</returns> private static async Task <HttpResponseMessage> ConvertResponse( Windows.Web.Http.HttpResponseMessage response, CancellationToken cancellationToken) { // Convert the response var converted = new HttpResponseMessage((HttpStatusCode)(int)response.StatusCode) { ReasonPhrase = response.ReasonPhrase, RequestMessage = await WindowsHttpMessageHandler .ConvertRequest(response.RequestMessage, cancellationToken) .ConfigureAwait(false), Content = await WindowsHttpMessageHandler.ConvertContent(response.Content, cancellationToken) .ConfigureAwait(false), Version = WindowsHttpMessageHandler.GetVersion(response.Version) }; // Copy headers foreach (var header in response.Headers) { converted.Headers.TryAddWithoutValidation(header.Key, header.Value); } // Return the converted message return(converted); }