结合:https://www.cnblogs.com/guangzhiruijie/p/16985533.html
unity自带的UnityWebRequest提供了构成 HTTP 请求和处理 HTTP 响应。构造函数:
public UnityWebRequest(); public UnityWebRequest(Uri uri); public UnityWebRequest(string url); public UnityWebRequest(Uri uri, string method); public UnityWebRequest(string url, string method); public UnityWebRequest(string url, string method, DownloadHandler downloadHandler, UploadHandler uploadHandler); public UnityWebRequest(Uri uri, string method, DownloadHandler downloadHandler, UploadHandler uploadHandler);简单的例子:
using FF = StarEngineTec.Generic.FormatFunc;
string url="";//请求地址
string jsonData="";//需要上传的json数据
IEnumerator UploadPost()
{
UnityWebRequest uwr = new UnityWebRequest(url, "POST");
uwr.uploadHandler = new UploadHandlerRaw(FF.StringToBytes(jsonData));
uwr.downloadHandler = new DownloadHandlerBuffer();
//如果有请求头,根据具体字段要求设置
uwr.SetRequestHeader("Content-Type", "application/json");
uwr.SetRequestHeader("app-version", "V1");
yield return uwr.SendWebRequest();
if (uwr.isHttpError || uwr.isNetworkError)
{
Debug.LogError("Login Error: " + uwr.error);
}
}
其他常用方法:
public static UnityWebRequest Post(Uri uri, Dictionary<string, string> formFields); public static UnityWebRequest Post(Uri uri, WWWForm formData); public static UnityWebRequest Post(string uri, WWWForm formData); public static UnityWebRequest Post(Uri uri, string postData); public static UnityWebRequest Post(string uri, string postData); public static UnityWebRequest Post(string uri, Dictionary<string, string> formFields);IEnumerator Upload(string str)
{
WWWForm form = new WWWForm();
form.AddField("data", str);
UnityWebRequest webRequest = UnityWebRequest.Post(url, form);
yield return webRequest.SendWebRequest();
if (webRequest.isHttpError || webRequest.isNetworkError)
Debug.Log(webRequest.error);
else
Debug.Log(webRequest.downloadHandler.text);
}
一般上传完数据会返回条消息,可以根据code值判断成功与否,以及请求失败的原因
Serializable特性表示该类可序列化,否则无法进行序列化
设置请求头header的Content-Type,它有四种类型: application/x-www-form-urlencoded(默认) application/xml application/json multipart/form-data 由于该接口所传的参数为json格式,所以需要设置为application/json,否则导致报错:HTTP/1.1 415 Unsupported Media Type,下面封装发起网络请求的携程函数:/// <summary>
/// 确认过闸
/// </summary>
[Serializable]
public class Affirm
{
public string id;
public string inId;
public string time;
public string batchCode;
}
public IEnumerator SendWebRequest()
{
//接口地址
string url = "http://**.**.***.***:****/********/*****";
//post数据 通过序列化获得字符串
string postData = JsonMapper.ToJson(new Affirm());
//Post网络请求
using (UnityWebRequest request = UnityWebRequest.Post(url, UnityWebRequest.kHttpVerbPOST))
{
byte[] postBytes = Encoding.UTF8.GetBytes(postData);
request.uploadHandler = new UploadHandlerRaw(postBytes);
request.downloadHandler = new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
yield return request.SendWebRequest();
if (!request.isNetworkError && !request.isHttpError)
{
Debug.Log(response);
}
else
{
Debug.LogError($"发起网络请求失败: 确认过闸接口 -{request.error}");
}
}
}
当后端返回数据时,通过反序列化得到我们所需的Response类
[Serializable]
public class AffirmResponse
{
public string msg;
public int code;
public bool hit;
public List<Affirm> data;
}
public IEnumerator SendWebRequest()
{
//接口地址
string url = "http://**.**.***.***:****/********/*****";
//post数据 通过序列化获得字符串
string postData = JsonMapper.ToJson(new Affirm());
//Post网络请求
using (UnityWebRequest request = UnityWebRequest.Post(url, UnityWebRequest.kHttpVerbPOST))
{
byte[] postBytes = Encoding.UTF8.GetBytes(postData);
request.uploadHandler = new UploadHandlerRaw(postBytes);
request.downloadHandler = new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
yield return request.SendWebRequest();
if (!request.isNetworkError && !request.isHttpError)
{
//反序列化
var response = JsonMapper.ToObject<AffirmResponse>(request.downloadHandler.text);
Debug.Log($"Code:{response.code} Msg:{response.msg} Hit:{response.hit}");
}
else
{
Debug.LogError($"发起网络请求失败: 确认过闸接口 -{request.error}");
}
}
}
标签:HTTP,string,request,public,unity,xorpay,new,Post,UnityWebRequest From: https://www.cnblogs.com/guangzhiruijie/p/17294925.html