Unity的网络请求_短连接

using System.Text;
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using System.Collections.Generic;

public class HttpControl : MonoBehaviour
{
    
    
    public HttpType hType;

    private void Start()                 //测试代码
    {
    
    
        Debug.Log("HttpType:" + hType.ToString());
        string url = "https://api.apiopen.top/todayVideo";
        string urlGET = "https://api.apiopen.top/musicRankingsDetails?";
        if (hType == HttpType.GET)
        {
    
    
            StartCoroutine(SendMsgGET(urlGET, "type=1"));
        }
        else if (hType == HttpType.POST)
        {
    
    
            StartCoroutine(SendMsgPOST(url, "1", "1"));
        }
    }
    /// <summary>
    /// POST请求
    /// </summary>
    /// <param name="url"></param>
    /// <param name="content"></param>
    /// <param name="token"></param>
    /// <returns></returns>
    public IEnumerator SendMsgPOST(string url, string content, string token)
    {
    
    
        UnityWebRequest unityWeb = new UnityWebRequest(url, "POST");
        byte[] body = Encoding.UTF8.GetBytes(content);
        unityWeb.uploadHandler = new UploadHandlerRaw(body);
        unityWeb.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
        unityWeb.SetRequestHeader("authorization", "Bearer " + token);
        unityWeb.certificateHandler = new WebRequestCert();
        unityWeb.downloadHandler = new DownloadHandlerBuffer();
        UnityWebRequestAsyncOperation ao = unityWeb.SendWebRequest();
        yield return ao;
        if (ao.webRequest.isNetworkError || ao.webRequest.isHttpError)
        {
    
    
            Debug.Log("error receiving.");
        }
        else
        {
    
    
            byte[] encodedBytes = Encoding.UTF8.GetBytes(ao.webRequest.downloadHandler.text);
            string data = Encoding.UTF8.GetString(encodedBytes);
            Debug.Log("POST" + data);
        }
    }
    public IEnumerator SendMsgGET(string url, string content)
    {
    
    
        UnityWebRequest unityWeb = UnityWebRequest.Get(url + content);
        unityWeb.downloadHandler = new DownloadHandlerBuffer();
        UnityWebRequestAsyncOperation ao = unityWeb.SendWebRequest();
        yield return ao;
        if (ao.webRequest.isNetworkError || ao.webRequest.isHttpError)
        {
    
    
            Debug.Log("error receiving.");
        }
        else
        {
    
    
            byte[] encodedBytes = Encoding.UTF8.GetBytes(ao.webRequest.downloadHandler.text);
            string data = Encoding.UTF8.GetString(encodedBytes);
            Debug.Log("GET" + data + "...");
        }
    }
}
/// <summary>
/// HTTP类型选择
/// </summary>
public enum HttpType
{
    
    
    POST,
    GET
}
/// <summary>
/// https跳过证书验证
/// </summary>
public class WebRequestCert : CertificateHandler
{
    
    
    protected override bool ValidateCertificate(byte[] certificateData)
    {
    
    
        return true;
    }
}

猜你喜欢

转载自blog.csdn.net/kuilaurence/article/details/109727317