2025-02-12 08:43:33 +08:00

100 lines
4.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Networking;
/********************************************************************************
*Create By CG
*Function 利用Get和Post方式进行网络请求
*********************************************************************************/
namespace ZXK.UTility
{
public class WebConnCtrl : MonoBehaviour
{
/// <summary>
/// Post发送json数据
/// </summary>
/// <param name="url"></param>
/// <param name="postData">Json数据 </param>
/// <param name="ac">回调信息</param>
/// <returns></returns>
public static IEnumerator SendPostRequestWithData(string url, string postData, UnityAction<string> ac = null)
{
Debug.Log("POST请求中。。。Url:" + url);
Debug.Log("数据。。。:" + postData);
using (UnityWebRequest requestPost = new UnityWebRequest(url, "POST"))
{
byte[] postBytes = Encoding.UTF8.GetBytes(postData);
requestPost.uploadHandler = new UploadHandlerRaw(postBytes);
requestPost.downloadHandler = new DownloadHandlerBuffer();
requestPost.SetRequestHeader("Content-Type", "application/json");
yield return requestPost.SendWebRequest();
if (requestPost.isDone && requestPost.result!=UnityWebRequest.Result.ProtocolError)
{
DownloadHandler downloadHandler_TXT = requestPost.downloadHandler;
Debug.Log(url + "。。。请求完成");
if (ac != null) ac(downloadHandler_TXT.text);
}
else
{
Debug.Log(url + " ERROR " + requestPost.error);
}
}
}
/// <summary>
/// 发送的POST请求提供请求成功后针对返回值的回调接口
/// </summary>
/// <param name="url">POST请求的路径</param>
/// <param name="ac">返回值处理接口</param>
/// <returns></returns>
public static IEnumerator SendPostRequest(string url, UnityAction<string> ac = null)
{
Debug.Log("POST请求中。。。Url:" + url);
using(UnityWebRequest requestPost = new UnityWebRequest(url, "POST"))
{
requestPost.downloadHandler = new DownloadHandlerBuffer();
yield return requestPost.SendWebRequest();
if (requestPost.isDone && requestPost.result != UnityWebRequest.Result.ProtocolError)
{
DownloadHandler downloadHandler_TXT = requestPost.downloadHandler;
Debug.Log(url + "。。。请求完成");
if (ac != null) ac(downloadHandler_TXT.text);
}
else
{
Debug.Log(url + " ERROR " + requestPost.error);
}
}
}
/// <summary>
/// 发送的GET请求提供请求成功后针对返回值的回调接口
/// </summary>
/// <param name="uriData">Get请求</param>
/// <param name="ac">返回值处理接口</param>
/// <returns></returns>
public static IEnumerator SendGetRequestWithValue(string uriData, UnityAction<string> ac = null)
{
Debug.Log("GET请求中。。。Url:" + uriData);
using (UnityWebRequest requestGet = UnityWebRequest.Get(uriData))
{
yield return requestGet.SendWebRequest();
if (requestGet.result == UnityWebRequest.Result.ProtocolError)
{
Debug.Log("————请求失败————");
Debug.Log(requestGet.downloadHandler.text);
}
else
{
Debug.Log("———请求成功———");
Debug.Log(requestGet.downloadHandler.text);
if (ac != null)
ac(requestGet.downloadHandler.text);
}
}
}
}
}