52 lines
1.7 KiB
C#
52 lines
1.7 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.IO;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
|
|
namespace HK
|
|
{
|
|
public class NetManager
|
|
{
|
|
public string url = "http://192.168.3.189:8000/api/v1/orders/";
|
|
|
|
private void Awake()
|
|
{
|
|
url = File.ReadAllText(Application.streamingAssetsPath + "/HttpsURL.txt");
|
|
}
|
|
|
|
public void SendRequest(string json, Action<bool> callback)
|
|
{
|
|
var path = Application.streamingAssetsPath + "/httpdata" + DateTime.Now.ToString("yyyyMMddHHmmssfff") +
|
|
".txt";
|
|
File.CreateText(path).Dispose();
|
|
File.WriteAllText(path, json);
|
|
// StartCoroutine(Request(json, callback));
|
|
}
|
|
|
|
IEnumerator Request(string json, Action<bool> callback)
|
|
{
|
|
using (UnityWebRequest request = new UnityWebRequest(url, "POST"))
|
|
{
|
|
byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(json);
|
|
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
|
|
request.downloadHandler = new DownloadHandlerBuffer();
|
|
request.SetRequestHeader("Content-Type", "application/json");
|
|
|
|
yield return request.SendWebRequest();
|
|
|
|
if (request.isNetworkError || request.isHttpError)
|
|
{
|
|
Debug.LogError("Error sending order: " + request.error);
|
|
callback?.Invoke(false);
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("Order sent successfully: " + request.downloadHandler.text);
|
|
callback?.Invoke(true);
|
|
// 执行打印操作
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |