首页 > 其他分享 >unity3d get post请求

unity3d get post请求

时间:2024-07-18 18:19:20浏览次数:9  
标签:unity3d return GetError get request result post UnityWebRequest Result

unity3d get  post请求

 

using UnityEngine;
using UnityEngine.Networking;

public class NetworkRequestExample : MonoBehaviour
{
    IEnumerator Start()
    {
        string url = "https://api.example.com/data";

        UnityWebRequest request = UnityWebRequest.Get(url);

        yield return request.SendWebRequest();

        if (request.result != UnityWebRequest.Result.Success)
        {
            Debug.Log(ErrorHelper.GetError(request.result) + ": " + request.error);
        }
        else
        {
            // 显示结果,这里假设服务器返回的是JSON格式的数据
            Debug.Log("Response: " + request.downloadHandler.text);

            // 处理响应数据...
        }
    }
}

// 一个简单的帮助类来处理UnityWebRequest的错误
public static class ErrorHelper
{
    public static string GetError(UnityWebRequest.Result result)
    {
        switch (result)
        {
            case UnityWebRequest.Result.ProtocolError:
                return "Protocol Error: " + UnityWebRequest.GetError(result);
            case UnityWebRequest.Result.ConnectionError:
                return "Connection Error: " + UnityWebRequest.GetError(result);
            case UnityWebRequest.Result.DataProcessingError:
                return "Data Processing Error: " + UnityWebRequest.GetError(result);
            default:
                return "Unknown Error";
        }
    }
}

 

 

 

IEnumerator Start()
{
    string url = "https://api.example.com/postdata";
    byte[] bodyRaw = Encoding.UTF8.GetBytes("key1=value1&key2=value2");

    UnityWebRequest request = new UnityWebRequest(url, "POST");
    byte[] body = bodyRaw;
    request.uploadHandler = (UploadHandler)new UploadHandlerRaw(body);
    request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
    request.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    yield return request.SendWebRequest();

    if (request.result != UnityWebRequest.Result.Success)
    {
        Debug.Log(ErrorHelper.GetError(request.result) + ": " + request.error);
    }
    else
    {
        Debug.Log("Form upload complete!");
        // 处理响应...
    }
}

 

 

 

##############################

标签:unity3d,return,GetError,get,request,result,post,UnityWebRequest,Result
From: https://www.cnblogs.com/herd/p/18297429

相关文章

  • Load balancer does not contain an instance for the service service-B [503] duri
    场景:service-A服务通过openFeign远程调用service-B服务的test()方法,结果报错Loadbalancerdoesnotcontainaninstancefortheserviceservice-Bfeign.FeignException$ServiceUnavailable:[503]during[POST]to[http://service-B/test]原因:报错信息的意思......
  • Postgresql主键自增的方法
    Postgresql主键自增的方法一.方法(一)使用 serialPRIMARYKEY插入数据 二.方法(二)......
  • leetcode 1459 矩形面积(postgresql)
    需求表:Points±--------------±--------+|ColumnName|Type|±--------------±--------+|id|int||x_value|int||y_value|int|±--------------±--------+id是该表主键每个点都用二维坐标(x_value,y_value)表示写一个SQL语句,报告由表......
  • request请求方式(post请求)
    这里我们定义一个html的静态文件,模拟用户正常提交表单  业务代码实现:fromflaskimportFlask,requestapp=Flask(__name__)@app.route('/test2',methods=['POST'])deftest2():name=request.form.get('user_name')age=request.form.get('user_age&......
  • request请求方式(get请求)
    在flask框架中,可以定义路由请求方式利用methods参数可以自己指定一个接口的请求方式get:把参数放在url后面,参数使用字符方式传输,所以也叫明文传输post:表单提交,采用字节流方式传递数据,文件传输必须用post 这里我们定义一个静态文件: get请求参数代码实现:fromflaskimpo......
  • Python爬虫(5-10)-编解码、ajax的get请求、ajax的post请求、URLError/HTTPError、微博
    五、编解码(Unicode编码)(1)GET请求所提方法都在urllib.parse.路径下get请求的quote()方法(适用于只提交一两个参数值)url='http://www.baidu.com/baidu?ie=utf-8&wd='#对汉字进行unicode编码name=urllib.parse.quote('白敬亭')url+=nameget请求的urlencode()方法(适用于......
  • unity3d sqlite
     usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingMono.Data.Sqlite;//注意:这取决于你使用的SQLite库publicclassSQLiteExample:MonoBehaviour{//数据库文件路径privatestringdbPath="URI=file:"+Applicatio......
  • postgresql删除用户
    背景**角色与用户**:在PostgreSQL中,用户和组的概念是通过“角色”来统一实现的。角色可以有登录权限(在这种情况下,它们通常被称为“用户”),也可以没有(在这种情况下,它们通常用于权限管理,类似于组)。**依赖关系**:在删除角色之前,需要确保该角色没有被其他数据库对象(如表、视图、......
  • 在 PowerShell 中Get-WmiObject Win32_PhysicalMemory,SMBIOSMemoryType 是一种用于描
    在PowerShell中Get-WmiObjectWin32_PhysicalMemory,SMBIOSMemoryType是一种用于描述系统中物理内存类型的属性。数字26表示特定的内存类型,具体为DDR4内存。每种内存类型在SMBIOS(SystemManagementBIOS)规范中都有一个对应的数字码,用来标识不同类型的内存。以下是一些常见......
  • live555 rtsp服务器实战之doGetNextFrame
    live555关于RTSP协议交互流程live555的核心数据结构值之闭环双向链表live555rtsp服务器实战之createNewStreamSourcelive555rtsp服务器实战之doGetNextFrame概要live555用于实际项目开发时,createNewStreamSource和doGetNextFrame是必须要实现的两个虚函数,一般会创建两......