diff --git a/PlainHttp/HttpRequest.cs b/PlainHttp/HttpRequest.cs index 5381605..c9c563c 100644 --- a/PlainHttp/HttpRequest.cs +++ b/PlainHttp/HttpRequest.cs @@ -43,6 +43,10 @@ public class HttpRequest : IHttpRequest public Encoding ResponseEncoding { get; set; } + public HttpCompletionOption HttpCompletionOption { get; set; } = HttpCompletionOption.ResponseContentRead; + + public bool ReadBody { get; set; } = true; + private static AsyncLocal testingMode = new AsyncLocal(); @@ -60,7 +64,7 @@ public HttpRequest(string url) this.Uri = new Uri(url); } - public async Task SendAsync(CancellationToken cancellationToken = default(CancellationToken)) + public async Task SendAsync(CancellationToken cancellationToken = default(CancellationToken)) { if (testingMode.Value != null) { @@ -128,7 +132,7 @@ public HttpRequest(string url) } // Send the request - responseMessage = await client.SendAsync(requestMessage, cts.Token).ConfigureAwait(false); + responseMessage = await client.SendAsync(requestMessage, this.HttpCompletionOption, cts.Token).ConfigureAwait(false); // Wrap the content into an HttpResponse instance, // also reading the body (string or file) @@ -145,24 +149,19 @@ public HttpRequest(string url) } } - private async Task CreateHttpResponse(HttpResponseMessage responseMessage) + private async Task CreateHttpResponse(HttpResponseMessage responseMessage) { - // No file name, given read the body of the response as string + // No file name given, read the body of the response as a string if (this.DownloadFileName == null) { - string body; + IHttpResponse response = new HttpResponse(this, responseMessage); - if (this.ResponseEncoding != null) - { - byte[] array = await responseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false); - body = this.ResponseEncoding.GetString(array); - } - else + if (this.ReadBody) { - body = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false); + await response.ReadBody().ConfigureAwait(false); } - return new HttpResponse(this, responseMessage, body); + return response; } // Copy the response to a file else @@ -177,7 +176,7 @@ private async Task CreateHttpResponse(HttpResponseMessage response } } - private async Task MockedResponse() + private async Task MockedResponse() { // Get the testing mode instance for this async context HttpResponseMessage message = testingMode.Value.RequestsQueue.Dequeue(); diff --git a/PlainHttp/HttpResponse.cs b/PlainHttp/HttpResponse.cs index 82c8584..9bc04b7 100644 --- a/PlainHttp/HttpResponse.cs +++ b/PlainHttp/HttpResponse.cs @@ -1,5 +1,6 @@ using System.Linq; using System.Net.Http; +using System.Threading.Tasks; namespace PlainHttp { @@ -48,5 +49,18 @@ public string GetSingleHeader(string name) return null; } } + + public async Task ReadBody() + { + if (this.Request.ResponseEncoding != null) + { + byte[] array = await Message.Content.ReadAsByteArrayAsync().ConfigureAwait(false); + this.Body = this.Request.ResponseEncoding.GetString(array); + } + else + { + this.Body = await this.Message.Content.ReadAsStringAsync().ConfigureAwait(false); + } + } } } diff --git a/PlainHttp/IHttpRequest.cs b/PlainHttp/IHttpRequest.cs index 96e439b..58fc064 100644 --- a/PlainHttp/IHttpRequest.cs +++ b/PlainHttp/IHttpRequest.cs @@ -17,7 +17,9 @@ public interface IHttpRequest Uri Proxy { get; set; } TimeSpan Timeout { get; set; } Uri Uri { get; set; } + HttpCompletionOption HttpCompletionOption { get; set; } + bool ReadBody { get; set; } - Task SendAsync(CancellationToken cancellationToken = default(CancellationToken)); + Task SendAsync(CancellationToken cancellationToken = default(CancellationToken)); } } diff --git a/PlainHttp/IHttpResponse.cs b/PlainHttp/IHttpResponse.cs index cc4c400..fed7829 100644 --- a/PlainHttp/IHttpResponse.cs +++ b/PlainHttp/IHttpResponse.cs @@ -1,4 +1,5 @@ using System.Net.Http; +using System.Threading.Tasks; namespace PlainHttp { @@ -10,5 +11,6 @@ public interface IHttpResponse bool Succeeded { get; } string GetSingleHeader(string name); + Task ReadBody(); } } diff --git a/PlainHttp/PlainHttp.csproj b/PlainHttp/PlainHttp.csproj index 7c54b70..c1fb4ce 100644 --- a/PlainHttp/PlainHttp.csproj +++ b/PlainHttp/PlainHttp.csproj @@ -3,7 +3,7 @@ netcoreapp2.2 7.3 - 1.0.3 + 1.1.0 Easy HTTP client with support for serialization, proxies, testing and more https://github.com/matteocontrini/PlainHttp git