110 lines
4.4 KiB
C#
110 lines
4.4 KiB
C#
|
|
|||
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using Unity.VisualScripting.Antlr3.Runtime;
|
|||
|
using UnityEngine;
|
|||
|
using UnityEngine.Networking;
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
public class web : MonoBehaviour
|
|||
|
{
|
|||
|
public const string URL = "http://192.168.3.103:8089"; // <20><>ע<EFBFBD>͵<EFBFBD><CDB5>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
//public const string URL = "ws://124.221.149.22:38445/safety/cloud"; // <20><>ע<EFBFBD>͵<EFBFBD><CDB5>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
// <20>洢<EFBFBD><E6B4A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>Cookie
|
|||
|
private static Dictionary<string, string> cookieJar = new Dictionary<string, string>();
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD>HTTP<54><50><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD>֧<EFBFBD><D6A7>GET<45><54>POST<53>Ȳ<EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ
|
|||
|
public static async Task<string> SendRequest(string url, string method = "GET", string jsonData = "{}", Dictionary<string, string> head = null)
|
|||
|
{
|
|||
|
Debug.Log(url + "<22><><EFBFBD><EFBFBD>web"); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
// ʹ<><CAB9>UnityWebRequest<73><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>HTTP<54><50><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֧<EFBFBD><D6A7><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
using (UnityWebRequest request = new UnityWebRequest(url, method))
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ش<EFBFBD><D8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
request.downloadHandler = new DownloadHandlerBuffer();
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>POST<53><54><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
if (method == "POST" && jsonData != null)
|
|||
|
{
|
|||
|
// <20><>JSON<4F>ַ<EFBFBD><D6B7><EFBFBD>ת<EFBFBD><D7AA>Ϊ<EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonData);
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڷ<EFBFBD><DAB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ΪJSON
|
|||
|
request.SetRequestHeader("Content-Type", "application/json");
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD>Ϣ
|
|||
|
if (head != null)
|
|||
|
foreach (string key in head.Keys)
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD>ֵ䣬<D6B5><E4A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7>
|
|||
|
request.SetRequestHeader(key, head[key]);
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>Ѵ洢<D1B4><E6B4A2> Cookie
|
|||
|
if (cookieJar.Count > 0)
|
|||
|
{
|
|||
|
// <20><><EFBFBD>ڴ洢<DAB4><E6B4A2><EFBFBD><EFBFBD>cookieֵ<65><D6B5><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
string cookieHeader = "";
|
|||
|
foreach (var cookie in cookieJar)
|
|||
|
{
|
|||
|
// <20><>ʽ<EFBFBD><CABD>ÿ<EFBFBD><C3BF>cookieΪ<65><CEAA><EFBFBD><EFBFBD>=ֵ<><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>У<EFBFBD><D0A3>Էֺŷָ<C5B7>
|
|||
|
cookieHeader += $"{cookie.Key}={cookie.Value}; ";
|
|||
|
}
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD>е<EFBFBD>Cookie<69>ֶ<EFBFBD>
|
|||
|
request.SetRequestHeader("Cookie", cookieHeader);
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ<F3B2A2B5><C8B4><EFBFBD>Ӧ
|
|||
|
var operation = request.SendWebRequest();
|
|||
|
while (!operation.isDone)
|
|||
|
await Task.Yield(); // <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɣ<EFBFBD>ʹ<EFBFBD><CAB9>await<69>Է<EFBFBD><D4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>null
|
|||
|
Debug.LogError($"Error: {request.error}" + url);
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6>嵯<EFBFBD><E5B5AF><EFBFBD><EFBFBD>ʾ<EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD>뱻ע<EBB1BB>͵<EFBFBD><CDB5><EFBFBD>
|
|||
|
// Promptmgr.Instance.PromptBubble(request.error, Color.black,Color.red);
|
|||
|
return null;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>Cookie
|
|||
|
string setCookieHeader;
|
|||
|
string str = request.downloadHandler.text; // <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><D8B5>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
//Debug.Log(str); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><DDB5><EFBFBD><EFBFBD><EFBFBD>̨
|
|||
|
|
|||
|
// <20><><EFBFBD>Ի<EFBFBD>ȡ<EFBFBD><C8A1>Ӧͷ<D3A6>еġ<D0B5>Set-Cookie<69><65><EFBFBD>ֶ<EFBFBD>
|
|||
|
if (request.GetResponseHeaders().TryGetValue("Set-Cookie", out setCookieHeader))
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD>Set-Cookie<69><65><EFBFBD>ֶε<D6B6><CEB5><EFBFBD><EFBFBD>ݰ<EFBFBD><DDB0>ֺŷָ<C5B7>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
string[] cookies = setCookieHeader.Split(';');
|
|||
|
foreach (var cookie in cookies)
|
|||
|
{
|
|||
|
// <20><>ÿ<EFBFBD><C3BF>cookie<69><65>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>=ֵ<><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ
|
|||
|
var cookieParts = cookie.Split('=');
|
|||
|
if (cookieParts.Length == 2)
|
|||
|
{
|
|||
|
string key = cookieParts[0].Trim(); // ȥ<><C8A5><EFBFBD><EFBFBD><EFBFBD>Ŀո<C4BF>
|
|||
|
string value = cookieParts[1].Trim(); // ȥ<><C8A5>ֵ<EFBFBD>Ŀո<C4BF>
|
|||
|
// <20>洢<EFBFBD><E6B4A2>cookieJar<61>У<EFBFBD><D0A3>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>
|
|||
|
cookieJar[key] = value;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD>ݣ<EFBFBD><DDA3>ı<EFBFBD><C4B1><EFBFBD>ʽ<EFBFBD><CABD>
|
|||
|
return request.downloadHandler.text;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|