Version: Unity 6.5 Alpha (6000.5)
LanguageEnglish
  • C#

UnityHttpMessageHandler

class in UnityEngine.Networking

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

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

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.

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; } }

UnityHttpMessageHandler can be used with GrpcChannel to make gRPC calls.

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); } }

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

Properties

Property Description
CertificateHandlerHolds a reference to a CertificateHandler object, which manages certificate validation for the underlying UnityWebRequest that this UnityHttpMessageHandler creates.
HttpForcedVersionForce the version of HTTP used when making web requests with UnityHttpMessageHandler.

Constructors

Constructor Description
UnityHttpMessageHandlerCreates a UnityHttpMessageHandler with the default options.

Protected Methods

Method Description
SendAsyncSend an HTTP request as an asynchronous operation.
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961