IHttpClientService
Wisej.AI.Services.IHttpClientService
Namespace: Wisej.AI.Services
Assembly: Wisej.AI (3.5.0.0)
Represents a service for handling HTTP client operations with customizable handlers and headers.
public interface IHttpClientService
The IHttpClientService interface provides methods for sending HTTP requests and handling responses. It allows setting a custom HttpClientHandler, adding and removing default headers, and executing various HTTP methods like GET and POST. This interface is intended to be implemented by classes that encapsulate HTTP client logic, enabling easy configuration and request execution.
Properties
HttpClient

HttpClient: Returns the underlying HttpClient instance.
Timeout

TimeSpan: Gets or sets the timeout.
Methods
GetAsync(uri)

Sends an asynchronous GET request to the specified URI.
uri
The URI to send the request to. This parameter is optional and defaults to an empty string if not provided.
Returns: Task<HttpResponseMessage>. A task representing the asynchronous operation, containing the HTTP response message.
The GetAsync method sends an HTTP GET request to the specified URI and returns the response. This is typically used for retrieving data from a web service. Example usage:
IHttpClientService clientService = new HttpClientService();
HttpResponseMessage response = await clientService.GetAsync("https://api.example.com/resource");
Throws:
ArgumentNullException Thrown when uri is null.
PostAsync(uri, content)

Sends an asynchronous POST request to the specified URI with the given content.
uri
The URI to send the request to. This parameter is optional and defaults to an empty string if not provided.
content
The HTTP content to send with the request. This parameter is optional and defaults to null if not provided.
Returns: Task<HttpResponseMessage>. A task representing the asynchronous operation, containing the HTTP response message.
The PostAsync method sends an HTTP POST request with the specified content to the given URI. It is commonly used to submit data to a web service. Example usage:
IHttpClientService clientService = new HttpClientService();
HttpContent content = new StringContent("{ \"name\": \"value\" }", Encoding.UTF8, "application/json");
HttpResponseMessage response = await clientService.PostAsync("https://api.example.com/resource", content);
Throws:
ArgumentNullException Thrown when uri or content is null.
RemoveDefaultHeader(key)

Removes a default header from the requests.
key
The name of the header to remove. This parameter is optional and defaults to an empty string if not provided.
The RemoveDefaultHeader method removes a previously added header, preventing it from being included in future requests. It is useful for managing dynamic header requirements. Example usage:
IHttpClientService clientService = new HttpClientService();
clientService.RemoveDefaultHeader("Authorization");
SendAsync(request)

Sends an asynchronous HTTP request to the specified URI.
Returns: Task<HttpResponseMessage>. A task representing the asynchronous operation, containing the HTTP response message.
The SendAsync method allows sending an HTTP request with custom configurations and headers. It provides flexibility for executing different HTTP methods like PUT, DELETE, etc. Example usage:
IHttpClientService clientService = new HttpClientService();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Put, "https://api.example.com/resource");
HttpResponseMessage response = await clientService.SendAsync(request);
Throws:
ArgumentNullException Thrown when request is null.
SetDefaultHeader(key, value)

Adds a default header to be included in all requests.
The SetDefaultHeader method allows adding a header that will be included in every HTTP request sent by the client service. This is useful for setting common headers like Authorization tokens or Content-Type. Example usage:
IHttpClientService clientService = new HttpClientService();
clientService.SetDefaultHeader("Authorization", "Bearer token");
SetHandler(handler)

Sets a custom HTTP client handler for the service.
handler
The HttpClientHandler to use for requests. This parameter is optional and defaults to null if not provided.
The SetHandler method allows you to set a custom handler for the HTTP client, which can be used to configure specific behaviors like proxy settings, cookies, or custom authentication mechanisms. Example usage:
IHttpClientService clientService = new HttpClientService();
HttpClientHandler handler = new HttpClientHandler
{
UseCookies = false
};
clientService.SetHandler(handler);
Implemented By
Represents a default HTTP client service that provides methods to send HTTP requests and manage default headers.
Last updated