三国卡牌客户端基础资源仓库
yyl
2026-03-28 25eb0e50d4e815efb16d1a9953beac6ea1c7cfc3
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
 
 
public class HttpRequest : SingletonMonobehaviour<HttpRequest>
 
{
    public const string defaultHttpContentType = "application/x-www-form-urlencoded";
    public const string jsonHttpContentType = "application/json ; charset=utf-8";
 
    public async void RequestHttpPost(string _url, IDictionary<string, string> _parameters, string _contentType, int _retry = 3, Action<bool, string> _result = null)
    {
        var content = HashtablaToString(_parameters);
         var task = HttpBehaviour.CreateAsync(_url, "POST", content, _contentType, _retry, _result);
         task.Forget();
    }
 
    public async void RequestHttpPost(string _url, string _content, string _contentType, int _retry = 3, Action<bool, string> _result = null)
    {
         var task = HttpBehaviour.CreateAsync(_url, "POST", _content, _contentType, _retry, _result);
         task.Forget();
    }
 
    public async void RequestHttpGet(string _url, string _contentType, int _retry = 3, Action<bool, string> _result = null)
    {
         var task = HttpBehaviour.CreateAsync(_url, "GET", "", _contentType, _retry, _result);
         task.Forget();
    }
 
    public async UniTask<(bool ok, string message)> UnityWebRequestGetAsync(string _url, string _contentType, int _retry = 3, Action<bool, string> _result = null)
    {
        return await HttpBehaviour.CreateAsync(_url, "GET", "", _contentType, _retry, _result);
    }
 
    //2024-09-26 HttpWebRequest Get CDN资源在某些节日可能无法访问 包含keepalive的设定等不同情况 改用UnityWebRequest
    //获取CDN资源不建议使用HttpWebRequest
    public async UniTask<(bool ok, string message)> UnityWebRequestGet(string _url, int timeout = 5, Action<bool, string> _result = null)
    {
        return await GetDataAsync(_url, timeout, _result);
    }
 
    private static async UniTask<(bool ok, string message)> GetDataAsync(string remoteURL, int timeout, Action<bool, string> _result = null)
    {
        using (UnityWebRequest request = UnityWebRequest.Get(remoteURL))
        {
            request.timeout = timeout;
            await request.SendWebRequest();
 
            var ok = request.result == UnityWebRequest.Result.Success;
            var message = ok ? request.downloadHandler.text : request.error;
            _result?.Invoke(ok, message);
            return (ok, message);
        }
    }
 
    
 
    static StringBuilder buffer = new StringBuilder();
    public static string HashtablaToString(IDictionary<string, string> parameters)
    {
        buffer.Remove(0, buffer.Length);
        int i = 0;
        foreach (KeyValuePair<string, string> item in parameters)
        {
            if (i > 0)
            {
                buffer.AppendFormat("&{0}={1}", item.Key, item.Value);
            }
            else
            {
                buffer.AppendFormat("{0}={1}", item.Key, item.Value);
            }
            i++;
        }
        string result = buffer.ToString();
        return result;
    }
 
}