Unity 基础 之 UnityWebRequest Post 的简单封装使用
一、简单介绍
Unity中的一些知识点整理。
本节简单介绍在Unity开发中的,使用 UnityWebRequest,进行 Post 请求,值得注意的是,根据不同的请求头,注意传入不同的数据格式就好,如果你有新的方式也可以留言,多谢。
具体可参见 Unity 官网 UnityWebRequest 介绍
Unity - Manual: UnityWebRequest
二、实现原理
1、UnityWebRequest(url, "POST") 进行 Post 请求
2、UnityWebRequest.uploadHandler = new UploadHandlerRaw(postBytes); //添加数据
UnityWebRequest.downloadHandler = new DownloadHandlerBuffer(); // 数据Buffer
UnityWebRequest.SetRequestHeader("Content-Type", requestHeaderStr);// 请求头设置
三、注意事项
1、注意请求头设置不同,传入的数据格式会有所区别
2、注意字符串转为字节数组
四、效果预览
五、实现步骤
1、打开Unity,新建工程
2、编写脚本,实现 UnityWebRequest Post 功能
3、把 测试脚本挂载场景中
4、运行场景,效果如上
六、关键代码
1、TestUnityWebRequestPostSimpleWrapper
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace XANUtils.TestSpace
{
public class TestUnityWebRequestPostSimpleWrapper : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Test();
}
void Test() {
UnityWebRequestPostSimpleWrapper postSimpleWrapper = new UnityWebRequestPostSimpleWrapper(this);
//请求头 Form 表单格式 Post
postSimpleWrapper.StartUnityWebRequestPost("你的网址",
"你的类似格式数据:password=12345", // 更加请求头不同,Post Data 数据不同
"application/x-www-form-urlencoded", // form 格式
(isNotError,responseStr)=> {
if (isNotError)
{
Debug.Log(GetType() + "/Test()/ responseStr : " + responseStr);
Debug.Log(GetType() + "/Test()/ 解析 responseStr ... " );
}
else {
Debug.Log(GetType() + "/Test()/ error : " + responseStr);
Debug.Log(GetType() + "/Test()/ 针对报错,检查处理 ... ");
}
});
//请求头 Json 表单格式 Post
postSimpleWrapper.StartUnityWebRequestPost("你的网址",
"你的类似格式数据:{\"password\":\"12345\"}", // 更加请求头不同,Post Data 数据不同
"application/json", // json 格式
(isNotError, responseStr) => {
if (isNotError)
{
Debug.Log(GetType() + "/Test()/ responseStr : " + responseStr);
Debug.Log(GetType() + "/Test()/ 解析 responseStr ... ");
}
else
{
Debug.Log(GetType() + "/Test()/ error : " + responseStr);
Debug.Log(GetType() + "/Test()/ 针对报错,检查处理 ... ");
}
});
}
}
}
2、UnityWebRequestPostSimpleWrapper
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
namespace XANUtils {
public class UnityWebRequestPostSimpleWrapper
{
private MonoBehaviour mMono;
public UnityWebRequestPostSimpleWrapper(MonoBehaviour mono) {
mMono = mono;
}
public Coroutine StartUnityWebRequestPost(string url, string postData, string requestHeaderStr, Action<bool, string> onPostCallback) {
if (mMono==null)
{
Debug.LogError(GetType()+ "/StartUnityWebRequestPost()/ mMono can not be null");
return null;
}
return mMono.StartCoroutine(UnityWebRequestPost(url, postData, requestHeaderStr, onPostCallback));
}
IEnumerator UnityWebRequestPost(string url, string postData,string requestHeaderStr, Action<bool,string> onPostCallback)
{
using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
{
Debug.Log(GetType() + "/UnityWebRequestPost()/ url : " + webRequest.url);
Debug.Log(GetType() + "/UnityWebRequestPost()/ postData : " + postData);
byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(postData);
webRequest.uploadHandler = new UploadHandlerRaw(postBytes);
webRequest.downloadHandler = new DownloadHandlerBuffer();
webRequest.SetRequestHeader("Content-Type", requestHeaderStr);
yield return webRequest.SendWebRequest();
string response = null;
if (webRequest.isNetworkError || webRequest.isHttpError )
{
response = webRequest.error;
Debug.LogError(GetType() + "/UnityWebRequestPost()/ error : " + webRequest.error);
if (onPostCallback != null)
{
onPostCallback.Invoke(false, response);
}
}
else
{
response = webRequest.downloadHandler.text;
Debug.Log(GetType() + "/UnityWebRequestPost()/ success : " + response);
if (onPostCallback != null)
{
onPostCallback.Invoke(true, response);
}
}
}
}
}
}