| | |
| | | using System.IO; |
| | | using System.Net; |
| | | using System.Text; |
| | | using Cysharp.Threading.Tasks; |
| | | using UnityEngine; |
| | | using UnityEngine.Networking; |
| | | |
| | | |
| | | public class HttpRequest : SingletonMonobehaviour<HttpRequest> |
| | | public class HttpRequest : SingletonMonobehaviour<HttpRequest> |
| | | |
| | | { |
| | | public const string defaultHttpContentType = "application/x-www-form-urlencoded"; |
| | | public const string jsonHttpContentType = "application/json ; charset=utf-8"; |
| | | |
| | | public void RequestHttpPost(string _url, IDictionary<string, string> _parameters, string _contentType, int _retry = 3, Action<bool, string> _result = null) |
| | | { |
| | | public const string defaultHttpContentType = "application/x-www-form-urlencoded"; |
| | | public const string jsonHttpContentType = "application/json ; charset=utf-8"; |
| | | var content = HashtablaToString(_parameters); |
| | | HttpBehaviour.Create(_url, "POST", content, _contentType, _retry, _result); |
| | | } |
| | | |
| | | public void RequestHttpPost(string _url, IDictionary<string, string> _parameters, string _contentType, int _retry = 3, Action<bool, string> _result = null) |
| | | public void RequestHttpPost(string _url, string _content, string _contentType, int _retry = 3, Action<bool, string> _result = null) |
| | | { |
| | | HttpBehaviour.Create(_url, "POST", _content, _contentType, _retry, _result); |
| | | } |
| | | |
| | | public void RequestHttpGet(string _url, string _contentType, int _retry = 3, Action<bool, string> _result = null) |
| | | { |
| | | HttpBehaviour.Create(_url, "GET", "", _contentType, _retry, _result); |
| | | } |
| | | |
| | | //2024-09-26 HttpWebRequest Get CDN资源在某些节日可能无法访问 包含keepalive的设定等不同情况 改用UnityWebRequest |
| | | //获取CDN资源不建议使用HttpWebRequest |
| | | public void UnityWebRequestGet(string _url, int timeout = 5, Action<bool, string> _result = null) |
| | | { |
| | | StartCoroutine(GetDataB(_url, timeout, _result)); |
| | | } |
| | | |
| | | IEnumerator GetDataB(string remoteURL, int timeout, Action<bool, string> _result = null) |
| | | { |
| | | UnityWebRequest request = UnityWebRequest.Get(remoteURL); |
| | | request.timeout = timeout; |
| | | yield return request.SendWebRequest(); |
| | | |
| | | if (request.isDone) |
| | | { |
| | | var content = HashtablaToString(_parameters); |
| | | HttpBehaviour.Create(_url, "POST", content, _contentType, _retry, _result); |
| | | } |
| | | |
| | | public void RequestHttpPost(string _url, string _content, string _contentType, int _retry = 3, Action<bool, string> _result = null) |
| | | { |
| | | HttpBehaviour.Create(_url, "POST", _content, _contentType, _retry, _result); |
| | | } |
| | | |
| | | public void RequestHttpGet(string _url, string _contentType, int _retry = 3, Action<bool, string> _result = null) |
| | | { |
| | | HttpBehaviour.Create(_url, "GET", "", _contentType, _retry, _result); |
| | | } |
| | | |
| | | //2024-09-26 HttpWebRequest Get CDN资源在某些节日可能无法访问 包含keepalive的设定等不同情况 改用UnityWebRequest |
| | | //获取CDN资源不建议使用HttpWebRequest |
| | | public void UnityWebRequestGet(string _url, int timeout = 5, Action<bool, string> _result = null) |
| | | { |
| | | StartCoroutine(GetDataB(_url, timeout, _result)); |
| | | } |
| | | |
| | | IEnumerator GetDataB(string remoteURL, int timeout, Action<bool, string> _result = null) |
| | | { |
| | | UnityWebRequest request = UnityWebRequest.Get(remoteURL); |
| | | request.timeout = timeout; |
| | | yield return request.SendWebRequest(); |
| | | |
| | | if (request.isDone) |
| | | if (request.result == UnityWebRequest.Result.Success) |
| | | { |
| | | if (request.result == UnityWebRequest.Result.Success) |
| | | { |
| | | _result(true, request.downloadHandler.text); |
| | | } |
| | | else |
| | | { |
| | | _result(false, request.result.ToString()); |
| | | } |
| | | _result(true, request.downloadHandler.text); |
| | | } |
| | | else |
| | | { |
| | | _result(false, request.result.ToString()); |
| | | } |
| | | } |
| | | } |
| | | |
| | | //回合战报 |
| | | public void UnityWebRequestTurnFightGet(string _url, string guid, int timeout = 5, Action<bool, string, byte[]> _result = null) |
| | | { |
| | | GetDataBEx(_url, guid, timeout, _result).Forget(); |
| | | } |
| | | |
| | | static StringBuilder buffer = new StringBuilder(); |
| | | public static string HashtablaToString(IDictionary<string, string> parameters) |
| | | async UniTask GetDataBEx(string remoteURL, string guid, int timeout, Action<bool, string, byte[]> _result = null) |
| | | { |
| | | UnityWebRequest request = UnityWebRequest.Get(remoteURL); |
| | | request.timeout = timeout; |
| | | await request.SendWebRequest(); |
| | | |
| | | if (request.isDone) |
| | | { |
| | | buffer.Remove(0, buffer.Length); |
| | | int i = 0; |
| | | foreach (KeyValuePair<string, string> item in parameters) |
| | | if (request.result == UnityWebRequest.Result.Success) |
| | | { |
| | | if (i > 0) |
| | | { |
| | | buffer.AppendFormat("&{0}={1}", item.Key, item.Value); |
| | | } |
| | | else |
| | | { |
| | | buffer.AppendFormat("{0}={1}", item.Key, item.Value); |
| | | } |
| | | i++; |
| | | _result(true, guid, request.downloadHandler.data); |
| | | } |
| | | string result = buffer.ToString(); |
| | | return result; |
| | | } |
| | | else |
| | | { |
| | | Debug.LogError("GetDataBEx 失败 " + request.result.ToString()); |
| | | } |
| | | |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | 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; |
| | | } |
| | | |
| | | } |