123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- using System.Collections;
- using UnityEngine;
- using System;
- using UnityEngine.Networking;
- using System.Text;
- using GFGGame.Launcher;
- using System.Reflection;
- using System.Collections.Generic;
- namespace GFGGame
- {
- /// <summary>
- /// Http Request SDK
- /// </summary>
- public class HttpTool : SingletonMonoBase<HttpTool>
- {
- //最大尝试次数
- const int TryTimes = 3;
- //存储尝试次数
- public Dictionary<string, int> TryTimesDic = new Dictionary<string, int>();
- public void Get(string url, Action<string> callback, bool showWrong = true)
- {
- StartCoroutine(GetRequest(url, callback, showWrong));
- }
- public void Get(string prefixUrl, string methodName, Action<string> callback, bool showWrong = true)
- {
- string url = prefixUrl + methodName;
- StartCoroutine(GetRequest(url, callback, showWrong));
- }
- public IEnumerator GetRequest(string url, Action<string> callback, bool showWrong)
- {
- var key = url;
- //Debug.Log("get url : " + key);
- using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
- {
- webRequest.timeout = LauncherConfig.HTTP_GET_TIME_OUT;
- TryTimesDic.TryGetValue(key, out var times);
- times++;
- TryTimesDic[key] = times;
- yield return webRequest.SendWebRequest();
- ResultHandler(webRequest, callback, url, () =>
- {
- if(times < HttpTool.TryTimes)
- {
- Get(url, callback, showWrong);
- }
- else
- {
- if (showWrong)
- {
- TryTimesDic.Remove(key);
- Debug.Log("get url : " + key);
- Alert.Show("连接服务器失败:\n请检查网络和服务器状态")
- .SetRightButton(true, "重试", (object data) =>
- {
- Get(url, callback, showWrong);
- });
- }
- }
- });
- }
- }
- public void Post(string url, string body, Action<string> callback = null, string contentType = "application/text", bool showWrong = false)
- {
- StartCoroutine(PostRequest(url, body, callback, contentType, showWrong));
- }
- public void Post(string prefixUrl, string methodName, string body, Action<string> callback = null, string contentType = "application/text", bool showWrong = false)
- {
- string url = prefixUrl + methodName;
- Post(url, body, callback, contentType);
- }
- private IEnumerator PostRequest(string url, string body, Action<string> callback, string contentType, bool showWrong)
- {
- var key = url + ":" + body;
- //ET.Log.Debug(string.Format($"post {key}", url, body));
- using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
- {
- byte[] bodyRaw = Encoding.UTF8.GetBytes(body);
- webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
- webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
- webRequest.timeout = LauncherConfig.HTTP_POST_TIME_OUT;
- //http header 的内容
- webRequest.SetRequestHeader("Content-Type", contentType);
- TryTimesDic.TryGetValue(key, out var times);
- times++;
- TryTimesDic[key] = times;
- yield return webRequest.SendWebRequest();
- ResultHandler(webRequest, callback, key, () =>
- {
- if (times < HttpTool.TryTimes)
- {
- Post(url, body, callback,contentType, showWrong);
- }
- else
- {
- if (showWrong)
- {
- TryTimesDic.Remove(key);
- Debug.Log(string.Format($"post {key}", url, body));
- Alert.Show("连接服务器失败:\n请检查网络和服务器状态")
- .SetRightButton(true, "重试", (object data) =>
- {
- Post(url, body, callback, contentType, showWrong);
- });
- }
- }
- });
- }
- }
- public void ResultHandler(UnityWebRequest webRequest, Action<string> callback, string tag, Action retryFunc)
- {
- string paramCallback = null;
- if (webRequest.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
- {
- Debug.LogWarning(webRequest.error + "\n" + webRequest.downloadHandler.text + "\nby " + tag);
- retryFunc?.Invoke();
- }
- else
- {
- //ET.Log.Debug("from server " + webRequest.downloadHandler.text + "\nby " + tag);
- paramCallback = webRequest.downloadHandler.text;
- //paramCallback = System.Text.Encoding.UTF8.GetString(webRequest.downloadHandler.data, 3, webRequest.downloadHandler.data.Length - 3);
- callback?.Invoke(paramCallback);
- }
- }
- }
- }
|