Unity发送Post请求

 

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);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
要使用Unity发送POST请求给SDWebUI,你需要使用Unity的WebRequest类或HttpClient库来发送HTTP请求。以下是一个简单的示例代码,可以向SDWebUI发送一个POST请求并获取响应: ```csharp using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Net; using System.IO; using System.Text; public class SDWebUIExample : MonoBehaviour { public string url = "http://your-api-url.com"; public string postData = "param1=value1&param2=value2"; IEnumerator Start() { // 创建Web请求对象 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); // 设置请求方法为POST request.Method = "POST"; // 设置请求参数 byte[] byteArray = Encoding.UTF8.GetBytes(postData); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = byteArray.Length; Stream dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); // 发送请求 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // 读取响应数据 Stream receiveStream = response.GetResponseStream(); StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8); string responseData = readStream.ReadToEnd(); // 关闭响应流 response.Close(); readStream.Close(); // 输出响应数据 Debug.Log(responseData); yield return null; } } ``` 你需要将 `url` 变量替换为你的API地址,并根据API文档设置请求方法和参数。在设置请求参数时,需要将参数转换为byte数组,并设置请求头的Content-Type为 `application/x-www-form-urlencoded`。注意,在发送请求时,你需要在Unity的主线程中使用协程(Coroutine)来避免阻塞UI线程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陌上桑AGO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值