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;
///
/// Http Request SDK
///
public class HttpTool : MonoSingleton
{
// private string baseUrl = "http://office.ghz-tech.com:3425/guideSystem";
//private string baseUrl = "http://office.ghz-tech.com:9904/api";
private string baseUrl = "https://nav21.ghz-tech.com:8843/api/";
private string UrlPointCloud = "https://pro.qulivr.com/mr-navigator/v1";
private string token = "";
Dictionary requestHeader = new Dictionary(); // header
void Awake()
{
//http header 的内容
requestHeader.Add("Content-Type", "application/json");
}
public string Token
{
get { return token; }
}
public string BaseUrl
{
get { return baseUrl; }
}
public Dictionary RequestHeader
{
get { return requestHeader; }
}
public void Get(string methodName, Action callback)
{
StartCoroutine(GetRequest(methodName, callback));
}
private IEnumerator GetRequest(string methodName, Action callback)
{
// string url = Application.streamingAssetsPath + methodName;
string url = baseUrl + methodName;
//Debug.Log("URL:"+url);
using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
{
webRequest.SetRequestHeader("authorization", token);
//设置header
foreach (var v in requestHeader)
{
webRequest.SetRequestHeader(v.Key, v.Value);
}
yield return webRequest.SendWebRequest();
if (webRequest.isHttpError || webRequest.isNetworkError)
{
Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
if (callback != null)
{
callback(null);
}
}
else
{
if (callback != null)
{
callback(webRequest.downloadHandler.text);
}
}
}
}
public void PostTest(string methodName, string jsonString, Action CallBack)
{
StartCoroutine(PostRequestTest(methodName, jsonString, CallBack));
}
private IEnumerator PostRequestTest(string methodName, string jsonString, Action CallBack)
{
string url = baseUrl + methodName;
Debug.Log(url + jsonString);
using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
{
byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
webRequest.SetRequestHeader("authorization", token);
foreach (var v in requestHeader)
{
webRequest.SetRequestHeader(v.Key, v.Value);
Debug.Log(v.Value + " " + methodName);
}
webRequest.timeout = 5;
yield return webRequest.SendWebRequest();
if (webRequest.isHttpError || webRequest.isNetworkError)
{
CallBack?.Invoke("UnityWebRequest Error");
Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text + "\n" + methodName);
//GameManager.Instance.text2.text = webRequest.error;
}
else
{
Debug.Log(webRequest.downloadHandler.text);
CallBack(webRequest.downloadHandler.text);
}
}
}
public void PostSnInfo(string methodName, string jsonString, int index, Action CallBack)
{
StartCoroutine(PostnInfoRequest(methodName, jsonString, index, CallBack));
}
public IEnumerator PostnInfoRequest(string methodName, string jsonString, int index, Action CallBack)
{
string url = baseUrl + methodName;
Debug.Log("URL:" + url);
using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
{
byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
webRequest.SetRequestHeader("authorization", token);
foreach (var v in requestHeader)
{
webRequest.SetRequestHeader(v.Key, v.Value);
//Debug.Log(v.Value + " " + methodName);
}
yield return webRequest.SendWebRequest();
if (webRequest.isHttpError || webRequest.isNetworkError)
{
Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text + "\n" + methodName);
//GameManager.Instance.text2.text = webRequest.error;
}
else
{
Debug.Log(webRequest.downloadHandler.text);
CallBack(index, webRequest.downloadHandler.text);
}
}
}
public IEnumerator PostRequestSpoitDetail(string methodName, string jsonString, Action CallBack)
{
string url = baseUrl + methodName;
Debug.Log(url);
using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
{
byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
webRequest.SetRequestHeader("authorization", token);
foreach (var v in requestHeader)
{
webRequest.SetRequestHeader(v.Key, v.Value);
//Debug.Log(v.Value + " " + methodName);
}
yield return webRequest.SendWebRequest();
if (webRequest.isHttpError || webRequest.isNetworkError)
{
Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text + "\n" + methodName);
//GameManager.Instance.text2.text = webRequest.error;
}
else
{
//Debug.Log(webRequest.downloadHandler.text);
CallBack(webRequest.downloadHandler.text);
}
}
}
public void Post(string methodName, string jsonString, Action CallBack)
{
StartCoroutine(PostRequest(methodName, jsonString, CallBack));
}
//
private IEnumerator PostRequest(string methodName, string jsonString, Action CallBack)
{
string url = baseUrl + methodName;
//Debug.Log(url);
using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
{
byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
webRequest.SetRequestHeader("authorization", token);
foreach (var v in requestHeader)
{
webRequest.SetRequestHeader(v.Key, v.Value);
//Debug.Log(v.Value + " " + methodName);
}
yield return webRequest.SendWebRequest();
if (webRequest.isHttpError || webRequest.isNetworkError)
{
Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text + "\n" + methodName);
//GameManager.Instance.text2.text = webRequest.error;
}
else
{
//Debug.Log(webRequest.downloadHandler.text);
CallBack(webRequest.downloadHandler.text);
}
}
}
public void GetAllMaterials(string methodName, string jsonString, Action CallBack)
{
StartCoroutine(GetRequest(methodName, jsonString, CallBack));
}
private IEnumerator GetRequest(string methodName, string jsonString, Action CallBack)
{
string url = baseUrl + methodName;
//Debug.Log("URL:"+url);
token = jsonString;
HeadAddToken(token);
using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
{
webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
webRequest.SetRequestHeader("authorization", jsonString);
foreach (var v in requestHeader)
{
//Debug.Log(v.Key + " " + v.Value);
webRequest.SetRequestHeader(v.Key, v.Value);
}
yield return webRequest.SendWebRequest();
if (webRequest.isHttpError || webRequest.isNetworkError)
{
Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
}
else
{
//Debug.Log(webRequest.downloadHandler.text);
CallBack(webRequest.downloadHandler.text);
}
}
}
///
/// 登录
///
///
///
///
public void PostLogin(string methodName, string jsonString, Action CallBack)
{
StartCoroutine(PostRequestLogin(methodName, jsonString, CallBack));
}
private IEnumerator PostRequestLogin(string methodName, string jsonString, Action CallBack)
{
string url = baseUrl + methodName;
Debug.Log("URL:" + url);
using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
{
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.isHttpError || webRequest.isNetworkError)
{
Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
string error = webRequest.downloadHandler.text;
JObject jObject = JObject.Parse(error);
CallBack(jObject["message"].ToString());
}
else
{
//Debug.Log(webRequest.downloadHandler.text);
var mes = webRequest.downloadHandler.text;
JObject obj = JObject.Parse(mes);
if (obj["code"].ToString() == "200")
{
//Debug.Log(obj["data"]["token"].ToString());
token = obj["data"]["token"].ToString();
//Debug.Log(mes);
}
CallBack(mes);
}
}
}
/////
///// 获取本地Sprite
/////
/////
/////
//public void GetLocalSprite(DownLoadMaterial mObj, Action CallBack)
//{
// StartCoroutine(GetSpriteRequest(mObj, CallBack));
//}
//private IEnumerator GetSpriteRequest(DownLoadMaterial mObj, Action CallBack)
//{
// UnityWebRequest request = UnityWebRequestTexture.GetTexture(mObj.mObj.localSavePath);
// yield return request.SendWebRequest();
// if (request.isNetworkError || request.isHttpError)
// {
// Debug.LogError(request.error);
// }
// else
// {
// Texture2D texture = DownloadHandlerTexture.GetContent(request);
// Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
// CallBack(mObj, sprite);
// // LoadManager.Instance.DownLoadEnd(mObj, sprite);
// }
//}
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;
}
}