internal async Task <SimpleHttpResponse> SendAsync(HttpContent requestContent) { var requestMessage = new HttpRequestMessage { Method = this.Method, RequestUri = this.Uri }; if (this.Method == HttpMethod.Post || this.Method == HttpMethod.Put) { if (requestContent != null) { requestMessage.Content = requestContent; } } requestMessage.Headers.Clear(); foreach (var header in this.Headers) { requestMessage.Headers.Add(header.Key, header.Value); } if (this.HttpProxy != null) { this.handler.Proxy = new SimpleProxy(this.HttpProxy); } this.handler.SkipCertificateValidation = this.SkipCertificateValidation; this.client.Timeout = this.Timeout; var startTime = DateTime.Now; try { var result = await this.client.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, this.cancellationToken).ConfigureAwait(false); var headers = new HttpHeadersCollection(result.Headers); HttpContent content = null; if (result.Content != null) { headers.AddRange(result.Content.Headers); content = result.Content; } return(new SimpleHttpResponse(content, headers, result.StatusCode)); } catch (Exception ex) { var tcex = ex as TaskCanceledException; if (tcex == null) { throw; } if (this.cancellationToken.IsCancellationRequested) { throw new OperationCanceledException("The operation was canceled by user request.", tcex, this.cancellationToken); } if (DateTime.Now - startTime > this.Timeout) { throw new TimeoutException(string.Format(CultureInfo.InvariantCulture, "The task failed to complete in the given timeout period ({0}).", this.Timeout), tcex); } throw; } }
/// <summary> /// Initializes a new instance of the <see cref="SimpleHttpResponse"/> class. /// </summary> /// <param name="content">The http content.</param> /// <param name="headers">The http headers.</param> /// <param name="status">The http status.</param> public SimpleHttpResponse(HttpContent content, HttpHeadersCollection headers, HttpStatusCode status) { this.Headers = headers ?? new HttpHeadersCollection(); this.StatusCode = status; this.Content = content; }