NamespaceSystemLibrary.Common.Web SystemLibrary.Common.Web.dll
HttpBaseClient is a class for all http(s) requests in your project
- Note: Polly (nuget package) does a similar thing about Retry, and it does a few other things, but last time I checked; it does not reuse the TCP connection, so it is actually unusable)
Has:
a retry handler configured through constructor
if request fails and retry is True, the retry request uses a new tcp connection with 10 seconds timeout
a retry request occurs only for GET, HEAD or OPTION request methods, never for PUT/POST/DELETE
a timeout handler configurable through constructor, but also per method
each underlying tcp connection is cached for up to 2 minutes
Use HttpBaseClient directly or inherit from it, see the examples
Configurations: "systemLibraryCommonWeb": { "httpBaseClient": { "timeoutMilliseconds": 60000, "retryRequestTimeoutSeconds": 10, "cacheClientConnectionSeconds": 120 } }
HttpBaseClient
public class HttpBaseClient
Inheritance
Examples
A simple class to hold our Response
class HttpBinResponse
{
public string url { get; set; }
}
Our Client - you can new up HttpBaseClient directly if you like, but here we can reuse "apiUrl" (and other stuff, headers/what not) for all methods against the same client
class HttpBinClient : HttpBaseClient
{
const string apiUrl = "http://httpbin.org";
public HttpBinClient() : base(retryOnceOnRequestCancelled: true, defaultTimeoutMilliseconds: 5000, ignoreSslErrors: false)
{
}
public HttpBinResponse Get()
{
return base.Get<HttpBinResponse>(apiUrl + "/get").Data;
}
}
Running the above Client and Response in a UnitTest project as such:
[TestMethod]
public void Test()
{
var client = new HttpBinClient();
var response = client.Get();
Assert.IsTrue(response.url.Contains("http"));
//Visit: http://httpbin.org/get to see the actual value of 'url', then you know this Assert statement is true
}
Another example of using the HttpBaseClient directly:
public void Test()
{
var client = new HttpBaseClient();
var response = client.Get<string>("http://httpbin.org/get");
Assert.IsTrue(response.Contains("http"));
//Response is now the whole json text that the url: http://httpbin.org/get is returning
}
Arguments
Constructors
public HttpBaseClient(bool retryOnceOnRequestCancelled = false, bool ignoreSslErrors = true, int defaultTimeoutMilliseconds = 60000, bool throwOnUnsuccessfulStatusCode = true)
Methods arguments
Type | Name | Description |
---|---|---|
System.Boolean | retryOnceOnRequestCancelled | Retry with a fixed 10 seconds timeout upon a request was cancelled |
System.Boolean | ignoreSslErrors | Set to true if you want to ignore errors such as Ssl Cert Expired |
System.Int32 | defaultTimeoutMilliseconds | Default is 60 seconds |
System.Boolean | throwOnUnsuccessfulStatusCode | Set to true if you want unsuccessful status codes to throw exceptions |
Methods
Send a HTTP DELETE request
public ClientResponse<T> Delete<T>(string url, object data, MediaType mediaType = MediaType.json, int timeoutMilliseconds = 60000, IDictionary<string, string> headers = null, JsonSerializerOptions jsonSerializerOptions = null, CancellationToken cancellationToken = default(CancellationToken))
var deleteId = 1;
var response = new HttpBaseClient().Delete<string>("https://www.systemlibrary.com/delete", deleteId, MediaType.textplain, 2000);
Generic types
Name | Description |
---|---|
T |
Methods arguments
Type | Description |
---|---|
ClientResponse<T> |
Send a HTTP DELETE async request
public async Task<ClientResponse<T>> DeleteAsync<T>(string url, object data, MediaType mediaType = MediaType.json, int timeoutMilliseconds = 60000, IDictionary<string, string> headers = null, JsonSerializerOptions jsonSerializerOptions = null, CancellationToken cancellationToken = default(CancellationToken))
var deleteId = 1;
var response = await new HttpBaseClient().DeleteAsync<string>("https://www.systemlibrary.com/delete", deleteId, MediaType.textplain, 2000);
Generic types
Name | Description |
---|---|
T |
Methods arguments
Type | Description |
---|---|
System.Threading.Tasks.Task<ClientResponse<T>> |
Send a HTTP GET request
public ClientResponse<T> Get<T>(string url, MediaType mediaType = MediaType.json, int timeoutMilliseconds = 60000, IDictionary<string, string> headers = null, JsonSerializerOptions jsonSerializerOptions = null, CancellationToken cancellationToken = default(CancellationToken))
class Client : HttpBaseClient
{
public string Get()
{
return base.Get<string>("https://www.systemlibrary.com/", MediaType.json, 2000).Data;
}
}
var client = new Client();
var data = client.Get();
//'data' contains the response as string
A sample with a Class as return type:
public class Car
{
public string Name { get; set; }
}
class Client : HttpBaseClient
{
public string Get()
{
return base.Get<Car>("https://www.systemlibrary.com/", MediaType.json, 2000).Data;
}
}
var data = new Client().Get();
//'data' is now a Car (or null, if server responsed with nothing)
//Note: This assumes response is on json formatted text, so it can be de-serialized into a Car
//Note2: There's no de-serialization for XML responses
//Note3: There's 'de-serialization' for plain text responses if the response contans only one value type: bool, a datetime, an int. For instance a response '999' can be converted to <int>
Generic types
Name | Description |
---|---|
T |
Methods arguments
Type | Description |
---|---|
ClientResponse<T> |
Send a HTTP GET async request
public async Task<ClientResponse<T>> GetAsync<T>(string url, MediaType mediaType = MediaType.json, int timeoutMilliseconds = 60000, IDictionary<string, string> headers = null, JsonSerializerOptions jsonSerializerOptions = null, CancellationToken cancellationToken = default(CancellationToken))
var getId = 1;
var response = new await HttpBaseClient().GetAsync<string>("https://www.systemlibrary.com/get", getId, MediaType.json, 2000);
Generic types
Name | Description |
---|---|
T |
Methods arguments
Type | Description |
---|---|
System.Threading.Tasks.Task<ClientResponse<T>> |
Send a HTTP HEAD request
public ClientResponse<T> Head<T>(string url, MediaType mediaType = MediaType.json, int timeoutMilliseconds = 60000, IDictionary<string, string> headers = null, JsonSerializerOptions jsonSerializerOptions = null, CancellationToken cancellationToken = default(CancellationToken))
var response = new HttpBaseClient().Head<string>("https://www.systemlibrary.com/head", MediaType.json, 2000);
Generic types
Name | Description |
---|---|
T |
Methods arguments
Type | Description |
---|---|
ClientResponse<T> |
Send a HTTP HEAD async request
public async Task<ClientResponse<T>> HeadAsync<T>(string url, MediaType mediaType = MediaType.json, int timeoutMilliseconds = 60000, IDictionary<string, string> headers = null, JsonSerializerOptions jsonSerializerOptions = null, CancellationToken cancellationToken = default(CancellationToken))
var response = await new HttpBaseClient().HeadAsync<string>("https://www.systemlibrary.com/head", MediaType.json, 2000);
Generic types
Name | Description |
---|---|
T |
Methods arguments
Type | Description |
---|---|
System.Threading.Tasks.Task<ClientResponse<T>> |
Send a HTTP POST request
public ClientResponse<T> Post<T>(string url, object data = null, MediaType mediaType = MediaType.json, int timeoutMilliseconds = 60000, IDictionary<string, string> headers = null, JsonSerializerOptions jsonSerializerOptions = null, CancellationToken cancellationToken = default(CancellationToken))
var postId = 1;
var response = new HttpBaseClient().Post<string>("https://www.systemlibrary.com/post", postId, MediaType.textplain, 2000);
Generic types
Name | Description |
---|---|
T |
Methods arguments
Type | Description |
---|---|
ClientResponse<T> |
Send a HTTP POST async request
public async Task<ClientResponse<T>> PostAsync<T>(string url, object data, MediaType mediaType = MediaType.json, int timeoutMilliseconds = 60000, IDictionary<string, string> headers = null, JsonSerializerOptions jsonSerializerOptions = null, CancellationToken cancellationToken = default(CancellationToken))
var postId = 1;
var response = await new HttpBaseClient().PostAsync<string>("https://www.systemlibrary.com/post", postId, MediaType.textplain, 2000);
Generic types
Name | Description |
---|---|
T |
Methods arguments
Type | Description |
---|---|
System.Threading.Tasks.Task<ClientResponse<T>> |
Send a HTTP PUT request
public ClientResponse<T> Put<T>(string url, object data, MediaType mediaType = MediaType.json, int timeoutMilliseconds = 60000, IDictionary<string, string> headers = null, JsonSerializerOptions jsonSerializerOptions = null, CancellationToken cancellationToken = default(CancellationToken))
var putId = 1;
var response = new HttpBaseClient().Put<string>("https://www.systemlibrary.com/put", putId, MediaType.textplain, 2000);
Generic types
Name | Description |
---|---|
T |
Methods arguments
Type | Description |
---|---|
ClientResponse<T> |
Send a HTTP PUT async request
public async Task<ClientResponse<T>> PutAsync<T>(string url, object data, MediaType mediaType = MediaType.json, int timeoutMilliseconds = 60000, IDictionary<string, string> headers = null, JsonSerializerOptions jsonSerializerOptions = null, CancellationToken cancellationToken = default(CancellationToken))
var putId = 1;
var response = await new HttpBaseClient().PutAsync<string>("https://www.systemlibrary.com/put", putId, MediaType.textplain, 2000);
Generic types
Name | Description |
---|---|
T |
Methods arguments
Type | Description |
---|---|
System.Threading.Tasks.Task<ClientResponse<T>> |
}