SDK_UnityMoney/Assets/EFSDK/Editor/SDKEditorNetworkTool.cs

65 lines
1.5 KiB
C#
Executable File

using System;
using System.Net;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
public class SDKEditorNetworkTool
{
public static string GetText(string url)
{
try
{
using (UnityWebRequest www = UnityWebRequest.Get(url))
{
www.timeout = 10; // 设置超时时间为10秒
www.SendWebRequest(); // 同步发送请求
while (www.isDone == false)
{
}
if (www.result != UnityWebRequest.Result.Success)
{
Debug.LogError($"Request failed: {www.error}");
return "";
}
if (www.responseCode != 200)
{
Debug.LogWarning($"Unexpected status code: {www.responseCode}");
return "";
}
return www.downloadHandler.text;
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
return "";
}
public static string HttpGetText(string url)
{
try
{
using (WebClient client = new WebClient())
{
client.Encoding = Encoding.UTF8;
return client.DownloadString(url);
}
}
catch (Exception e)
{
UnityEngine.Debug.LogError($"[NetworkTool] Exception: {e}");
return "";
}
}
}