using asap.core; using LitJson; using System; using System.Net; using System.Net.Http; using System.Text; using System.Threading.Tasks; using UnityEngine; public interface ICustomServerMgr { Task GetStringAsync(string url); Task CustomServerPost(string url, string bodyString, string contentType = "application/json"); Task AccountRequest(string url, object data); Task EventRequest(string url, object data); Task ClubRequest(string url, object data); Task BombRequest(string url, object data); Task DuelRequest(string url, object data); Task CommonDataRequest(string url, object data); Task FriendRequest(string url, object data); Task EventPartnerRequest(string url, object data = null); CookieContainer Cookie { get; } } public class CustomServerMgr : ICustomServerMgr { IConfig config { get; set; } //网络客户端 HttpClient client; private CookieContainer _cookie; public CookieContainer Cookie => _cookie ?? throw new System.NullReferenceException(); public CustomServerMgr(IConfig config) { this.config = config; var httpClientHandler = new HttpClientHandler(); _cookie = new CookieContainer(); httpClientHandler.CookieContainer = _cookie; client = new HttpClient(httpClientHandler); client.BaseAddress = new System.Uri(config.Get(GConstant.K_Event_API_URL, "http://192.168.9.101:5292/")); client.Timeout = TimeSpan.FromSeconds(15); } public async Task CustomServerPost(string url, string bodyString, string contentType = "application/json") { try { //#if UNITY_EDITOR // Debug.Log("CustomServerPost url:" + url); // Debug.Log("CustomServerPost bodyString:" + bodyString); //#endif var content = new StringContent(bodyString, Encoding.UTF8, "application/json"); var response = await client.PostAsync(url, content); string result = await response.Content.ReadAsStringAsync(); if (response.StatusCode == System.Net.HttpStatusCode.OK) { //GameDebug.Log("CustomServerPost result:" + result.ToString()); return result; } else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized || response.StatusCode == System.Net.HttpStatusCode.Forbidden) { GContext.Publish(new game.EvtAPISvrUnauthorized()); return null; } else { Debug.LogError(response.StatusCode.ToString() + "+++CustomServerPost error:" + result.ToString() + "+++CustomServerPost url:" + url + ":" + bodyString); return null; } } catch (Exception e) { Debug.LogError("CustomServerPost error:" + e.Message + "---CustomServerPost url:" + url + ":" + bodyString + "\n" + e.StackTrace); return null; } } public async Task GetStringAsync(string url) { var response = await client.GetAsync(url); if (response.StatusCode == HttpStatusCode.OK) { return await response.Content.ReadAsStringAsync(); } if (response.StatusCode == HttpStatusCode.Unauthorized) { GContext.Publish(new game.EvtAPISvrUnauthorized()); } return null; } public async Task AccountRequest(string url, object data) { return await CustomServerPost($"account/{url}", Newtonsoft.Json.JsonConvert.SerializeObject(data)); } public async Task EventRequest(string url, object data) { return await CustomServerPost($"event/{url}", Newtonsoft.Json.JsonConvert.SerializeObject(data)); } public async Task ClubRequest(string url, object data) { return await CustomServerPost($"club/{url}", Newtonsoft.Json.JsonConvert.SerializeObject(data)); } public async Task BombRequest(string url, object data) { return await CustomServerPost($"bomb/{url}", Newtonsoft.Json.JsonConvert.SerializeObject(data)); } public async Task CommonDataRequest(string url, object data) { return await CustomServerPost($"commondata/{url}", Newtonsoft.Json.JsonConvert.SerializeObject(data)); } public async Task DuelRequest(string url, object data) { return await CustomServerPost($"duel/{url}", Newtonsoft.Json.JsonConvert.SerializeObject(data)); } public async Task FriendRequest(string url, object data) { return await CustomServerPost($"friend/{url}", Newtonsoft.Json.JsonConvert.SerializeObject(data)); } public async Task EventPartnerRequest(string url, object data = null) { var body = Newtonsoft.Json.JsonConvert.SerializeObject(data); var responseString = await CustomServerPost($"eventpartner/{url}", body); try { return Newtonsoft.Json.JsonConvert.DeserializeObject(responseString); } catch(Exception e) { Debug.LogError( $"[EventPartner] Response {responseString} failed to be deserialized: {e.Message}"); return default(T); } } }