unity Post与Get请求
POST请求:
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
public class MainProcess : MonoBehaviour
{
string token = "";//登陆成功后token值
System.TimeSpan st;//时间戳
private void GenerateTimestamp()
{
st = System.DateTime.UtcNow - new System.DateTime(1970, 1, 1, 0, 0, 0);//获取时间戳Debug.Log(Convert.ToInt64(st.TotalSeconds) );//时间戳转换Int64
}
IEnumerator post_Request(string url)
{
//传递信息
WWWForm form = new WWWForm();
form.AddField("type", 0);
form.AddField("score", 100);
form.AddField("remark", "无");
form.AddField("progress", "1001");
form.AddField("operationTimes", st);
UnityWebRequest www = UnityWebRequest.Post(url, form);
//请求添加token验证
www.SetRequestHeader("admin-token", token );
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
Debug.Log("Form upload complete!");
Debug.Log(www.downloadHandler);
Debug.Log(www.downloadHandler.text);
}
}
}
POST请求2:
public class ReqData
{
public string id;
public string score;
public string date;
}
public void Post_Send(string _id,string _score)
{
string url = "http://ht.shiwi.cn/api/score/setScore";
ReqData _reqData = new ReqData();
_reqData.id = _id;
_reqData.score = _score;
//_reqData.date = "2021-05-22 00:00:00";
_reqData.date = string.Concat(System.DateTime.Today.Year.ToString() + "-" + System.DateTime.Today.Month.ToString() + "-" +
System.DateTime.Today.Day.ToString() + " " + System.DateTime.Now.Hour + ":" + System.DateTime.Now.Minute + ":00");
string json = JsonUtility.ToJson(_reqData);
StartCoroutine(post(url, json));
}
IEnumerator post(string url,string json)
{
UnityWebRequest uwr = new UnityWebRequest($"{url}", UnityWebRequest.kHttpVerbPOST);
uwr.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(json));
uwr.SetRequestHeader("Content-Type", "application/json");
yield return uwr.SendWebRequest();
if (uwr.isDone && !uwr.isHttpError && string.IsNullOrEmpty(uwr.error))
{
Debug.Log("success");
}
else
{
Debug.Log("failed");
Debug.Log(uwr.error);
}
}
Get请求:
//_url就是你对应服务器接口的请求链接
IEnumerator get_Request(string _url,string _token)
{
UnityWebRequest www = UnityWebRequest.Get(_url);
//请求添加token验证
www.SetRequestHeader("admin-token", _token);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
Debug.Log("获取成功!");
Debug.Log(www.downloadHandler);
Debug.Log(www.downloadHandler.text);
}
}
Get请求需要带参数时,需要在url后进行拼接 。如 请求参数如下图时,给出的请求链接后面
使用"?参数名=参数值&参数名=参数值"形式;
例: url="www.baidu.com?page=1&type=0&sn=480800";
Get请求2:
public void getUserinf()
{
string url = "http://121.43.135.175:8080/userinf/getOne?username=mahuateng&password=mahuateng123";
StartCoroutine(get(url, (_data) =>
{
data = _data;
Debug.Log(data);
}));
}
public static IEnumerator get(string path, UnityAction<string> callback)
{
UnityWebRequest req = new UnityWebRequest(path);
req.downloadHandler = new DownloadHandlerBuffer();
yield return req.SendWebRequest();
if (req.isHttpError || req.isHttpError)
{
Debug.LogError("----" + path);
}
else
{
callback(req.downloadHandler.text);
}
}