# 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.

{% tabs %}
{% tab title="C#" %}

```csharp
public interface IHttpClientService
```

{% endtab %}

{% tab title="VB.NET" %}

```visual-basic
Public Interface IHttpClientService
```

{% endtab %}
{% endtabs %}

The [IHttpClientService](/ai/components/api/services/ihttpclientservice.md) interface provides methods for sending HTTP requests and handling responses. It allows setting a custom [HttpClientHandler](https://docs.microsoft.com/dotnet/api/system.net.http.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

### ![](/files/ptrKjmmRoQB76pvrIqh0) HttpClient

[HttpClient](https://docs.microsoft.com/dotnet/api/system.net.http.httpclient): Returns the underlying [HttpClient](#httpclient) instance.

### ![](/files/ptrKjmmRoQB76pvrIqh0) Timeout

[TimeSpan](https://docs.microsoft.com/dotnet/api/system.timespan): Gets or sets the timeout.

## Methods

### ![](/files/ptrKjmmRoQB76pvrIqh0) GetAsync(uri)

Sends an asynchronous GET request to the specified URI.

| Parameter | Type                                                          | Description                                                                                                 |
| --------- | ------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------- |
| **uri**   | [String](https://docs.microsoft.com/dotnet/api/system.string) | The URI to send the request to. This parameter is optional and defaults to an empty string if not provided. |

**Returns:** [Task\<HttpResponseMessage>](https://docs.microsoft.com/dotnet/api/system.threading.tasks.task-1). A task representing the asynchronous operation, containing the HTTP response message.

The [GetAsync](#getasync-uri) 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:

```csharp

  IHttpClientService clientService = new HttpClientService();
  HttpResponseMessage response = await clientService.GetAsync("https://api.example.com/resource");

```

**Throws:**

* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception)\
  Thrown when *uri* is null.

### ![](/files/ptrKjmmRoQB76pvrIqh0) PostAsync(uri, content)

Sends an asynchronous POST request to the specified URI with the given content.

| Parameter   | Type                                                                             | Description                                                                                                 |
| ----------- | -------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------- |
| **uri**     | [String](https://docs.microsoft.com/dotnet/api/system.string)                    | The URI to send the request to. This parameter is optional and defaults to an empty string if not provided. |
| **content** | [HttpContent](https://docs.microsoft.com/dotnet/api/system.net.http.httpcontent) | The HTTP content to send with the request. This parameter is optional and defaults to null if not provided. |

**Returns:** [Task\<HttpResponseMessage>](https://docs.microsoft.com/dotnet/api/system.threading.tasks.task-1). A task representing the asynchronous operation, containing the HTTP response message.

The [PostAsync](#postasync-uri-content) 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:

```csharp

  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](https://docs.microsoft.com/dotnet/api/system.argumentnullexception)\
  Thrown when *uri* or *content* is null.

### ![](/files/ptrKjmmRoQB76pvrIqh0) RemoveDefaultHeader(key)

Removes a default header from the requests.

| Parameter | Type                                                          | Description                                                                                                   |
| --------- | ------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------- |
| **key**   | [String](https://docs.microsoft.com/dotnet/api/system.string) | The name of the header to remove. This parameter is optional and defaults to an empty string if not provided. |

The [RemoveDefaultHeader](#removedefaultheader-key) method removes a previously added header, preventing it from being included in future requests. It is useful for managing dynamic header requirements.\
Example usage:

```csharp

  IHttpClientService clientService = new HttpClientService();
  clientService.RemoveDefaultHeader("Authorization");

```

### ![](/files/ptrKjmmRoQB76pvrIqh0) SendAsync(request)

Sends an asynchronous HTTP request to the specified URI.

| Parameter   | Type                                                                                           | Description                       |
| ----------- | ---------------------------------------------------------------------------------------------- | --------------------------------- |
| **request** | [HttpRequestMessage](https://docs.microsoft.com/dotnet/api/system.net.http.httprequestmessage) | The HTTP request message to send. |

**Returns:** [Task\<HttpResponseMessage>](https://docs.microsoft.com/dotnet/api/system.threading.tasks.task-1). A task representing the asynchronous operation, containing the HTTP response message.

The [SendAsync](#sendasync-request) 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:

```csharp

  IHttpClientService clientService = new HttpClientService();
  HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Put, "https://api.example.com/resource");
  HttpResponseMessage response = await clientService.SendAsync(request);

```

**Throws:**

* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception)\
  Thrown when *request* is null.

### ![](/files/ptrKjmmRoQB76pvrIqh0) SetDefaultHeader(key, value)

Adds a default header to be included in all requests.

| Parameter | Type                                                          | Description                                                                                                 |
| --------- | ------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------- |
| **key**   | [String](https://docs.microsoft.com/dotnet/api/system.string) | The name of the header to add. This parameter is optional and defaults to an empty string if not provided.  |
| **value** | [String](https://docs.microsoft.com/dotnet/api/system.string) | The value of the header to add. This parameter is optional and defaults to an empty string if not provided. |

The [SetDefaultHeader](#setdefaultheader-key-value) 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:

```csharp

  IHttpClientService clientService = new HttpClientService();
  clientService.SetDefaultHeader("Authorization", "Bearer token");

```

### ![](/files/ptrKjmmRoQB76pvrIqh0) SetHandler(handler)

Sets a custom HTTP client handler for the service.

| Parameter   | Type                                                                                         | Description                                                                                                                                                                            |
| ----------- | -------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **handler** | [HttpClientHandler](https://docs.microsoft.com/dotnet/api/system.net.http.httpclienthandler) | The [HttpClientHandler](https://docs.microsoft.com/dotnet/api/system.net.http.httpclienthandler) to use for requests. This parameter is optional and defaults to null if not provided. |

The [SetHandler](#sethandler-handler) 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:

```csharp

  IHttpClientService clientService = new HttpClientService();
  HttpClientHandler handler = new HttpClientHandler
  {
    UseCookies = false
  };
  clientService.SetHandler(handler);

```

## Implemented By

| Name                                                                                                | Description                                                                                                      |
| --------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| [DefaultHttpClientService](https://docs.wisej.com/api?q=wisej.ai.services.defaulthttpclientservice) | Represents a default HTTP client service that provides methods to send HTTP requests and manage default headers. |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.wisej.com/ai/components/api/services/ihttpclientservice.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
