using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
public class NetWorkHttp : MonoBehaviour
{
private static NetWorkHttp instance;
public static NetWorkHttp Instance
{
get
{
if (instance == null)
{
GameObject go = new GameObject("NetWorkHttp");
DontDestroyOnLoad(go);
instance = go.CreateOrAddComponent<NetWorkHttp>();
}
return instance;
}
}
public void Get(string url)
{
UnityWebRequest unityWebRequest = UnityWebRequest.Get(url);
StartCoroutine(GetUrl(unityWebRequest));
}
private IEnumerator GetUrl(UnityWebRequest unityWebRequest)
{
yield return unityWebRequest.SendWebRequest();
if (unityWebRequest.isHttpError || unityWebRequest.isNetworkError)
Debug.Log(unityWebRequest.error);
else
{
Debug.Log(unityWebRequest.downloadHandler.text);
AccountEntity entity = JsonUtility.FromJson<AccountEntity>(unityWebRequest.downloadHandler.text);
Debug.Log(entity.pwd);
}
}
private IEnumerator GetPost(UnityWebRequest unityWebRequest)
{
yield return unityWebRequest.SendWebRequest();
if (unityWebRequest.isHttpError || unityWebRequest.isNetworkError)
Debug.Log(unityWebRequest.error);
else
{
Debug.Log(unityWebRequest.downloadHandler.text);
}
}
public void Post(string url)
{
WWWForm wWWForm = new WWWForm();
AccountEntity entity = new AccountEntity { account = "zzzsss123", pwd = "sssss123" };
Debug.Log(JsonUtility.ToJson(entity));
wWWForm.AddField("", JsonUtility.ToJson(entity));
UnityWebRequest unityWebRequest = UnityWebRequest.Post(url,wWWForm);
StartCoroutine(GetPost(unityWebRequest));
}
}
Unity UnityWebRequest 进行Get和Post请求
最新推荐文章于 2024-05-23 13:11:51 发布
关键词由CSDN通过智能技术生成