DefaultHttpClientService
Wisej.AI.Services.DefaultHttpClientService
Namespace: Wisej.AI.Services
Assembly: Wisej.AI (3.5.0.0)
Represents a default HTTP client service that provides methods to send HTTP requests and manage default headers.
public class DefaultHttpClientService : IHttpClientService
Constructors
DefaultHttpClientService()

Initializes a new instance of DefaultHttpClientService.
Properties
HttpClient

HttpClient: Gets the instance of HttpClient used by this service.
This property initializes the HttpClient if it is not already initialized. A default User-Agent header is added to the client upon initialization. This property is thread-safe and locks on the instance to ensure that the HttpClient is initialized only once.
Timeout

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

Sends a GET request to the specified URI.
Returns: Task<HttpResponseMessage>. A task that represents the asynchronous operation. The task result contains the HTTP response message.
This method sends a GET request to the specified uri . The method is asynchronous and returns a task that completes when the HTTP response is received.
var clientService = new DefaultHttpClientService();
var response = await clientService.GetAsync("https://api.example.com/resource");
Throws:
ArgumentNullException Thrown if uri is null.
PostAsync(uri, content)

Sends a POST request to the specified URI with the specified content.
Returns: Task<HttpResponseMessage>. A task that represents the asynchronous operation. The task result contains the HTTP response message.
This method sends a POST request to the specified uri with the provided content . The method is asynchronous and returns a task that completes when the HTTP response is received.
var clientService = new DefaultHttpClientService();
var content = new StringContent("{ \"name\": \"value\" }", Encoding.UTF8, "application/json");
var response = await clientService.PostAsync("https://api.example.com/resource", content);
Throws:
ArgumentNullException Thrown if uri or content is null.
RemoveDefaultHeader(key)

Removes a default header from the HttpClient.
This method removes a header from the HTTP client's request headers based on the specified key . If the header does not exist, this method does nothing.
var clientService = new DefaultHttpClientService();
clientService.RemoveDefaultHeader("Authorization");
SendAsync(request)

Sends an HTTP request to the specified URI.
Returns: Task<HttpResponseMessage>. A task that represents the asynchronous operation. The task result contains the HTTP response message.
This method sends an HTTP request using the provided request . The method is asynchronous and returns a task that completes when the HTTP response is received.
var clientService = new DefaultHttpClientService();
var request = new HttpRequestMessage(HttpMethod.Put, "https://api.example.com/resource");
var response = await clientService.SendAsync(request);
Throws:
ArgumentNullException Thrown if request is null.
SetDefaultHeader(key, value)

Adds a default header to the HttpClient.
This method adds a default header to the HTTP client's request headers. If a header with the same key already exists, this method adds another entry to the headers collection.
var clientService = new DefaultHttpClientService();
clientService.SetDefaultHeader("Authorization", "Bearer token");
SetHandler(handler)

Sets a new HTTP handler for the HttpClient.
This method replaces the current HttpClient with a new instance using the specified handler . A default User-Agent header is also added to the new instance.
var clientService = new DefaultHttpClientService();
var handler = new HttpClientHandler
{
// Configure handler properties
};
clientService.SetHandler(handler);
Implements
Represents a service for handling HTTP client operations with customizable handlers and headers.
Last updated