Skip to main content

DefaultHttpClientService

Namespace: Wisej.AI.Services

Assembly: Wisej.AI

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.

ParameterTypeDescription
uriStringThe URI to which the GET request is sent.

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:

PostAsync(uri, content)

Sends a POST request to the specified URI with the specified content.

ParameterTypeDescription
uriStringThe URI to which the POST request is sent.
contentHttpContentThe HTTP content to be sent with the request.

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:

RemoveDefaultHeader(key)

Removes a default header from the HttpClient.

ParameterTypeDescription
keyStringThe key of the header to be removed.

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.

ParameterTypeDescription
requestHttpRequestMessageThe HTTP request message to send.

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:

SetDefaultHeader(key, value)

Adds a default header to the HttpClient.

ParameterTypeDescription
keyStringThe key of the header to be added.
valueStringThe value of the header to be added.

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.

ParameterTypeDescription
handlerHttpClientHandlerThe HttpClientHandler to be used by 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

NameDescription
IHttpClientServiceRepresents a service for handling HTTP client operations with customizable handlers and headers.