将 HttpWebRequest 转换为 UnityWebRequest

Converting HttpWebRequest to UnityWebRequest

如何使用 UnityWebRequest Put 消息而不是来自 System.net 的 HttpWebRequest 编写以下代码。

我需要在 WebGL 中进行 PUT 调用,因为 HttpWebRequest 在该平台上不工作。我试图以某种方式转换它,但这对我不起作用。

我需要一个 UnityWebRequest Put 方法来代替这两个方法。

 public static void InvokeHttpRequest(Uri endpointUri, string httpMethod,
            IDictionary<string, string> headers, string requestBody)
{
    try
    {
        var request = ConstructWebRequest(endpointUri, httpMethod, headers);

        if (!string.IsNullOrEmpty(requestBody))
        {
            var buffer = new byte[8192]; // arbitrary buffer size                        
            var requestStream = request.GetRequestStream();

            using (var inputStream = new MemoryStream(Encoding.UTF8.GetBytes(requestBody)))
            {
                int bytesRead;

                while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    requestStream.Write(buffer, 0, bytesRead);
                }
            }
        }

        CheckResponse(request);
    }
    catch (WebException ex)
    {
        using (var response = ex.Response as HttpWebResponse)
        {
            if (response != null)
            {
                var errorMsg = ReadResponseBody(response);
                Debug.LogError(
                    $"\n-- HTTP call failed with exception '{errorMsg}'," +
                    $" status code '{response.StatusCode}'");
            }
        }
    }
}

    public static HttpWebRequest ConstructWebRequest(Uri endpointUri, string httpMethod,
    IDictionary<string, string> headers)
{
    var request = (HttpWebRequest) WebRequest.Create(endpointUri);

    request.Method = httpMethod;

    foreach (var header in headers.Keys)
    {
        // not all headers can be set via the dictionary
        if (header.Equals("host", StringComparison.OrdinalIgnoreCase))
        {
            request.Host = headers[header];
        }
        else if (header.Equals("content-length", StringComparison.OrdinalIgnoreCase))
        {
            request.ContentLength = long.Parse(headers[header]);
        }
        else if (header.Equals("content-type", StringComparison.OrdinalIgnoreCase))
        {
            request.ContentType = headers[header];
        }
        else
        {
            request.Headers.Add(header, headers[header]);
        }
    }

    return request;
}
 private IEnumerator PostRequest(Uri uri, IDictionary<string, string> formHeaders, string contentBody)
{
    var uwr = new UnityWebRequest(uri, "PUT");
    var contentBytes = new UTF8Encoding().GetBytes(contentBody);
    uwr.uploadHandler = new UploadHandlerRaw(contentBytes);
    uwr.downloadHandler = new DownloadHandlerBuffer();

    foreach (var header in formHeaders.Keys)
    {
        // not all headers can be set via the dictionary
        if (header.Equals("host", StringComparison.OrdinalIgnoreCase))
        {
            uwr.SetRequestHeader("host", formHeaders[header]);
        }
        else if (header.Equals("content-length", StringComparison.OrdinalIgnoreCase))
        {
            uwr.SetRequestHeader("content-length", formHeaders[header]);
        }
        else if (header.Equals("content-type", StringComparison.OrdinalIgnoreCase))
        {
            uwr.SetRequestHeader("content-type", formHeaders[header]);
        }
        else
        {
            uwr.SetRequestHeader(header, formHeaders[header]);
        }
    }

    //Send the request then wait here until it returns
    yield return uwr.SendWebRequest();

    if (uwr.isNetworkError)
    {
        Debug.Log("Error While Sending: " + uwr.error);
    }
    else
    {
        Debug.Log("Received: " + uwr.downloadHandler.text);
    }
}