using System.Collections;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
public class PostMsg : MonoBehaviour
{
private string postUrl = "https://api.yeeloc.com/open-api/api-cgi/iot/lock/action";
string jsonData = "{\"sn\":\"bbbbb\",\"action\":\"lock\"}";//需要上传的json数据
byte[] bodyRaw;
[System.Obsolete]
void Update()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
StartCoroutine(WebPost());
}
}
[System.Obsolete]
IEnumerator WebPost(string msg)
{
UnityWebRequest uwr = new UnityWebRequest(postUrl, "POST");
bodyRaw = Encoding.UTF8.GetBytes(jsonData);
uwr.uploadHandler = new UploadHandlerRaw(bodyRaw);
uwr.downloadHandler = new DownloadHandlerBuffer();
//如果有请求头,根据具体字段要求设置
uwr.SetRequestHeader("Authorization", "请求头token");
uwr.SetRequestHeader("Content-Type", "application/json");//数据格式
yield return uwr.SendWebRequest();
if (uwr.isHttpError || uwr.isNetworkError)
{
Debug.LogError("Login Error: " + uwr.error);
}
else
{
string receiveContent = uwr.downloadHandler.text;
Debug.Log(receiveContent);
}
}
}