UnityWebRequest Post和Get数据的封装

public class WebRequest : MonoBehaviour
{
   
    private void Start()
    {
        string url = "http://192.169.19.239:8081/Sim";
        Dictionary keyValues = new Dictionary();
        keyValues.Add("request", "userInfo");
        HttpHelper.Request(this, url, HttpHelper.MethodType.POST, keyValues, delegate (object value) {
            // 这个object的封箱和拆箱有什么好办法吗?
            Debug.Log(value.ToString());
        }, HttpHelper.DownloadHanlderType.kHttpTEXT);
    }
}

public class HttpHelper
{
    public enum MethodType
    {
        GET,
        POST
    }

    public class DownloadHanlderType
    {
        public const string kHttpBYTE = "BYTE";
        public const string kHttpTEXT = "TEXT";
    }

   
    public static void Request(MonoBehaviour mono, string url, MethodType method, Dictionary form, Action callback, string responseType)
    {
        if (method == MethodType.GET)
        {
            url = CreateGetData(url, form);
            mono.StartCoroutine(Request(url, null, callback, responseType));
        }
        else if (method == MethodType.POST)
        {
            WWWForm formData = CreatePostData(form);
            mono.StartCoroutine(Request(url, formData, callback, responseType));
        }
        else
        {
            Debug.LogError("你不能这样子哦...");
        }
    }

    static IEnumerator Request(string url, WWWForm form, Action callback, string dateType)
    {
        UnityWebRequest request = null;
        if (form == null)
            request = UnityWebRequest.Get(url);
        else
            request = UnityWebRequest.Post(url, form);

        yield return request.SendWebRequest();
        if (request.isHttpError || request.isNetworkError)
        {
            Debug.LogErrorFormat("Request Error: {0}", request.error);
        }
        if (request.isDone)
        {
            if (dateType == DownloadHanlderType.kHttpTEXT)
            {
                callback?.Invoke(request.downloadHandler.text);
            }
            else if (dateType == DownloadHanlderType.kHttpBYTE)
            {
                callback?.Invoke(request.downloadHandler.data);
            }
            else
            {
                Debug.LogError("你不能这样子哦...");
            }
        }
    }

    private static string CreateGetData(string url, Dictionary form)
    {
        string data = "";
        if (form != null && form.Count > 0)
        {
            foreach (var item in form)
            {
                data += item.Key + "=";
                data += item.Value.ToString() + "&";
            }
        }
        if (url.IndexOf("?") == -1)
            url += "?";
        else
            url += "&";

        url += data.TrimEnd(new char[] { '&' });
        return url;
    }

    private static WWWForm CreatePostData(Dictionary formData)
    {
        WWWForm form = new WWWForm();
        if (formData != null && formData.Count > 0)
        {
            foreach (var item in formData)
            {
                if (item.Value is byte[])
                    form.AddBinaryData(item.Key, item.Value as byte[]);
                else
                    form.AddField(item.Key, item.Value.ToString());
            }
        }
        return form;
    }
} 
  

运行结果:

转载、使用时注明出处。

本文代码参考:https://blog.csdn.net/qq_35135955/article/details/52128157

你可能感兴趣的:(unity)