liuzhiwei
2020-11-30 31dbc3717e73353eaf5b8627489308adee9f51a6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
 
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
 
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<string, object> form, Action<object> 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<object> 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<string, object> 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<string, object> 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;
    }
}