Unity 使用UnityWebRequest进行Get/Post请求

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

Unity 使用UnityWebRequest进行Get/Post请求

1、请求服务器链接

using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
public class URL : MonoBehaviour
{
   
   
    string GetURL = "http://edu.xiaotu.cn/index/Unityapi/getUserInfo";
    string PostURL = "http://edu.xiaotu.cn/index/Unityapi/sendCourseInfo";

    private void Start()
    {
   
   
      //  StartCoroutine(Get_Demo());
        StartCoroutine(Post_Demo());
    }
    public IEnumerator Get_Demo()
    {
   
   
        UnityWebRequest webRequest = UnityWebRequest.Get(GetURL);
        //发送请求
        yield return webRequest.SendWebRequest();
        //等待请求完成
        while (!webRequest.isDone)
        {
   
   
            yield return null;
        }
        if (string.IsNullOrEmpty(webRequest.error))
        {
   
   //Get的请求成功//Get请求的返回参数
            var data = webRequest.downloadHandler.text;
            Debug.Log(data);
            Debug.Log("成功");

            UserInfo sg;
            sg = JsonUtility.FromJson<UserInfo>(data);

            Debug.Log(sg.uid+sg.err_msg);
            
        }
        else
        {
   
   //Get的请求失败
            Debug.Log("失败");
        }
    }

    public IEnumerator Post_Demo()
    {
   
   
        //Post请求的参数
        WWWForm form = new WWWForm();
        form.AddField("uid", "tea10000");
        form.AddField("course_id", "N700003");
        form.AddField("course_duration", "121212");
        form.AddField("course_score", "80");
        UnityWebRequest webRequest = UnityWebRequest.Post(PostURL, form);
        //发送请求
        yield return webRequest.SendWebRequest();
        if (string.IsNullOrEmpty(webRequest.error))
        {
   
   //Post的请求成功 //Post请求的返回参数
            var data = webRequest.downloadHandler.text;
              Debug.Log("成功:"+data);        }
        else
        {
   
   //Post的请求失败
     

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Ke-Di

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

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

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

打赏作者

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

抵扣说明:

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

余额充值