using System.Collections; using System.Collections.Generic; using System.Text; using UnityEngine.Networking; using System; using LitJson; using UnityEngine; using Newtonsoft.Json; public class WebManager : MonoBehaviour { public static WebManager GetInstance; public void Awake() { GetInstance = this; } Action mlogincomplete; public void Login(Action logincomplete) { mlogincomplete = logincomplete; var url = PROTOCOLS.URL + PROTOCOLS.Login; var code = MainAccountData.code; var nickname = MainAccountData.NickName; var avatar = MainAccountData.Avatar; var gender = MainAccountData.Gender; var country = MainAccountData.Country; var province = MainAccountData.Province; var city = MainAccountData.City; var device = MainAccountData.Device; var share_id = MainAccountData.ShareId; var scene = MainAccountData.Scene; var v = MainAccountData.ver; var args = "code=" + code + "&" + "nickname=" + nickname + "&" + "avatar=" + avatar + "&" + "gender=" + gender + "&" + "country=" + country + "&" + "province=" + province + "&" + "city=" + city + "&" + "device=" + device + "&" + "share_id=" + share_id + "&" + "scene=" + scene + "&" + "v=" + v; Debug.Log("--->>µÇ½¿ªÊ¼" + code); StartCoroutine(PostRequest(PROTOCOLS.Login, args, LoginCallback, null)); } private void LoginCallback(JsonData jsonData) { //Debug.Log("LoadXJ_Completecb"); //Debug.Log(_text); new MainAccountData(jsonData); MainAccountData.Instance.JundgCfg(jsonData["table_version"].ToString(), () => { if (mlogincomplete != null) mlogincomplete(); }); } public void HttpPostRequest_Send(string protocol, Dictionary param, System.Action completecb, System.Action errorcb, bool hasToken = true) { var jsonString = ""; if (hasToken) { jsonString = "token=" + MainAccountData.Instance.token + "&"; } if (protocol == PROTOCOLS.game || protocol == PROTOCOLS.end || protocol == PROTOCOLS.fission || protocol == PROTOCOLS.vd || protocol == PROTOCOLS.themefission || protocol == PROTOCOLS.revival || protocol == PROTOCOLS.Sign || protocol == PROTOCOLS.Buy) { string _newStr = "abcdefghijkimnopqrstuvwxyz0123456789"; string _code = "{"; string _mask = ""; for (int i = 0; i < 4; i++) { _mask += _newStr[UnityEngine.Random.Range(0, 100) % _newStr.Length]; } int _newIndex = 0; if (param != null) { foreach (var item in param) { _newIndex++; string key = item.Key; string value = item.Value; _code += "\"" + key + "\":\"" + value + "\""; if (_newIndex < param.Count) { _code += ","; } else { _code += "}"; } } } jsonString += "_r=" + _mask + this.base64Encode(_code); } else { if (param != null) { var _index = 0; foreach (var item in param) { _index++; string key = item.Key; string value = item.Value; jsonString += key + "=" + value; if (_index < param.Count) { jsonString += "&"; } } } } StartCoroutine(this.PostRequest(protocol, jsonString, completecb, errorcb)); ; } private IEnumerator PostRequest(string protocol, string jsonString, Action completecb, Action errorcb) { byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString); string url = PROTOCOLS.URL + protocol + "?" + jsonString; Debug.Log("PostRequest--->>>url-->" + url); using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST", new DownloadHandlerBuffer(), new UploadHandlerRaw(bodyRaw))) { webRequest.SetRequestHeader("Content-Type", "application/json"); yield return webRequest.SendWebRequest(); if (webRequest.isHttpError || webRequest.isNetworkError) { Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text); if (errorcb != null) { this.onHttpRequestErrorcb(webRequest.downloadHandler.text, errorcb); } } else { if (completecb != null) { this.onHttpRequestComplete(webRequest.downloadHandler.text, completecb, errorcb); } } } } private void onHttpRequestComplete(string _text, Action completecb, Action errorcb) { //JObject obj = JsonConvert.DeserializeObject(_text); JsonData valuePairs = JsonMapper.ToObject(_text); if (valuePairs["code"].ToString() != "0") { Debug.Log("Error! Http request complete error" + valuePairs["message"]); if (errorcb != null) { errorcb(valuePairs); } return; } if (completecb != null) { Debug.Log(_text); completecb(valuePairs); } } private void onHttpRequestErrorcb(string _text, Action errorcb) { JsonData valuePairs = JsonMapper.ToObject(_text); Debug.Log("Error! Http request error..." + valuePairs["message"]); if (errorcb != null) { errorcb(valuePairs); } } private string base64Encode(string str) { Debug.Log("base64Encode-->>" + str); int c1, c2, c3; string _base64Encode = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; char[] base64EncodeChars = _base64Encode.ToCharArray(); char[] strChars = str.ToCharArray(); int i = 0; int len = str.Length; string stringStr = ""; while (i < len) { c1 = strChars[i++] & 0xff; if (i == len) { stringStr += base64EncodeChars[c1 >> 2]; stringStr += base64EncodeChars[(c1 & 0x3) << 4]; stringStr += "=="; break; } c2 = str[i++]; if (i == len) { stringStr += base64EncodeChars[c1 >> 2]; stringStr += base64EncodeChars[((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4)]; stringStr += base64EncodeChars[(c2 & 0xF) << 2]; stringStr += "="; break; } c3 = str[i++]; stringStr += base64EncodeChars[c1 >> 2]; stringStr += base64EncodeChars[((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4)]; stringStr += base64EncodeChars[((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6)]; stringStr += base64EncodeChars[c3 & 0x3F]; } Debug.Log(stringStr.Length + " " + stringStr); return stringStr; } void OnHpptError() { } }