diff --git a/src/HttpClientServiceHelper/Delete.cs b/src/HttpClientServiceHelper/Delete.cs
index 827b2d7..7df2c25 100644
--- a/src/HttpClientServiceHelper/Delete.cs
+++ b/src/HttpClientServiceHelper/Delete.cs
@@ -1,4 +1,4 @@
-using HttpClientServiceHelper.Models;
+using HttpClientServiceHelper.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
@@ -17,11 +17,7 @@ public partial class HttpClientHelper
/// an HTTP Response Message
public static async Task DeleteAsync(string Route)
{
- using (HttpClient httpClient = new HttpClient())
- {
- var httpResponse = await httpClient.DeleteAsync(new Uri(Route));
- return httpResponse;
- }
+ return await _httpClient.DeleteAsync(new Uri(Route));
}
///
/// Triggers a DELETE request to the specified route with a Bearer token and retrieves the result as a string which you can serialize.
@@ -31,12 +27,10 @@ public static async Task DeleteAsync(string Route)
/// an HTTP Response Message
public static async Task DeleteAsync(string Route, string Token = null)
{
- using (HttpClient httpClient = new HttpClient())
- {
- httpClient.DefaultRequestHeaders.Authorization = !string.IsNullOrEmpty(Token) ? new AuthenticationHeaderValue("Bearer", Token) : null;
- var httpResponse = await httpClient.DeleteAsync(new Uri(Route));
- return httpResponse;
- }
+ using var request = new HttpRequestMessage(HttpMethod.Delete, new Uri(Route));
+ if (!string.IsNullOrEmpty(Token))
+ request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Token);
+ return await _httpClient.SendAsync(request);
}
///
/// Triggers a DELETE request with headers to the specified route and retrieves the result as a string which you can serialize.
@@ -46,15 +40,10 @@ public static async Task DeleteAsync(string Route, string T
/// an HTTP Response Message
public static async Task DeleteAsync(string Route, List Headers)
{
- using (HttpClient httpClient = new HttpClient())
- {
- foreach (var Header in Headers)
- {
- httpClient.DefaultRequestHeaders.Add(Header.Name, Header.Value);
- }
- var httpResponse = await httpClient.DeleteAsync(new Uri(Route));
- return httpResponse;
- }
+ using var request = new HttpRequestMessage(HttpMethod.Delete, new Uri(Route));
+ foreach (var Header in Headers)
+ request.Headers.TryAddWithoutValidation(Header.Name, Header.Value);
+ return await _httpClient.SendAsync(request);
}
///
/// Triggers a DELETE request with headers and authorization to the specified route and retrieves the result as a string which you can serialize.
@@ -65,17 +54,13 @@ public static async Task DeleteAsync(string Route, Listan HTTP Response Message
public static async Task DeleteAsync(string Route, List Headers, Authorization Authorization)
{
- using (HttpClient httpClient = new HttpClient())
- {
- foreach (var Header in Headers)
- {
- httpClient.DefaultRequestHeaders.Add(Header.Name, Header.Value);
- }
- httpClient.DefaultRequestHeaders.Authorization = string.IsNullOrEmpty(Authorization.Parameter) ?
- new AuthenticationHeaderValue(Authorization.Scheme) : new AuthenticationHeaderValue(Authorization.Scheme, Authorization.Parameter);
- var httpResponse = await httpClient.DeleteAsync(new Uri(Route));
- return httpResponse;
- }
+ using var request = new HttpRequestMessage(HttpMethod.Delete, new Uri(Route));
+ foreach (var Header in Headers)
+ request.Headers.TryAddWithoutValidation(Header.Name, Header.Value);
+ request.Headers.Authorization = string.IsNullOrEmpty(Authorization.Parameter)
+ ? new AuthenticationHeaderValue(Authorization.Scheme)
+ : new AuthenticationHeaderValue(Authorization.Scheme, Authorization.Parameter);
+ return await _httpClient.SendAsync(request);
}
///
@@ -85,11 +70,8 @@ public static async Task DeleteAsync(string Route, Lista string format of the HTTP Response message
public static async Task DeleteAndGetResponseAsStringAsync(string Route)
{
- using (HttpClient httpClient = new HttpClient())
- {
- var httpResponse = await httpClient.DeleteAsync(new Uri(Route));
- return await httpResponse.Content.ReadAsStringAsync();
- }
+ var httpResponse = await _httpClient.DeleteAsync(new Uri(Route));
+ return await httpResponse.Content.ReadAsStringAsync();
}
///
/// Triggers a DELETE request to the specified route with a Bearer token and retrieves the result as a string which you can serialize.
@@ -99,12 +81,11 @@ public static async Task DeleteAndGetResponseAsStringAsync(string Route)
/// a string format of the HTTP Response message
public static async Task DeleteAndGetResponseAsStringAsync(string Route, string Token = null)
{
- using (HttpClient httpClient = new HttpClient())
- {
- httpClient.DefaultRequestHeaders.Authorization = !string.IsNullOrEmpty(Token) ? new AuthenticationHeaderValue("Bearer", Token) : null;
- var httpResponse = await httpClient.DeleteAsync(new Uri(Route));
- return await httpResponse.Content.ReadAsStringAsync();
- }
+ using var request = new HttpRequestMessage(HttpMethod.Delete, new Uri(Route));
+ if (!string.IsNullOrEmpty(Token))
+ request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Token);
+ var httpResponse = await _httpClient.SendAsync(request);
+ return await httpResponse.Content.ReadAsStringAsync();
}
///
/// Triggers a DELETE request with headers to the specified route and retrieves the result as a string which you can serialize.
@@ -114,15 +95,11 @@ public static async Task DeleteAndGetResponseAsStringAsync(string Route,
/// a string format of the HTTP Response message
public static async Task DeleteAndGetResponseAsStringAsync(string Route, List Headers)
{
- using (HttpClient httpClient = new HttpClient())
- {
- foreach (var Header in Headers)
- {
- httpClient.DefaultRequestHeaders.Add(Header.Name, Header.Value);
- }
- var httpResponse = await httpClient.DeleteAsync(new Uri(Route));
- return await httpResponse.Content.ReadAsStringAsync();
- }
+ using var request = new HttpRequestMessage(HttpMethod.Delete, new Uri(Route));
+ foreach (var Header in Headers)
+ request.Headers.TryAddWithoutValidation(Header.Name, Header.Value);
+ var httpResponse = await _httpClient.SendAsync(request);
+ return await httpResponse.Content.ReadAsStringAsync();
}
///
/// Triggers a DELETE request with headers and authorization to the specified route and retrieves the result as a string which you can serialize.
@@ -133,17 +110,14 @@ public static async Task DeleteAndGetResponseAsStringAsync(string Route,
/// a string format of the HTTP Response message
public static async Task DeleteAndGetResponseAsStringAsync(string Route, List Headers, Authorization Authorization)
{
- using (HttpClient httpClient = new HttpClient())
- {
- foreach (var Header in Headers)
- {
- httpClient.DefaultRequestHeaders.Add(Header.Name, Header.Value);
- }
- httpClient.DefaultRequestHeaders.Authorization = string.IsNullOrEmpty(Authorization.Parameter) ?
- new AuthenticationHeaderValue(Authorization.Scheme) : new AuthenticationHeaderValue(Authorization.Scheme, Authorization.Parameter);
- var httpResponse = await httpClient.DeleteAsync(new Uri(Route));
- return await httpResponse.Content.ReadAsStringAsync();
- }
+ using var request = new HttpRequestMessage(HttpMethod.Delete, new Uri(Route));
+ foreach (var Header in Headers)
+ request.Headers.TryAddWithoutValidation(Header.Name, Header.Value);
+ request.Headers.Authorization = string.IsNullOrEmpty(Authorization.Parameter)
+ ? new AuthenticationHeaderValue(Authorization.Scheme)
+ : new AuthenticationHeaderValue(Authorization.Scheme, Authorization.Parameter);
+ var httpResponse = await _httpClient.SendAsync(request);
+ return await httpResponse.Content.ReadAsStringAsync();
}
///
diff --git a/src/HttpClientServiceHelper/Get.cs b/src/HttpClientServiceHelper/Get.cs
index 4e7f579..60e5969 100644
--- a/src/HttpClientServiceHelper/Get.cs
+++ b/src/HttpClientServiceHelper/Get.cs
@@ -1,324 +1,301 @@
-using HttpClientServiceHelper.Models;
-using Newtonsoft.Json;
-using System;
-using System.Collections.Generic;
-using System.Net.Http;
-using System.Net.Http.Headers;
-using System.Threading.Tasks;
-
-namespace HttpClientServiceHelper
-{
- ///
- /// HTTP Client Helper Library
- ///
- public partial class HttpClientHelper
- {
- #region TODO
- ///
- /// Triggers a GET request to the specified route and retrieves the result as a generic object response.
- ///
- ///
- /// An object
- //public static async Task> GetAsGenericModelAsync(string Route)
- //{
- // using (HttpClient httpClient = new HttpClient())
- // {
- // var httpResponse = await httpClient.GetAsync(new Uri(Route));
- // if (httpResponse.IsSuccessStatusCode)
- // {
- // if (typeof(T) == typeof(string))
- // {
- // return new GenericModel()
- // {
- // Response = (T)Convert.ChangeType(await httpResponse.Content.ReadAsStringAsync(), typeof(T)),
- // StatusCode = (int)httpResponse.StatusCode
- // };
- // }
- // else
- // {
- // return new GenericModel()
- // {
- // Response = JsonConvert.DeserializeObject(await httpResponse.Content.ReadAsStringAsync()),
- // StatusCode = (int)httpResponse.StatusCode
- // };
- // }
- // }
- // else
- // {
- // return new GenericModel()
- // {
- // StatusCode = (int)httpResponse.StatusCode,
- // ErrorMessage = await httpResponse.Content.ReadAsStringAsync()
- // };
- // }
- // }
- //}
- ///
- /// Triggers a GET request with bearer token to the specified route and retrieves the result as a generic object response.
- ///
- ///
- /// An object
- //public static async Task> GetAsGenericModelAsync(string Route, string Token)
- //{
- // using (HttpClient httpClient = new HttpClient())
- // {
- // httpClient.DefaultRequestHeaders.Authorization = !string.IsNullOrEmpty(Token) ? new AuthenticationHeaderValue("Bearer", Token) : null;
- // var httpResponse = await httpClient.GetAsync(new Uri(Route));
- // if (httpResponse.IsSuccessStatusCode)
- // {
- // if (typeof(T) == typeof(string))
- // {
- // return new GenericModel()
- // {
- // Response = (T)Convert.ChangeType(await httpResponse.Content.ReadAsStringAsync(), typeof(T)),
- // StatusCode = (int)httpResponse.StatusCode
- // };
- // }
- // else
- // {
- // return new GenericModel()
- // {
- // Response = JsonConvert.DeserializeObject(await httpResponse.Content.ReadAsStringAsync()),
- // StatusCode = (int)httpResponse.StatusCode
- // };
- // }
- // }
- // else
- // {
- // return new GenericModel()
- // {
- // StatusCode = (int)httpResponse.StatusCode,
- // ErrorMessage = await httpResponse.Content.ReadAsStringAsync()
- // };
- // }
- // }
- //}
- ///
- /// Triggers a GET request with headers to the specified route and retrieves the result as a generic object response.
- ///
- ///
- ///
- /// An object
- //public static async Task> GetAsGenericModelAsync(string Route, List Headers)
- //{
- // using (HttpClient httpClient = new HttpClient())
- // {
- // foreach (var Header in Headers)
- // {
- // httpClient.DefaultRequestHeaders.Add(Header.Name, Header.Value);
- // }
- // var httpResponse = await httpClient.GetAsync(new Uri(Route));
- // if (httpResponse.IsSuccessStatusCode)
- // {
- // if (typeof(T) == typeof(string))
- // {
- // return new GenericModel()
- // {
- // Response = (T)Convert.ChangeType(await httpResponse.Content.ReadAsStringAsync(), typeof(T)),
- // StatusCode = (int)httpResponse.StatusCode
- // };
- // }
- // else
- // {
- // return new GenericModel()
- // {
- // Response = JsonConvert.DeserializeObject(await httpResponse.Content.ReadAsStringAsync()),
- // StatusCode = (int)httpResponse.StatusCode
- // };
- // }
- // }
- // else
- // {
- // return new GenericModel()
- // {
- // StatusCode = (int)httpResponse.StatusCode,
- // ErrorMessage = await httpResponse.Content.ReadAsStringAsync()
- // };
- // }
- // }
- //}
- ///
- /// Triggers a GET request with headers and authorization to the specified route and retrieves the result as a generic object response.
- ///
- ///
- ///
- ///
- /// An object
- //public static async Task> GetAsGenericModelAsync(string Route, List Headers, Authorization Authorization)
- //{
- // using (HttpClient httpClient = new HttpClient())
- // {
- // httpClient.DefaultRequestHeaders.Authorization = string.IsNullOrEmpty(Authorization.Parameter) ?
- // new AuthenticationHeaderValue(Authorization.Scheme) : new AuthenticationHeaderValue(Authorization.Scheme, Authorization.Parameter);
- // foreach (var Header in Headers)
- // {
- // httpClient.DefaultRequestHeaders.Add(Header.Name, Header.Value);
- // }
- // var httpResponse = await httpClient.GetAsync(new Uri(Route));
- // if (httpResponse.IsSuccessStatusCode)
- // {
- // if (typeof(T) == typeof(string))
- // {
- // return new GenericModel()
- // {
- // Response = (T)Convert.ChangeType(await httpResponse.Content.ReadAsStringAsync(), typeof(T)),
- // StatusCode = (int)httpResponse.StatusCode
- // };
- // }
- // else
- // {
- // return new GenericModel()
- // {
- // Response = JsonConvert.DeserializeObject(await httpResponse.Content.ReadAsStringAsync()),
- // StatusCode = (int)httpResponse.StatusCode
- // };
- // }
- // }
- // else
- // {
- // return new GenericModel()
- // {
- // StatusCode = (int)httpResponse.StatusCode,
- // ErrorMessage = await httpResponse.Content.ReadAsStringAsync()
- // };
- // }
- // }
- //}
- #endregion
- ///
- /// Triggers a GET request to the specified route and retrieves the result as an HTTP Response Message.
- ///
- ///
- /// An HTTP Reponse Message
- public static async Task GetAsync(string Route)
- {
- using (HttpClient httpClient = new HttpClient())
- {
- var httpResponse = await httpClient.GetAsync(new Uri(Route));
- return httpResponse;
- }
- }
- ///
- /// Triggers a GET request to the specified route with a Bearer Token and retrieves the result as an HTTP Response Message.
- ///
- ///
- ///
- /// An HTTP Reponse Message
- public static async Task GetAsync(string Route, string Token = null)
- {
- using (HttpClient httpClient = new HttpClient())
- {
- httpClient.DefaultRequestHeaders.Authorization = !string.IsNullOrEmpty(Token) ? new AuthenticationHeaderValue("Bearer", Token) : null;
- var httpResponse = await httpClient.GetAsync(new Uri(Route));
- return httpResponse;
- }
- }
- ///
- /// Triggers a GET request with headers to the specified route and retrieves the result as an HTTP Response Message.
- ///
- ///
- ///
- /// An HTTP Reponse Message
- public static async Task GetAsync(string Route, List Headers)
- {
- using (HttpClient httpClient = new HttpClient())
- {
- foreach (var Header in Headers)
- {
- httpClient.DefaultRequestHeaders.Add(Header.Name, Header.Value);
- }
- var httpResponse = await httpClient.GetAsync(new Uri(Route));
- return httpResponse;
- }
- }
- ///
- /// Triggers a GET request with headers and authorization to the specified route and retrieves the result as an HTTP Response Message.
- ///
- ///
- ///
- ///
- /// An HTTP Reponse Message
- public static async Task GetAsync(string Route, List Headers, Authorization Authorization)
- {
- using (HttpClient httpClient = new HttpClient())
- {
- foreach (var Header in Headers)
- {
- httpClient.DefaultRequestHeaders.Add(Header.Name, Header.Value);
- }
- httpClient.DefaultRequestHeaders.Authorization = string.IsNullOrEmpty(Authorization.Parameter) ?
- new AuthenticationHeaderValue(Authorization.Scheme) : new AuthenticationHeaderValue(Authorization.Scheme, Authorization.Parameter);
- var httpResponse = await httpClient.GetAsync(new Uri(Route));
- return httpResponse;
- }
- }
-
- ///
- /// Triggers a GET request to the specified route and retrieves the result as a string which you can serialize.
- ///
- ///
- /// a string format of the HTTP Response message
- public static async Task GetAsStringAsync(string Route)
- {
- using (HttpClient httpClient = new HttpClient())
- {
- var httpResponse = await httpClient.GetAsync(new Uri(Route));
- return await httpResponse.Content.ReadAsStringAsync();
- }
- }
- ///
- /// Triggers a GET request to the specified route with a Bearer Token and retrieves the result as an HTTP Response Message.
- ///
- ///
- ///
- /// a string format of the HTTP Response message
- public static async Task GetAsStringAsync(string Route, string Token = null)
- {
- using (HttpClient httpClient = new HttpClient())
- {
- httpClient.DefaultRequestHeaders.Authorization = !string.IsNullOrEmpty(Token) ? new AuthenticationHeaderValue("Bearer", Token) : null;
- var httpResponse = await httpClient.GetAsync(new Uri(Route));
- return await httpResponse.Content.ReadAsStringAsync();
- }
- }
- ///
- /// Triggers a GET request with headers to the specified route and retrieves the result as an HTTP Response Message.
- ///
- ///
- ///
- /// a string format of the HTTP Response message
- public static async Task GetAsStringAsync(string Route, List Headers)
- {
- using (HttpClient httpClient = new HttpClient())
- {
- foreach (var Header in Headers)
- {
- httpClient.DefaultRequestHeaders.Add(Header.Name, Header.Value);
- }
- var httpResponse = await httpClient.GetAsync(new Uri(Route));
- return await httpResponse.Content.ReadAsStringAsync();
- }
- }
- ///
- /// Triggers a GET request with headers and authorization to the specified route and retrieves the result as an HTTP Response Message.
- ///
- ///
- ///
- ///
- /// a string format of the HTTP Response message
- public static async Task GetAsStringAsync(string Route, List Headers, Authorization Authorization)
- {
- using (HttpClient httpClient = new HttpClient())
- {
- foreach (var Header in Headers)
- {
- httpClient.DefaultRequestHeaders.Add(Header.Name, Header.Value);
- }
- httpClient.DefaultRequestHeaders.Authorization = string.IsNullOrEmpty(Authorization.Parameter) ?
- new AuthenticationHeaderValue(Authorization.Scheme) : new AuthenticationHeaderValue(Authorization.Scheme, Authorization.Parameter);
- var httpResponse = await httpClient.GetAsync(new Uri(Route));
- return await httpResponse.Content.ReadAsStringAsync();
- }
- }
- }
-}
+using HttpClientServiceHelper.Models;
+using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.Net.Http;
+using System.Net.Http.Headers;
+using System.Threading.Tasks;
+
+namespace HttpClientServiceHelper
+{
+ ///
+ /// HTTP Client Helper Library
+ ///
+ public partial class HttpClientHelper
+ {
+ // Shared instance — reusing a single HttpClient avoids socket exhaustion from per-call disposal.
+ private static readonly HttpClient _httpClient = new HttpClient();
+
+ #region TODO
+ ///
+ /// Triggers a GET request to the specified route and retrieves the result as a generic object response.
+ ///
+ ///
+ /// An object
+ //public static async Task> GetAsGenericModelAsync(string Route)
+ //{
+ // using (HttpClient httpClient = new HttpClient())
+ // {
+ // var httpResponse = await httpClient.GetAsync(new Uri(Route));
+ // if (httpResponse.IsSuccessStatusCode)
+ // {
+ // if (typeof(T) == typeof(string))
+ // {
+ // return new GenericModel()
+ // {
+ // Response = (T)Convert.ChangeType(await httpResponse.Content.ReadAsStringAsync(), typeof(T)),
+ // StatusCode = (int)httpResponse.StatusCode
+ // };
+ // }
+ // else
+ // {
+ // return new GenericModel()
+ // {
+ // Response = JsonConvert.DeserializeObject(await httpResponse.Content.ReadAsStringAsync()),
+ // StatusCode = (int)httpResponse.StatusCode
+ // };
+ // }
+ // }
+ // else
+ // {
+ // return new GenericModel()
+ // {
+ // StatusCode = (int)httpResponse.StatusCode,
+ // ErrorMessage = await httpResponse.Content.ReadAsStringAsync()
+ // };
+ // }
+ // }
+ //}
+ ///
+ /// Triggers a GET request with bearer token to the specified route and retrieves the result as a generic object response.
+ ///
+ ///
+ /// An object
+ //public static async Task> GetAsGenericModelAsync(string Route, string Token)
+ //{
+ // using (HttpClient httpClient = new HttpClient())
+ // {
+ // httpClient.DefaultRequestHeaders.Authorization = !string.IsNullOrEmpty(Token) ? new AuthenticationHeaderValue("Bearer", Token) : null;
+ // var httpResponse = await httpClient.GetAsync(new Uri(Route));
+ // if (httpResponse.IsSuccessStatusCode)
+ // {
+ // if (typeof(T) == typeof(string))
+ // {
+ // return new GenericModel()
+ // {
+ // Response = (T)Convert.ChangeType(await httpResponse.Content.ReadAsStringAsync(), typeof(T)),
+ // StatusCode = (int)httpResponse.StatusCode
+ // };
+ // }
+ // else
+ // {
+ // return new GenericModel()
+ // {
+ // Response = JsonConvert.DeserializeObject(await httpResponse.Content.ReadAsStringAsync()),
+ // StatusCode = (int)httpResponse.StatusCode
+ // };
+ // }
+ // }
+ // else
+ // {
+ // return new GenericModel()
+ // {
+ // StatusCode = (int)httpResponse.StatusCode,
+ // ErrorMessage = await httpResponse.Content.ReadAsStringAsync()
+ // };
+ // }
+ // }
+ //}
+ ///
+ /// Triggers a GET request with headers to the specified route and retrieves the result as a generic object response.
+ ///
+ ///
+ ///
+ /// An object
+ //public static async Task> GetAsGenericModelAsync(string Route, List Headers)
+ //{
+ // using (HttpClient httpClient = new HttpClient())
+ // {
+ // foreach (var Header in Headers)
+ // {
+ // httpClient.DefaultRequestHeaders.Add(Header.Name, Header.Value);
+ // }
+ // var httpResponse = await httpClient.GetAsync(new Uri(Route));
+ // if (httpResponse.IsSuccessStatusCode)
+ // {
+ // if (typeof(T) == typeof(string))
+ // {
+ // return new GenericModel()
+ // {
+ // Response = (T)Convert.ChangeType(await httpResponse.Content.ReadAsStringAsync(), typeof(T)),
+ // StatusCode = (int)httpResponse.StatusCode
+ // };
+ // }
+ // else
+ // {
+ // return new GenericModel()
+ // {
+ // Response = JsonConvert.DeserializeObject(await httpResponse.Content.ReadAsStringAsync()),
+ // StatusCode = (int)httpResponse.StatusCode
+ // };
+ // }
+ // }
+ // else
+ // {
+ // return new GenericModel()
+ // {
+ // StatusCode = (int)httpResponse.StatusCode,
+ // ErrorMessage = await httpResponse.Content.ReadAsStringAsync()
+ // };
+ // }
+ // }
+ //}
+ ///
+ /// Triggers a GET request with headers and authorization to the specified route and retrieves the result as a generic object response.
+ ///
+ ///
+ ///
+ ///
+ /// An object
+ //public static async Task> GetAsGenericModelAsync(string Route, List Headers, Authorization Authorization)
+ //{
+ // using (HttpClient httpClient = new HttpClient())
+ // {
+ // httpClient.DefaultRequestHeaders.Authorization = string.IsNullOrEmpty(Authorization.Parameter) ?
+ // new AuthenticationHeaderValue(Authorization.Scheme) : new AuthenticationHeaderValue(Authorization.Scheme, Authorization.Parameter);
+ // foreach (var Header in Headers)
+ // {
+ // httpClient.DefaultRequestHeaders.Add(Header.Name, Header.Value);
+ // }
+ // var httpResponse = await httpClient.GetAsync(new Uri(Route));
+ // if (httpResponse.IsSuccessStatusCode)
+ // {
+ // if (typeof(T) == typeof(string))
+ // {
+ // return new GenericModel()
+ // {
+ // Response = (T)Convert.ChangeType(await httpResponse.Content.ReadAsStringAsync(), typeof(T)),
+ // StatusCode = (int)httpResponse.StatusCode
+ // };
+ // }
+ // else
+ // {
+ // return new GenericModel()
+ // {
+ // Response = JsonConvert.DeserializeObject(await httpResponse.Content.ReadAsStringAsync()),
+ // StatusCode = (int)httpResponse.StatusCode
+ // };
+ // }
+ // }
+ // else
+ // {
+ // return new GenericModel()
+ // {
+ // StatusCode = (int)httpResponse.StatusCode,
+ // ErrorMessage = await httpResponse.Content.ReadAsStringAsync()
+ // };
+ // }
+ // }
+ //}
+ #endregion
+ ///
+ /// Triggers a GET request to the specified route and retrieves the result as an HTTP Response Message.
+ ///
+ ///
+ /// An HTTP Reponse Message
+ public static async Task GetAsync(string Route)
+ {
+ return await _httpClient.GetAsync(new Uri(Route));
+ }
+ ///
+ /// Triggers a GET request to the specified route with a Bearer Token and retrieves the result as an HTTP Response Message.
+ ///
+ ///
+ ///
+ /// An HTTP Reponse Message
+ public static async Task GetAsync(string Route, string Token = null)
+ {
+ using var request = new HttpRequestMessage(HttpMethod.Get, new Uri(Route));
+ if (!string.IsNullOrEmpty(Token))
+ request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Token);
+ return await _httpClient.SendAsync(request);
+ }
+ ///
+ /// Triggers a GET request with headers to the specified route and retrieves the result as an HTTP Response Message.
+ ///
+ ///
+ ///
+ /// An HTTP Reponse Message
+ public static async Task GetAsync(string Route, List Headers)
+ {
+ using var request = new HttpRequestMessage(HttpMethod.Get, new Uri(Route));
+ foreach (var Header in Headers)
+ request.Headers.TryAddWithoutValidation(Header.Name, Header.Value);
+ return await _httpClient.SendAsync(request);
+ }
+ ///
+ /// Triggers a GET request with headers and authorization to the specified route and retrieves the result as an HTTP Response Message.
+ ///
+ ///
+ ///
+ ///
+ /// An HTTP Reponse Message
+ public static async Task GetAsync(string Route, List Headers, Authorization Authorization)
+ {
+ using var request = new HttpRequestMessage(HttpMethod.Get, new Uri(Route));
+ foreach (var Header in Headers)
+ request.Headers.TryAddWithoutValidation(Header.Name, Header.Value);
+ request.Headers.Authorization = string.IsNullOrEmpty(Authorization.Parameter)
+ ? new AuthenticationHeaderValue(Authorization.Scheme)
+ : new AuthenticationHeaderValue(Authorization.Scheme, Authorization.Parameter);
+ return await _httpClient.SendAsync(request);
+ }
+
+ ///
+ /// Triggers a GET request to the specified route and retrieves the result as a string which you can serialize.
+ ///
+ ///
+ /// a string format of the HTTP Response message
+ public static async Task GetAsStringAsync(string Route)
+ {
+ var httpResponse = await _httpClient.GetAsync(new Uri(Route));
+ return await httpResponse.Content.ReadAsStringAsync();
+ }
+ ///
+ /// Triggers a GET request to the specified route with a Bearer Token and retrieves the result as an HTTP Response Message.
+ ///
+ ///
+ ///
+ /// a string format of the HTTP Response message
+ public static async Task GetAsStringAsync(string Route, string Token = null)
+ {
+ using var request = new HttpRequestMessage(HttpMethod.Get, new Uri(Route));
+ if (!string.IsNullOrEmpty(Token))
+ request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Token);
+ var httpResponse = await _httpClient.SendAsync(request);
+ return await httpResponse.Content.ReadAsStringAsync();
+ }
+ ///
+ /// Triggers a GET request with headers to the specified route and retrieves the result as an HTTP Response Message.
+ ///
+ ///
+ ///
+ /// a string format of the HTTP Response message
+ public static async Task GetAsStringAsync(string Route, List Headers)
+ {
+ using var request = new HttpRequestMessage(HttpMethod.Get, new Uri(Route));
+ foreach (var Header in Headers)
+ request.Headers.TryAddWithoutValidation(Header.Name, Header.Value);
+ var httpResponse = await _httpClient.SendAsync(request);
+ return await httpResponse.Content.ReadAsStringAsync();
+ }
+ ///
+ /// Triggers a GET request with headers and authorization to the specified route and retrieves the result as an HTTP Response Message.
+ ///
+ ///
+ ///
+ ///
+ /// a string format of the HTTP Response message
+ public static async Task GetAsStringAsync(string Route, List Headers, Authorization Authorization)
+ {
+ using var request = new HttpRequestMessage(HttpMethod.Get, new Uri(Route));
+ foreach (var Header in Headers)
+ request.Headers.TryAddWithoutValidation(Header.Name, Header.Value);
+ request.Headers.Authorization = string.IsNullOrEmpty(Authorization.Parameter)
+ ? new AuthenticationHeaderValue(Authorization.Scheme)
+ : new AuthenticationHeaderValue(Authorization.Scheme, Authorization.Parameter);
+ var httpResponse = await _httpClient.SendAsync(request);
+ return await httpResponse.Content.ReadAsStringAsync();
+ }
+ }
+}
diff --git a/src/HttpClientServiceHelper/Patch.cs b/src/HttpClientServiceHelper/Patch.cs
index b9ad35e..13e678e 100644
--- a/src/HttpClientServiceHelper/Patch.cs
+++ b/src/HttpClientServiceHelper/Patch.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
@@ -19,14 +19,9 @@ public partial class HttpClientHelper
/// an HTTP Response Message
public static async Task PatchAsync(string Route, object Model)
{
- using (HttpClient httpClient = new HttpClient())
- {
- var uri = new Uri(Route);
- string jsonTransport = JsonConvert.SerializeObject(Model);
- var jsonPayload = new StringContent(jsonTransport, Encoding.UTF8, "application/json");
- var httpResponse = await httpClient.PatchAsync(uri, jsonPayload);
- return httpResponse;
- }
+ using var request = new HttpRequestMessage(HttpMethod.Patch, new Uri(Route));
+ request.Content = new StringContent(JsonConvert.SerializeObject(Model), Encoding.UTF8, "application/json");
+ return await _httpClient.SendAsync(request);
}
///
/// Triggers a Patch request to the specified route and retrieves the result as an HTTP Response Message
@@ -37,15 +32,11 @@ public static async Task PatchAsync(string Route, object Mo
/// an HTTP Response Message
public static async Task PatchAsync(string Route, object Model, string Token = null)
{
- using (HttpClient httpClient = new HttpClient())
- {
- var uri = new Uri(Route);
- httpClient.DefaultRequestHeaders.Authorization = !string.IsNullOrEmpty(Token) ? new AuthenticationHeaderValue("Bearer", Token) : null;
- string jsonTransport = JsonConvert.SerializeObject(Model);
- var jsonPayload = new StringContent(jsonTransport, Encoding.UTF8, "application/json");
- var httpResponse = await httpClient.PatchAsync(uri, jsonPayload);
- return httpResponse;
- }
+ using var request = new HttpRequestMessage(HttpMethod.Patch, new Uri(Route));
+ if (!string.IsNullOrEmpty(Token))
+ request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Token);
+ request.Content = new StringContent(JsonConvert.SerializeObject(Model), Encoding.UTF8, "application/json");
+ return await _httpClient.SendAsync(request);
}
///
/// Triggers a Patch request to the specified route with headers and retrieves the result as an HTTP Response Message
@@ -56,18 +47,11 @@ public static async Task PatchAsync(string Route, object Mo
/// an HTTP Response Message
public static async Task PatchAsync(string Route, object Model, List Headers)
{
- using (HttpClient httpClient = new HttpClient())
- {
- var uri = new Uri(Route);
- foreach (var Header in Headers)
- {
- httpClient.DefaultRequestHeaders.Add(Header.Name, Header.Value);
- }
- string jsonTransport = JsonConvert.SerializeObject(Model);
- var jsonPayload = new StringContent(jsonTransport, Encoding.UTF8, "application/json");
- var httpResponse = await httpClient.PatchAsync(uri, jsonPayload);
- return httpResponse;
- }
+ using var request = new HttpRequestMessage(HttpMethod.Patch, new Uri(Route));
+ foreach (var Header in Headers)
+ request.Headers.TryAddWithoutValidation(Header.Name, Header.Value);
+ request.Content = new StringContent(JsonConvert.SerializeObject(Model), Encoding.UTF8, "application/json");
+ return await _httpClient.SendAsync(request);
}
///
/// Triggers a Patch request to the specified route with headers and authorization and retrieves the result as an HTTP Response Message
@@ -79,20 +63,14 @@ public static async Task PatchAsync(string Route, object Mo
/// an HTTP Response Message
public static async Task PatchAsync(string Route, object Model, List Headers, Authorization Authorization)
{
- using (HttpClient httpClient = new HttpClient())
- {
- var uri = new Uri(Route);
- foreach (var Header in Headers)
- {
- httpClient.DefaultRequestHeaders.Add(Header.Name, Header.Value);
- }
- httpClient.DefaultRequestHeaders.Authorization = string.IsNullOrEmpty(Authorization.Parameter) ?
- new AuthenticationHeaderValue(Authorization.Scheme) : new AuthenticationHeaderValue(Authorization.Scheme, Authorization.Parameter);
- string jsonTransport = JsonConvert.SerializeObject(Model);
- var jsonPayload = new StringContent(jsonTransport, Encoding.UTF8, "application/json");
- var httpResponse = await httpClient.PatchAsync(uri, jsonPayload);
- return httpResponse;
- }
+ using var request = new HttpRequestMessage(HttpMethod.Patch, new Uri(Route));
+ foreach (var Header in Headers)
+ request.Headers.TryAddWithoutValidation(Header.Name, Header.Value);
+ request.Headers.Authorization = string.IsNullOrEmpty(Authorization.Parameter)
+ ? new AuthenticationHeaderValue(Authorization.Scheme)
+ : new AuthenticationHeaderValue(Authorization.Scheme, Authorization.Parameter);
+ request.Content = new StringContent(JsonConvert.SerializeObject(Model), Encoding.UTF8, "application/json");
+ return await _httpClient.SendAsync(request);
}
///
@@ -103,14 +81,10 @@ public static async Task PatchAsync(string Route, object Mo
/// an HTTP Response Message as string
public static async Task PatchAndGetResponseAsStringAsync(string Route, object Model)
{
- using (HttpClient httpClient = new HttpClient())
- {
- var uri = new Uri(Route);
- string jsonTransport = JsonConvert.SerializeObject(Model);
- var jsonPayload = new StringContent(jsonTransport, Encoding.UTF8, "application/json");
- var httpResponse = await httpClient.PatchAsync(uri, jsonPayload);
- return await httpResponse.Content.ReadAsStringAsync();
- }
+ using var request = new HttpRequestMessage(HttpMethod.Patch, new Uri(Route));
+ request.Content = new StringContent(JsonConvert.SerializeObject(Model), Encoding.UTF8, "application/json");
+ var httpResponse = await _httpClient.SendAsync(request);
+ return await httpResponse.Content.ReadAsStringAsync();
}
///
/// Triggers a Patch request to the specified route and retrieves the result as an HTTP Response Message.
@@ -121,15 +95,12 @@ public static async Task PatchAndGetResponseAsStringAsync(string Route,
/// an HTTP Response Message as string
public static async Task PatchAndGetResponseAsStringAsync(string Route, object Model, string Token = null)
{
- using (HttpClient httpClient = new HttpClient())
- {
- var uri = new Uri(Route);
- httpClient.DefaultRequestHeaders.Authorization = !string.IsNullOrEmpty(Token) ? new AuthenticationHeaderValue("Bearer", Token) : null;
- string jsonTransport = JsonConvert.SerializeObject(Model);
- var jsonPayload = new StringContent(jsonTransport, Encoding.UTF8, "application/json");
- var httpResponse = await httpClient.PatchAsync(uri, jsonPayload);
- return await httpResponse.Content.ReadAsStringAsync();
- }
+ using var request = new HttpRequestMessage(HttpMethod.Patch, new Uri(Route));
+ if (!string.IsNullOrEmpty(Token))
+ request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Token);
+ request.Content = new StringContent(JsonConvert.SerializeObject(Model), Encoding.UTF8, "application/json");
+ var httpResponse = await _httpClient.SendAsync(request);
+ return await httpResponse.Content.ReadAsStringAsync();
}
///
/// Triggers a Patch request to the specified route with headers and retrieves the result as an HTTP Response Message
@@ -140,18 +111,12 @@ public static async Task PatchAndGetResponseAsStringAsync(string Route,
/// an HTTP Response Message as string
public static async Task PatchAndGetResponseAsStringAsync(string Route, object Model, List Headers)
{
- using (HttpClient httpClient = new HttpClient())
- {
- var uri = new Uri(Route);
- foreach (var Header in Headers)
- {
- httpClient.DefaultRequestHeaders.Add(Header.Name, Header.Value);
- }
- string jsonTransport = JsonConvert.SerializeObject(Model);
- var jsonPayload = new StringContent(jsonTransport, Encoding.UTF8, "application/json");
- var httpResponse = await httpClient.PatchAsync(uri, jsonPayload);
- return await httpResponse.Content.ReadAsStringAsync();
- }
+ using var request = new HttpRequestMessage(HttpMethod.Patch, new Uri(Route));
+ foreach (var Header in Headers)
+ request.Headers.TryAddWithoutValidation(Header.Name, Header.Value);
+ request.Content = new StringContent(JsonConvert.SerializeObject(Model), Encoding.UTF8, "application/json");
+ var httpResponse = await _httpClient.SendAsync(request);
+ return await httpResponse.Content.ReadAsStringAsync();
}
///
/// Triggers a Patch request to the specified route with headers and authorization and retrieves the result as an HTTP Response Message
@@ -163,20 +128,15 @@ public static async Task PatchAndGetResponseAsStringAsync(string Route,
/// an HTTP Response Message as string
public static async Task PatchAndGetResponseAsStringAsync(string Route, object Model, List Headers, Authorization Authorization)
{
- using (HttpClient httpClient = new HttpClient())
- {
- var uri = new Uri(Route);
- foreach (var Header in Headers)
- {
- httpClient.DefaultRequestHeaders.Add(Header.Name, Header.Value);
- }
- httpClient.DefaultRequestHeaders.Authorization = string.IsNullOrEmpty(Authorization.Parameter) ?
- new AuthenticationHeaderValue(Authorization.Scheme) : new AuthenticationHeaderValue(Authorization.Scheme, Authorization.Parameter);
- string jsonTransport = JsonConvert.SerializeObject(Model);
- var jsonPayload = new StringContent(jsonTransport, Encoding.UTF8, "application/json");
- var httpResponse = await httpClient.PatchAsync(uri, jsonPayload);
- return await httpResponse.Content.ReadAsStringAsync();
- }
+ using var request = new HttpRequestMessage(HttpMethod.Patch, new Uri(Route));
+ foreach (var Header in Headers)
+ request.Headers.TryAddWithoutValidation(Header.Name, Header.Value);
+ request.Headers.Authorization = string.IsNullOrEmpty(Authorization.Parameter)
+ ? new AuthenticationHeaderValue(Authorization.Scheme)
+ : new AuthenticationHeaderValue(Authorization.Scheme, Authorization.Parameter);
+ request.Content = new StringContent(JsonConvert.SerializeObject(Model), Encoding.UTF8, "application/json");
+ var httpResponse = await _httpClient.SendAsync(request);
+ return await httpResponse.Content.ReadAsStringAsync();
}
#region TODO
///
diff --git a/src/HttpClientServiceHelper/Post.cs b/src/HttpClientServiceHelper/Post.cs
index 6e8b114..809e770 100644
--- a/src/HttpClientServiceHelper/Post.cs
+++ b/src/HttpClientServiceHelper/Post.cs
@@ -1,4 +1,4 @@
-using HttpClientServiceHelper.Models;
+using HttpClientServiceHelper.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
@@ -19,14 +19,9 @@ public partial class HttpClientHelper
/// an HTTP Response Message
public static async Task PostAsync(string Route, object Model)
{
- using (HttpClient httpClient = new HttpClient())
- {
- var uri = new Uri(Route);
- string jsonTransport = JsonConvert.SerializeObject(Model);
- var jsonPayload = new StringContent(jsonTransport, Encoding.UTF8, "application/json");
- var httpResponse = await httpClient.PostAsync(uri, jsonPayload);
- return httpResponse;
- }
+ using var request = new HttpRequestMessage(HttpMethod.Post, new Uri(Route));
+ request.Content = new StringContent(JsonConvert.SerializeObject(Model), Encoding.UTF8, "application/json");
+ return await _httpClient.SendAsync(request);
}
///
/// Triggers a Post request to the specified route and retrieves the result as an HTTP Response Message
@@ -37,15 +32,11 @@ public static async Task PostAsync(string Route, object Mod
/// an HTTP Response Message
public static async Task PostAsync(string Route, object Model, string Token = null)
{
- using (HttpClient httpClient = new HttpClient())
- {
- var uri = new Uri(Route);
- httpClient.DefaultRequestHeaders.Authorization = !string.IsNullOrEmpty(Token) ? new AuthenticationHeaderValue("Bearer", Token) : null;
- string jsonTransport = JsonConvert.SerializeObject(Model);
- var jsonPayload = new StringContent(jsonTransport, Encoding.UTF8, "application/json");
- var httpResponse = await httpClient.PostAsync(uri, jsonPayload);
- return httpResponse;
- }
+ using var request = new HttpRequestMessage(HttpMethod.Post, new Uri(Route));
+ if (!string.IsNullOrEmpty(Token))
+ request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Token);
+ request.Content = new StringContent(JsonConvert.SerializeObject(Model), Encoding.UTF8, "application/json");
+ return await _httpClient.SendAsync(request);
}
///
/// Triggers a Post request to the specified route with headers and retrieves the result as an HTTP Response Message
@@ -56,18 +47,11 @@ public static async Task PostAsync(string Route, object Mod
/// an HTTP Response Message
public static async Task PostAsync(string Route, object Model, List Headers)
{
- using (HttpClient httpClient = new HttpClient())
- {
- var uri = new Uri(Route);
- foreach (var Header in Headers)
- {
- httpClient.DefaultRequestHeaders.Add(Header.Name, Header.Value);
- }
- string jsonTransport = JsonConvert.SerializeObject(Model);
- var jsonPayload = new StringContent(jsonTransport, Encoding.UTF8, "application/json");
- var httpResponse = await httpClient.PostAsync(uri, jsonPayload);
- return httpResponse;
- }
+ using var request = new HttpRequestMessage(HttpMethod.Post, new Uri(Route));
+ foreach (var Header in Headers)
+ request.Headers.TryAddWithoutValidation(Header.Name, Header.Value);
+ request.Content = new StringContent(JsonConvert.SerializeObject(Model), Encoding.UTF8, "application/json");
+ return await _httpClient.SendAsync(request);
}
///
/// Triggers a Post request to the specified route with headers and authorization and retrieves the result as an HTTP Response Message
@@ -79,20 +63,14 @@ public static async Task PostAsync(string Route, object Mod
/// an HTTP Response Message
public static async Task PostAsync(string Route, object Model, List Headers, Authorization Authorization)
{
- using (HttpClient httpClient = new HttpClient())
- {
- var uri = new Uri(Route);
- foreach (var Header in Headers)
- {
- httpClient.DefaultRequestHeaders.Add(Header.Name, Header.Value);
- }
- httpClient.DefaultRequestHeaders.Authorization = string.IsNullOrEmpty(Authorization.Parameter) ?
- new AuthenticationHeaderValue(Authorization.Scheme) : new AuthenticationHeaderValue(Authorization.Scheme, Authorization.Parameter);
- string jsonTransport = JsonConvert.SerializeObject(Model);
- var jsonPayload = new StringContent(jsonTransport, Encoding.UTF8, "application/json");
- var httpResponse = await httpClient.PostAsync(uri, jsonPayload);
- return httpResponse;
- }
+ using var request = new HttpRequestMessage(HttpMethod.Post, new Uri(Route));
+ foreach (var Header in Headers)
+ request.Headers.TryAddWithoutValidation(Header.Name, Header.Value);
+ request.Headers.Authorization = string.IsNullOrEmpty(Authorization.Parameter)
+ ? new AuthenticationHeaderValue(Authorization.Scheme)
+ : new AuthenticationHeaderValue(Authorization.Scheme, Authorization.Parameter);
+ request.Content = new StringContent(JsonConvert.SerializeObject(Model), Encoding.UTF8, "application/json");
+ return await _httpClient.SendAsync(request);
}
///
@@ -103,14 +81,10 @@ public static async Task PostAsync(string Route, object Mod
/// an HTTP Response Message as string
public static async Task PostAndGetResponseAsStringAsync(string Route, object Model)
{
- using (HttpClient httpClient = new HttpClient())
- {
- var uri = new Uri(Route);
- string jsonTransport = JsonConvert.SerializeObject(Model);
- var jsonPayload = new StringContent(jsonTransport, Encoding.UTF8, "application/json");
- var httpResponse = await httpClient.PostAsync(uri, jsonPayload);
- return await httpResponse.Content.ReadAsStringAsync();
- }
+ using var request = new HttpRequestMessage(HttpMethod.Post, new Uri(Route));
+ request.Content = new StringContent(JsonConvert.SerializeObject(Model), Encoding.UTF8, "application/json");
+ var httpResponse = await _httpClient.SendAsync(request);
+ return await httpResponse.Content.ReadAsStringAsync();
}
///
/// Triggers a Post request to the specified route and retrieves the result as an HTTP Response Message.
@@ -121,15 +95,12 @@ public static async Task PostAndGetResponseAsStringAsync(string Route, o
/// an HTTP Response Message as string
public static async Task PostAndGetResponseAsStringAsync(string Route, object Model, string Token = null)
{
- using (HttpClient httpClient = new HttpClient())
- {
- var uri = new Uri(Route);
- httpClient.DefaultRequestHeaders.Authorization = !string.IsNullOrEmpty(Token) ? new AuthenticationHeaderValue("Bearer", Token) : null;
- string jsonTransport = JsonConvert.SerializeObject(Model);
- var jsonPayload = new StringContent(jsonTransport, Encoding.UTF8, "application/json");
- var httpResponse = await httpClient.PostAsync(uri, jsonPayload);
- return await httpResponse.Content.ReadAsStringAsync();
- }
+ using var request = new HttpRequestMessage(HttpMethod.Post, new Uri(Route));
+ if (!string.IsNullOrEmpty(Token))
+ request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Token);
+ request.Content = new StringContent(JsonConvert.SerializeObject(Model), Encoding.UTF8, "application/json");
+ var httpResponse = await _httpClient.SendAsync(request);
+ return await httpResponse.Content.ReadAsStringAsync();
}
///
/// Triggers a Post request to the specified route with headers and retrieves the result as an HTTP Response Message
@@ -140,18 +111,12 @@ public static async Task PostAndGetResponseAsStringAsync(string Route, o
/// an HTTP Response Message as string
public static async Task PostAndGetResponseAsStringAsync(string Route, object Model, List Headers)
{
- using (HttpClient httpClient = new HttpClient())
- {
- var uri = new Uri(Route);
- foreach (var Header in Headers)
- {
- httpClient.DefaultRequestHeaders.Add(Header.Name, Header.Value);
- }
- string jsonTransport = JsonConvert.SerializeObject(Model);
- var jsonPayload = new StringContent(jsonTransport, Encoding.UTF8, "application/json");
- var httpResponse = await httpClient.PostAsync(uri, jsonPayload);
- return await httpResponse.Content.ReadAsStringAsync();
- }
+ using var request = new HttpRequestMessage(HttpMethod.Post, new Uri(Route));
+ foreach (var Header in Headers)
+ request.Headers.TryAddWithoutValidation(Header.Name, Header.Value);
+ request.Content = new StringContent(JsonConvert.SerializeObject(Model), Encoding.UTF8, "application/json");
+ var httpResponse = await _httpClient.SendAsync(request);
+ return await httpResponse.Content.ReadAsStringAsync();
}
///
/// Triggers a Post request to the specified route with headers and authorization and retrieves the result as an HTTP Response Message
@@ -163,20 +128,15 @@ public static async Task PostAndGetResponseAsStringAsync(string Route, o
/// an HTTP Response Message as string
public static async Task PostAndGetResponseAsStringAsync(string Route, object Model, List Headers, Authorization Authorization)
{
- using (HttpClient httpClient = new HttpClient())
- {
- var uri = new Uri(Route);
- foreach (var Header in Headers)
- {
- httpClient.DefaultRequestHeaders.Add(Header.Name, Header.Value);
- }
- httpClient.DefaultRequestHeaders.Authorization = string.IsNullOrEmpty(Authorization.Parameter) ?
- new AuthenticationHeaderValue(Authorization.Scheme) : new AuthenticationHeaderValue(Authorization.Scheme, Authorization.Parameter);
- string jsonTransport = JsonConvert.SerializeObject(Model);
- var jsonPayload = new StringContent(jsonTransport, Encoding.UTF8, "application/json");
- var httpResponse = await httpClient.PostAsync(uri, jsonPayload);
- return await httpResponse.Content.ReadAsStringAsync();
- }
+ using var request = new HttpRequestMessage(HttpMethod.Post, new Uri(Route));
+ foreach (var Header in Headers)
+ request.Headers.TryAddWithoutValidation(Header.Name, Header.Value);
+ request.Headers.Authorization = string.IsNullOrEmpty(Authorization.Parameter)
+ ? new AuthenticationHeaderValue(Authorization.Scheme)
+ : new AuthenticationHeaderValue(Authorization.Scheme, Authorization.Parameter);
+ request.Content = new StringContent(JsonConvert.SerializeObject(Model), Encoding.UTF8, "application/json");
+ var httpResponse = await _httpClient.SendAsync(request);
+ return await httpResponse.Content.ReadAsStringAsync();
}
#region TODO
///
diff --git a/src/HttpClientServiceHelper/Put.cs b/src/HttpClientServiceHelper/Put.cs
index 2f10f2f..0f3d042 100644
--- a/src/HttpClientServiceHelper/Put.cs
+++ b/src/HttpClientServiceHelper/Put.cs
@@ -1,4 +1,4 @@
-using HttpClientServiceHelper.Models;
+using HttpClientServiceHelper.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
@@ -19,14 +19,9 @@ public partial class HttpClientHelper
/// an HTTP Response Message
public static async Task PutAsync(string Route, object Model)
{
- using (HttpClient httpClient = new HttpClient())
- {
- var uri = new Uri(Route);
- string jsonTransport = JsonConvert.SerializeObject(Model);
- var jsonPayload = new StringContent(jsonTransport, Encoding.UTF8, "application/json");
- var httpResponse = await httpClient.PutAsync(uri, jsonPayload);
- return httpResponse;
- }
+ using var request = new HttpRequestMessage(HttpMethod.Put, new Uri(Route));
+ request.Content = new StringContent(JsonConvert.SerializeObject(Model), Encoding.UTF8, "application/json");
+ return await _httpClient.SendAsync(request);
}
///
/// Triggers a PUT request to the specified route and retrieves the result as an HTTP Response Message
@@ -37,15 +32,11 @@ public static async Task PutAsync(string Route, object Mode
/// an HTTP Response Message
public static async Task PutAsync(string Route, object Model, string Token = null)
{
- using (HttpClient httpClient = new HttpClient())
- {
- var uri = new Uri(Route);
- httpClient.DefaultRequestHeaders.Authorization = !string.IsNullOrEmpty(Token) ? new AuthenticationHeaderValue("Bearer", Token) : null;
- string jsonTransport = JsonConvert.SerializeObject(Model);
- var jsonPayload = new StringContent(jsonTransport, Encoding.UTF8, "application/json");
- var httpResponse = await httpClient.PutAsync(uri, jsonPayload);
- return httpResponse;
- }
+ using var request = new HttpRequestMessage(HttpMethod.Put, new Uri(Route));
+ if (!string.IsNullOrEmpty(Token))
+ request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Token);
+ request.Content = new StringContent(JsonConvert.SerializeObject(Model), Encoding.UTF8, "application/json");
+ return await _httpClient.SendAsync(request);
}
///
/// Triggers a PUT request to the specified route with headers and retrieves the result as an HTTP Response Message
@@ -56,18 +47,11 @@ public static async Task PutAsync(string Route, object Mode
/// an HTTP Response Message
public static async Task PutAsync(string Route, object Model, List Headers)
{
- using (HttpClient httpClient = new HttpClient())
- {
- var uri = new Uri(Route);
- foreach (var Header in Headers)
- {
- httpClient.DefaultRequestHeaders.Add(Header.Name, Header.Value);
- }
- string jsonTransport = JsonConvert.SerializeObject(Model);
- var jsonPayload = new StringContent(jsonTransport, Encoding.UTF8, "application/json");
- var httpResponse = await httpClient.PutAsync(uri, jsonPayload);
- return httpResponse;
- }
+ using var request = new HttpRequestMessage(HttpMethod.Put, new Uri(Route));
+ foreach (var Header in Headers)
+ request.Headers.TryAddWithoutValidation(Header.Name, Header.Value);
+ request.Content = new StringContent(JsonConvert.SerializeObject(Model), Encoding.UTF8, "application/json");
+ return await _httpClient.SendAsync(request);
}
///
/// Triggers a PUT request to the specified route with headers and authorization and retrieves the result as an HTTP Response Message
@@ -79,20 +63,14 @@ public static async Task PutAsync(string Route, object Mode
/// an HTTP Response Message
public static async Task PutAsync(string Route, object Model, List Headers, Authorization Authorization)
{
- using (HttpClient httpClient = new HttpClient())
- {
- var uri = new Uri(Route);
- foreach (var Header in Headers)
- {
- httpClient.DefaultRequestHeaders.Add(Header.Name, Header.Value);
- }
- httpClient.DefaultRequestHeaders.Authorization = string.IsNullOrEmpty(Authorization.Parameter) ?
- new AuthenticationHeaderValue(Authorization.Scheme) : new AuthenticationHeaderValue(Authorization.Scheme, Authorization.Parameter);
- string jsonTransport = JsonConvert.SerializeObject(Model);
- var jsonPayload = new StringContent(jsonTransport, Encoding.UTF8, "application/json");
- var httpResponse = await httpClient.PutAsync(uri, jsonPayload);
- return httpResponse;
- }
+ using var request = new HttpRequestMessage(HttpMethod.Put, new Uri(Route));
+ foreach (var Header in Headers)
+ request.Headers.TryAddWithoutValidation(Header.Name, Header.Value);
+ request.Headers.Authorization = string.IsNullOrEmpty(Authorization.Parameter)
+ ? new AuthenticationHeaderValue(Authorization.Scheme)
+ : new AuthenticationHeaderValue(Authorization.Scheme, Authorization.Parameter);
+ request.Content = new StringContent(JsonConvert.SerializeObject(Model), Encoding.UTF8, "application/json");
+ return await _httpClient.SendAsync(request);
}
///
@@ -103,14 +81,10 @@ public static async Task PutAsync(string Route, object Mode
/// an HTTP Response Message as string
public static async Task PutAndGetResponseAsStringAsync(string Route, object Model)
{
- using (HttpClient httpClient = new HttpClient())
- {
- var uri = new Uri(Route);
- string jsonTransport = JsonConvert.SerializeObject(Model);
- var jsonPayload = new StringContent(jsonTransport, Encoding.UTF8, "application/json");
- var httpResponse = await httpClient.PutAsync(uri, jsonPayload);
- return await httpResponse.Content.ReadAsStringAsync();
- }
+ using var request = new HttpRequestMessage(HttpMethod.Put, new Uri(Route));
+ request.Content = new StringContent(JsonConvert.SerializeObject(Model), Encoding.UTF8, "application/json");
+ var httpResponse = await _httpClient.SendAsync(request);
+ return await httpResponse.Content.ReadAsStringAsync();
}
///
/// Triggers a PUT request to the specified route and retrieves the result as an HTTP Response Message.
@@ -121,15 +95,12 @@ public static async Task PutAndGetResponseAsStringAsync(string Route, ob
/// an HTTP Response Message as string
public static async Task PutAndGetResponseAsStringAsync(string Route, object Model, string Token = null)
{
- using (HttpClient httpClient = new HttpClient())
- {
- var uri = new Uri(Route);
- httpClient.DefaultRequestHeaders.Authorization = !string.IsNullOrEmpty(Token) ? new AuthenticationHeaderValue("Bearer", Token) : null;
- string jsonTransport = JsonConvert.SerializeObject(Model);
- var jsonPayload = new StringContent(jsonTransport, Encoding.UTF8, "application/json");
- var httpResponse = await httpClient.PutAsync(uri, jsonPayload);
- return await httpResponse.Content.ReadAsStringAsync();
- }
+ using var request = new HttpRequestMessage(HttpMethod.Put, new Uri(Route));
+ if (!string.IsNullOrEmpty(Token))
+ request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Token);
+ request.Content = new StringContent(JsonConvert.SerializeObject(Model), Encoding.UTF8, "application/json");
+ var httpResponse = await _httpClient.SendAsync(request);
+ return await httpResponse.Content.ReadAsStringAsync();
}
///
/// Triggers a PUT request to the specified route with headers and retrieves the result as an HTTP Response Message
@@ -140,18 +111,12 @@ public static async Task PutAndGetResponseAsStringAsync(string Route, ob
/// an HTTP Response Message as string
public static async Task PutAndGetResponseAsStringAsync(string Route, object Model, List Headers)
{
- using (HttpClient httpClient = new HttpClient())
- {
- var uri = new Uri(Route);
- foreach (var Header in Headers)
- {
- httpClient.DefaultRequestHeaders.Add(Header.Name, Header.Value);
- }
- string jsonTransport = JsonConvert.SerializeObject(Model);
- var jsonPayload = new StringContent(jsonTransport, Encoding.UTF8, "application/json");
- var httpResponse = await httpClient.PutAsync(uri, jsonPayload);
- return await httpResponse.Content.ReadAsStringAsync();
- }
+ using var request = new HttpRequestMessage(HttpMethod.Put, new Uri(Route));
+ foreach (var Header in Headers)
+ request.Headers.TryAddWithoutValidation(Header.Name, Header.Value);
+ request.Content = new StringContent(JsonConvert.SerializeObject(Model), Encoding.UTF8, "application/json");
+ var httpResponse = await _httpClient.SendAsync(request);
+ return await httpResponse.Content.ReadAsStringAsync();
}
///
/// Triggers a PUT request to the specified route with headers and authorization and retrieves the result as an HTTP Response Message
@@ -163,20 +128,15 @@ public static async Task PutAndGetResponseAsStringAsync(string Route, ob
/// an HTTP Response Message as string
public static async Task PutAndGetResponseAsStringAsync(string Route, object Model, List Headers, Authorization Authorization)
{
- using (HttpClient httpClient = new HttpClient())
- {
- var uri = new Uri(Route);
- foreach (var Header in Headers)
- {
- httpClient.DefaultRequestHeaders.Add(Header.Name, Header.Value);
- }
- httpClient.DefaultRequestHeaders.Authorization = string.IsNullOrEmpty(Authorization.Parameter) ?
- new AuthenticationHeaderValue(Authorization.Scheme) : new AuthenticationHeaderValue(Authorization.Scheme, Authorization.Parameter);
- string jsonTransport = JsonConvert.SerializeObject(Model);
- var jsonPayload = new StringContent(jsonTransport, Encoding.UTF8, "application/json");
- var httpResponse = await httpClient.PutAsync(uri, jsonPayload);
- return await httpResponse.Content.ReadAsStringAsync();
- }
+ using var request = new HttpRequestMessage(HttpMethod.Put, new Uri(Route));
+ foreach (var Header in Headers)
+ request.Headers.TryAddWithoutValidation(Header.Name, Header.Value);
+ request.Headers.Authorization = string.IsNullOrEmpty(Authorization.Parameter)
+ ? new AuthenticationHeaderValue(Authorization.Scheme)
+ : new AuthenticationHeaderValue(Authorization.Scheme, Authorization.Parameter);
+ request.Content = new StringContent(JsonConvert.SerializeObject(Model), Encoding.UTF8, "application/json");
+ var httpResponse = await _httpClient.SendAsync(request);
+ return await httpResponse.Content.ReadAsStringAsync();
}
#region TODO
///