|
@@ -4,6 +4,7 @@ using System;
|
|
|
using UnityEngine.Networking;
|
|
|
using System.Text;
|
|
|
using GFGGame.Launcher;
|
|
|
+using System.Reflection;
|
|
|
|
|
|
namespace GFGGame
|
|
|
{
|
|
@@ -13,68 +14,81 @@ namespace GFGGame
|
|
|
public class HttpTool : SingletonMonoBase<HttpTool>
|
|
|
{
|
|
|
|
|
|
- public void Get(string prefixUrl, string methodName, Action<string> callback)
|
|
|
+ public void Get(string url, Action<string> callback, bool showWrong = true)
|
|
|
{
|
|
|
- StartCoroutine(GetRequest(prefixUrl, methodName, callback));
|
|
|
+ StartCoroutine(GetRequest(url, callback, showWrong));
|
|
|
}
|
|
|
|
|
|
- public IEnumerator GetRequest(string prefixUrl, string methodName, Action<string> callback)
|
|
|
+ public void Get(string prefixUrl, string methodName, Action<string> callback, bool showWrong = true)
|
|
|
{
|
|
|
string url = prefixUrl + methodName;
|
|
|
- Debug.Log("get url : " + url);
|
|
|
+ StartCoroutine(GetRequest(url, callback, showWrong));
|
|
|
+ }
|
|
|
+
|
|
|
+ public IEnumerator GetRequest(string url, Action<string> callback, bool showWrong)
|
|
|
+ {
|
|
|
+ ET.Log.Debug("get url : " + url);
|
|
|
using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
|
|
|
{
|
|
|
webRequest.timeout = LauncherConfig.HTTP_GET_TIME_OUT;
|
|
|
yield return webRequest.SendWebRequest();
|
|
|
- ResultHandler(webRequest, callback, prefixUrl, () =>
|
|
|
+ ResultHandler(webRequest, callback, url, () =>
|
|
|
{
|
|
|
- Get(prefixUrl, methodName, callback);
|
|
|
- });
|
|
|
+ Get(url, callback);
|
|
|
+ }, showWrong);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- //jsonString 为json字符串,post提交的数据包为json
|
|
|
- public void Post(string prefixUrl, string methodName, string jsonString, Action<string> callback)
|
|
|
+ public void Post(string url, string body, Action<string> callback = null, string contentType = "application/text", bool showWrong = false)
|
|
|
{
|
|
|
- StartCoroutine(PostRequest(prefixUrl, methodName, jsonString, callback));
|
|
|
+ StartCoroutine(PostRequest(url, body, callback, contentType, showWrong));
|
|
|
}
|
|
|
|
|
|
- public IEnumerator PostRequest(string prefixUrl, string methodName, string jsonString, Action<string> callback)
|
|
|
+ public void Post(string prefixUrl, string methodName, string body, Action<string> callback = null, string contentType = "application/text", bool showWrong = false)
|
|
|
{
|
|
|
string url = prefixUrl + methodName;
|
|
|
- Debug.Log(string.Format("post url:{0} postData:{1}", url, jsonString));
|
|
|
+ Post(url, body, callback, contentType);
|
|
|
+ }
|
|
|
+
|
|
|
+ private IEnumerator PostRequest(string url, string body, Action<string> callback, string contentType, bool showWrong)
|
|
|
+ {
|
|
|
+
|
|
|
+ ET.Log.Debug(string.Format("post url:{0} postData:{1}", url, body));
|
|
|
using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
|
|
|
{
|
|
|
- byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
|
|
|
+ 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", "application/json");
|
|
|
+ webRequest.SetRequestHeader("Content-Type", contentType);
|
|
|
// webRequest.timeout;
|
|
|
yield return webRequest.SendWebRequest();
|
|
|
- ResultHandler(webRequest, callback, jsonString, () =>
|
|
|
+ ResultHandler(webRequest, callback, body, () =>
|
|
|
{
|
|
|
- Post(prefixUrl, methodName, jsonString, callback);
|
|
|
- });
|
|
|
+ Post(url, body, callback);
|
|
|
+ }, showWrong);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void ResultHandler(UnityWebRequest webRequest, Action<string> callback, string tag, Action retryCall)
|
|
|
+ public void ResultHandler(UnityWebRequest webRequest, Action<string> callback, string tag, Action retryCall, bool showWrong)
|
|
|
{
|
|
|
string paramCallback = null;
|
|
|
if (webRequest.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
|
|
|
{
|
|
|
- Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
|
|
|
- Alert.Show("连接服务器失败:\n请检查网络和服务器状态")
|
|
|
- .SetRightButton(true, "重试", (object data) =>
|
|
|
+ ET.Log.Error(webRequest.error + "\n" + webRequest.downloadHandler.text);
|
|
|
+ if(showWrong)
|
|
|
{
|
|
|
- retryCall();
|
|
|
- });
|
|
|
+ Alert.Show("连接服务器失败:\n请检查网络和服务器状态")
|
|
|
+ .SetRightButton(true, "重试", (object data) =>
|
|
|
+ {
|
|
|
+ retryCall();
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- Debug.Log("from server " + webRequest.downloadHandler.text + "\nby " + tag);
|
|
|
+ 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);
|