202 lines
6.1 KiB
C#
202 lines
6.1 KiB
C#
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
|
|
public static class InterfaceManager
|
|
{
|
|
private static string _IP = "172.16.1.127";
|
|
public static string IP
|
|
{
|
|
get { return _IP; }
|
|
set
|
|
{
|
|
if (_IP != value)
|
|
{
|
|
_IP = value;
|
|
}
|
|
}
|
|
}
|
|
private static string _Port = "9206";
|
|
public static string Port
|
|
{
|
|
get { return _Port; }
|
|
set
|
|
{
|
|
if (_Port != value)
|
|
{
|
|
_Port = value;
|
|
}
|
|
}
|
|
}
|
|
public static string IpAddress { get => string.Format("http://{0}:{1}", IP, Port); }
|
|
/// <summary>
|
|
/// 获取试卷接口
|
|
/// </summary>
|
|
public static string Performancecontract { get => IpAddress + "/proSimulationExamination/queryById?"; }
|
|
/// <summary>
|
|
/// 提交试卷
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static string Submittheperformanceexam { get => IpAddress + "/pro/score/simulationScore"; }
|
|
public static string GetLocalTxt(string path)
|
|
{
|
|
using (System.IO.StreamReader reader = new System.IO.StreamReader(path)) { return reader.ReadToEnd(); }
|
|
}
|
|
|
|
public static IEnumerator Getbytes(string url, Action<string> callback)
|
|
{
|
|
using (UnityWebRequest www = new UnityWebRequest(url))
|
|
{
|
|
yield return www.SendWebRequest();
|
|
if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.ProtocolError)
|
|
{
|
|
callback(null);
|
|
Debug.LogError("网络请求失败了");
|
|
}
|
|
else
|
|
{
|
|
if (!string.IsNullOrEmpty(www.downloadHandler.text))
|
|
{
|
|
callback(www.downloadHandler.text);
|
|
}
|
|
else
|
|
{
|
|
callback(null);
|
|
Debug.Log("没有获取到数据");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get网络请求
|
|
/// </summary>
|
|
/// <param name="url">路径</param>
|
|
/// <param name="callback">方法回调</param>
|
|
/// <returns></returns>
|
|
public static IEnumerator Getstring(string url, Action<string> callback)
|
|
{
|
|
using (UnityWebRequest www = UnityWebRequest.Get(url))
|
|
{
|
|
yield return www.SendWebRequest();
|
|
if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.ProtocolError)
|
|
{
|
|
callback(null);
|
|
Debug.LogError("网络请求失败了");
|
|
}
|
|
else
|
|
{
|
|
if (!string.IsNullOrEmpty(www.downloadHandler.text))
|
|
{
|
|
callback(www.downloadHandler.text);
|
|
}
|
|
else
|
|
{
|
|
callback(null);
|
|
Debug.Log("没有获取到数据");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Get每隔一段时间请求一下
|
|
/// </summary>
|
|
/// <param name="url">请求路径</param>
|
|
/// <param name="tiem">每隔多长时间请求一下</param>
|
|
/// <param name="callback">回调方法</param>
|
|
/// <returns></returns>
|
|
public static IEnumerator Getstring(string url, int tiem, Action<string> callback)
|
|
{
|
|
while (true)
|
|
{
|
|
using (UnityWebRequest www = UnityWebRequest.Get(url))
|
|
{
|
|
yield return new WaitForSeconds(tiem);
|
|
yield return www.SendWebRequest();
|
|
if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.ProtocolError)
|
|
{
|
|
callback(null);
|
|
Debug.LogError("网络请求失败了");
|
|
}
|
|
else
|
|
{
|
|
if (!string.IsNullOrEmpty(www.downloadHandler.text))
|
|
{
|
|
callback(www.downloadHandler.text);
|
|
}
|
|
else
|
|
{
|
|
callback(null);
|
|
Debug.Log("没有请求到数据");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// post的请求接口
|
|
/// </summary>
|
|
/// <param name="url">路径</param>
|
|
/// <param name="keyvaluepir">字典方式传入值</param>
|
|
/// <param name="callback">回调方法</param>
|
|
/// <returns></returns>
|
|
public static IEnumerator Poststring(string url, Dictionary<string, string> keyvaluepir = null, Action<bool, string> callback = null)
|
|
{
|
|
WWWForm wWWForm = new WWWForm();
|
|
|
|
foreach (var item in keyvaluepir)
|
|
{
|
|
wWWForm.AddField(item.Key, item.Value);
|
|
}
|
|
UnityWebRequest www = UnityWebRequest.Post(url, wWWForm);
|
|
yield return www.SendWebRequest();
|
|
if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.ProtocolError)
|
|
{
|
|
callback(false, null);
|
|
Debug.LogError("网络请求失败了");
|
|
}
|
|
else
|
|
{
|
|
if (!string.IsNullOrEmpty(www.downloadHandler.text))
|
|
{
|
|
callback(true, www.downloadHandler.text);
|
|
Debug.Log(www.downloadHandler.text);
|
|
}
|
|
else
|
|
{
|
|
callback(false, null);
|
|
Debug.Log("没有请求到数据");
|
|
}
|
|
}
|
|
}
|
|
public static IEnumerator PoststringByRaw(string url,string json)
|
|
{
|
|
UnityWebRequest request = new UnityWebRequest(url, "POST");
|
|
|
|
byte[] bodyData = Encoding.ASCII.GetBytes(json);
|
|
request.SetRequestHeader("Content-Type", "application/json");
|
|
request.uploadHandler = new UploadHandlerRaw(bodyData);
|
|
request.downloadHandler = new DownloadHandlerBuffer();
|
|
yield return request.SendWebRequest();
|
|
|
|
if (request.result != UnityWebRequest.Result.Success)
|
|
{
|
|
Debug.LogError("Error: " + request.error);
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("Response: " + request.downloadHandler.text);
|
|
}
|
|
|
|
request.Dispose();
|
|
}
|
|
|
|
|
|
}
|