123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- using UnityEngine.Networking;
- using System.Text;
- using System.Security.Cryptography;
- using System.Net;
- using System.IO;
- using Newtonsoft.Json.Linq;
- /// <summary>
- /// Http Request SDK
- /// </summary>
- public class HttpTool : MonoSingleton<HttpTool>
- {
- Dictionary<string, string> requestHeader = new Dictionary<string, string>(); // header
- void Awake()
- {
- //http header 的内容
- requestHeader.Add("Content-Type", "application/json");
- }
- // public string Token
- // {
- // get { return token; }
- // }
- public Dictionary<string, string> RequestHeader
- {
- get { return requestHeader; }
- }
- public void Get(string methodName, Action<string> callback)
- {
- StartCoroutine(SendHttp(methodName,"", callback,false));
- }
- public void PostTest(string methodName, string jsonString, Action<string> CallBack)
- {
- StartCoroutine(SendHttp(methodName, jsonString, CallBack));
- }
- public void Post(string methodName, string jsonString, Action<string> CallBack)
- {
- StartCoroutine(SendHttp(methodName, jsonString, CallBack));
- }
- public void GetAllMaterials(string methodName, string jsonString, Action<String> CallBack)
- {
- StartCoroutine(SendHttp(methodName, "", CallBack,false));
- }
- /// <summary>
- /// 登录
- /// </summary>
- /// <param name="methodName"></param>
- /// <param name="jsonString"></param>
- /// <param name="CallBack"></param>
- public void PostLogin(string methodName, string jsonString, Action<string> CallBack)
- {
- StartCoroutine(SendHttp(methodName, jsonString, CallBack));
- }
- public void initHead()
- {
- requestHeader.Clear();
- if (UserInfo.Instance.Token!="" && UserInfo.Instance.Token!=null)
- {
- requestHeader.Add("x-token", UserInfo.Instance.Token);
- requestHeader.Add("authorization", UserInfo.Instance.Token);
- }
- requestHeader.Add("Content-Type", "application/json");
- }
- public IEnumerator SendHttp(string methodName, string jsonString, Action<string> CallBack,bool isPost=true)
- {
- //Debug.Log("Start Queue SendHttp " + methodName);
- string url;
- if (methodName.Contains("http"))
- {
- url =methodName;
- }
- else
- {
- if(UserInfo.Instance.is20)
- {
- url = HttpAction.Instance.baseurl20 + methodName;
- }
- else
- {
- url = HttpAction.Instance.baseurl + methodName;
- }
- }
- Debug.Log(url+"开始通讯 " + methodName + " 发送数据 " + jsonString + " 是否Post" + isPost);
- // Debug.Log("URL:" + url+" isPost "+ isPost);
- UnityWebRequest webRequest;
- if (!isPost)
- {
- webRequest = UnityWebRequest.Get(url);
- }
- else
- {
- webRequest = new UnityWebRequest(url, "POST");
- }
- Debug.Log(url + "开始通讯 URL " + url);
- initHead();
- using (webRequest)
- {
- if (!isPost)
- {
- webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
- foreach (var v in requestHeader)
- {
- webRequest.SetRequestHeader(v.Key, v.Value);
- }
- }
- else
- {
- byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
- webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
- webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
- }
- foreach (var v in requestHeader)
- {
- webRequest.SetRequestHeader(v.Key, v.Value);
- }
- yield return webRequest.SendWebRequest();
- if (webRequest.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)//
- {
- Debug.LogError(url + "\n" + webRequest.error + "\n" + webRequest.downloadHandler.text);
- string error = webRequest.downloadHandler.error;
- //JObject jObject = JObject.Parse(error);
- CallBack(error);
- }
- else
- {
- //Debug.Log(webRequest.downloadHandler.text);
- var mes = webRequest.downloadHandler.text;
- CallBack(mes);
- Debug.Log(url+"通讯回调信息 " + webRequest.downloadHandler.text);
- }
- }
- }
- public void HeadAddToken(string token)
- {
- requestHeader.Add("x-token", token);
- }
- public string GetMd5Hash(string strToEncrypt)
- {
- MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
- byte[] bytes = Encoding.ASCII.GetBytes(strToEncrypt);
- byte[] encoded = md5.ComputeHash(bytes);
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < 10; i++)
- {
- sb.Append(encoded[i].ToString("x2"));
- }
- string password = sb.ToString();
- password = password.Substring(0, 10);
- return password;
- }
- }
|