# UnityHttpMessageHandler

> An HttpMessageHandler that can be used with HttpClient to perform web requests using UnityWebRequest.

## Definition

* **Type:** Class
* **Namespace:** [UnityEngine.Networking](/engine/6000.7/script-reference/unityengine/networking.md)
* **Assembly:** UnityEngine.UnityWebRequestModule
* **Inherits from:** [HttpMessageHandler](https://learn.microsoft.com/dotnet/api/system.net.http.httpmessagehandler)
* **Implements:** [IDisposable](https://learn.microsoft.com/dotnet/api/system.idisposable)

```csharp
public sealed class UnityHttpMessageHandler : HttpMessageHandler, IDisposable
```

## Remarks

`UnityHttpMessageHandler` enables sending web requests to HTTP servers using the standard `HttpClient`. This allows the use of libraries that expect to use `HttpClient`, but allow the developer to replace the underlying `HttpMessageHandler` that `HttpClient` uses. The request and response streams can be used to stream data for uploading and downloading, respectively.

`UnityHttpMessageHandler` can be used with `GrpcChannel` to make gRPC calls.

In a basic use case, `UnityHttpMessageHandler` can be used with `HttpClient` to make HTTP requests.

## Examples

```csharp
using UnityEngine;
using System.Net.Http;
using UnityEngine.Networking;
using Grpc.Core;
using Grpc.Net.Client;

public class GrpcChannelFactory
{
    public static GrpcChannel CreateGrpcChannel()
    {
        var httpHandler = new UnityEngine.Networking.UnityHttpMessageHandler()
        var channel = GrpcChannel.ForAddress("https://www.my-server.com", new GrpcChannelOptions
        {
            HttpHandler = httpHandler
        });
        return channel;
    }
}
```

```csharp
using UnityEngine;
using System.Net.Http;
using UnityEngine.Networking;

public class MyBehaviour : MonoBehaviour
{
    void Start()
    {
        DoRequestAsync();
    }

    async void DoRequestAsync()
    {
        HttpClient client = new HttpClient(new UnityEngine.Networking.UnityHttpMessageHandler());
        var request = new HttpRequestMessage(HttpMethod.Get, "https://www.my-server.com");
        var response = await client.SendAsync(request);

        var status = response.StatusCode;
        var content = await response.Content.ReadAsStringAsync(); // content will contain the HTTP response body

        // Show results as text
        Debug.Log(content);
    }
}
```

## Constructors

| Constructor                                                                                                         | Description                                                   |
| ------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------- |
| [UnityHttpMessageHandler()](/engine/6000.7/script-reference/unityengine/networking/unityhttpmessagehandler/ctor.md) | Creates a `UnityHttpMessageHandler` with the default options. |

## Properties

| Property                                                                                                                   | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| -------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [CertificateHandler](/engine/6000.7/script-reference/unityengine/networking/unityhttpmessagehandler/certificatehandler.md) | Holds a reference to a [UnityHttpMessageHandler.CertificateHandler](/engine/6000.7/script-reference/unityengine/networking/unityhttpmessagehandler/certificatehandler.md) object, which manages certificate validation for the underlying [UnityWebRequest](/engine/6000.7/script-reference/unityengine/networking/unitywebrequest.md) that this [UnityHttpMessageHandler](/engine/6000.7/script-reference/unityengine/networking/unityhttpmessagehandler.md) creates. |
| [HttpForcedVersion](/engine/6000.7/script-reference/unityengine/networking/unityhttpmessagehandler/httpforcedversion.md)   | Force the version of HTTP used when making web requests with [UnityHttpMessageHandler](/engine/6000.7/script-reference/unityengine/networking/unityhttpmessagehandler.md).                                                                                                                                                                                                                                                                                             |

## Methods

| Method                                                                                                   | Description                                        |
| -------------------------------------------------------------------------------------------------------- | -------------------------------------------------- |
| [SendAsync](/engine/6000.7/script-reference/unityengine/networking/unityhttpmessagehandler/sendasync.md) | Send an HTTP request as an asynchronous operation. |

## Inheritance

**Inherited Members:**

* [HttpMessageHandler.Dispose()](https://learn.microsoft.com/dotnet/api/system.net.http.httpmessagehandler.dispose#system-net-http-httpmessagehandler-dispose)
