提交日志-20240710-2322:

1、新增接口请求方式配置
This commit is contained in:
Afeijia 2024-07-10 23:22:20 +08:00
parent a88bd2f25c
commit ac273c9a34
5 changed files with 254 additions and 17 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
@ -48,7 +50,7 @@ public static class RequestBase
public static IEnumerator Get(string _url, Dictionary<string, string> _quary_datas, Action<string, string> _callback)
{
using (UnityWebRequest webRequest = new UnityWebRequest())
if (ApiManager.Instance != null)
{
if (_quary_datas != null)
{
@ -62,31 +64,247 @@ public static class RequestBase
UnityEngine.Debug.Log(_url);
webRequest.url = _url;
webRequest.method = "GET";
webRequest.downloadHandler = new DownloadHandlerBuffer();
yield return webRequest.SendWebRequest();
switch (webRequest.result)
switch (ApiManager.method)
{
case UnityWebRequest.Result.InProgress:
case "0":
Debug.Log("method 0");
using (UnityWebRequest webRequest = new UnityWebRequest())
{
if (_quary_datas != null)
{
_url = _url + "?";
foreach (var item in _quary_datas)
{
_url = string.Format("{0}{1}={2}&", _url, item.Key, item.Value);
}
}
_url = _url.TrimEnd('&');
UnityEngine.Debug.Log(_url);
webRequest.url = _url;
webRequest.method = "GET";
webRequest.downloadHandler = new DownloadHandlerBuffer();
yield return webRequest.SendWebRequest();
switch (webRequest.result)
{
case UnityWebRequest.Result.InProgress:
break;
case UnityWebRequest.Result.Success:
_callback?.Invoke(webRequest.downloadHandler.text, null);
break;
case UnityWebRequest.Result.ConnectionError:
_callback?.Invoke(null, webRequest.error);
break;
case UnityWebRequest.Result.ProtocolError:
_callback?.Invoke(null, webRequest.error);
break;
case UnityWebRequest.Result.DataProcessingError:
_callback?.Invoke(null, webRequest.error);
break;
default:
break;
}
}
break;
case UnityWebRequest.Result.Success:
_callback?.Invoke(webRequest.downloadHandler.text, null);
case "1":
Debug.Log("method 1");
UnityWebRequest _request_1 = new UnityWebRequest(_url, "GET");
_request_1.downloadHandler = new DownloadHandlerBuffer();
yield return _request_1.SendWebRequest();
if (_request_1.isDone)
{
if (_request_1.isHttpError || _request_1.isNetworkError)
{
Debug.LogError(_request_1.error);
_callback?.Invoke(null, _request_1.error);
}
else
{
Debug.Log(_request_1.downloadHandler.text);
_callback?.Invoke(_request_1.downloadHandler.text, null);
}
}
break;
case UnityWebRequest.Result.ConnectionError:
_callback?.Invoke(null, webRequest.error);
case "2":
Debug.Log("method 2");
UnityWebRequest _request_2 = UnityWebRequest.Get(_url);
yield return _request_2.SendWebRequest();
if (_request_2.isDone)
{
if (_request_2.isHttpError || _request_2.isNetworkError)
{
Debug.LogError(_request_2.error);
_callback?.Invoke(null,_request_2.error);
}
else
{
Debug.Log(_request_2.downloadHandler.text);
_callback?.Invoke( _request_2.downloadHandler.text, null);
}
}
break;
case UnityWebRequest.Result.ProtocolError:
_callback?.Invoke(null, webRequest.error);
case "3":
Debug.Log("method 3");
WWW www = new WWW(_url);
yield return www;
if (www.isDone)
{
if (www.error != null)
{
Debug.LogError(www.error);
_callback?.Invoke(www.error, null);
}
else
{
Debug.Log(www.text);
_callback?.Invoke( www.text, null);
}
}
break;
case UnityWebRequest.Result.DataProcessingError:
_callback?.Invoke(null, webRequest.error);
case "4":
Debug.Log("method 4");
HttpWebRequest _request_4 = (HttpWebRequest)WebRequest.Create(_url);
_request_4.Method = "GET";
// 启动异步请求
_request_4.BeginGetResponse(asyncResult =>
{
// 获取HttpWebRequest对象
HttpWebRequest myRequest = (HttpWebRequest)asyncResult.AsyncState;
// 结束异步操作,获取响应
HttpWebResponse response = (HttpWebResponse)myRequest.EndGetResponse(asyncResult);
// 开始异步读取响应流
System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream());
string responseData = reader.ReadToEnd();
_callback?.Invoke( responseData,null);
reader.Close();
response.GetResponseStream().Close();
response.Close();
}, _request_4);
break;
case "5":
Debug.Log("method 5");
Stream instream = null;
StreamReader sr = null;
HttpWebResponse response = null;
HttpWebRequest request = null;
Encoding encoding = Encoding.UTF8;
// 准备请求...
// 设置参数
request = WebRequest.Create(_url) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
//默认值TimeOut100 秒
//发送请求并获取相应回应数据
response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
instream = response.GetResponseStream();
sr = new StreamReader(instream, encoding);
//返回结果网页html代码
string content = sr.ReadToEnd();
string err = string.Empty;
_callback?.Invoke(content, null);
break;
case "6":
Debug.Log("method 6");
break;
default:
using (UnityWebRequest webRequest = new UnityWebRequest())
{
if (_quary_datas != null)
{
_url = _url + "?";
foreach (var item in _quary_datas)
{
_url = string.Format("{0}{1}={2}&", _url, item.Key, item.Value);
}
}
_url = _url.TrimEnd('&');
UnityEngine.Debug.Log(_url);
webRequest.url = _url;
webRequest.method = "GET";
webRequest.downloadHandler = new DownloadHandlerBuffer();
yield return webRequest.SendWebRequest();
switch (webRequest.result)
{
case UnityWebRequest.Result.InProgress:
break;
case UnityWebRequest.Result.Success:
_callback?.Invoke(webRequest.downloadHandler.text, null);
break;
case UnityWebRequest.Result.ConnectionError:
_callback?.Invoke(null, webRequest.error);
break;
case UnityWebRequest.Result.ProtocolError:
_callback?.Invoke(null, webRequest.error);
break;
case UnityWebRequest.Result.DataProcessingError:
_callback?.Invoke(null, webRequest.error);
break;
default:
break;
}
}
break;
}
}
else
{
using (UnityWebRequest webRequest = new UnityWebRequest())
{
if (_quary_datas != null)
{
_url = _url + "?";
foreach (var item in _quary_datas)
{
_url = string.Format("{0}{1}={2}&", _url, item.Key, item.Value);
}
}
_url = _url.TrimEnd('&');
UnityEngine.Debug.Log(_url);
webRequest.url = _url;
webRequest.method = "GET";
webRequest.downloadHandler = new DownloadHandlerBuffer();
yield return webRequest.SendWebRequest();
switch (webRequest.result)
{
case UnityWebRequest.Result.InProgress:
break;
case UnityWebRequest.Result.Success:
_callback?.Invoke(webRequest.downloadHandler.text, null);
break;
case UnityWebRequest.Result.ConnectionError:
_callback?.Invoke(null, webRequest.error);
break;
case UnityWebRequest.Result.ProtocolError:
_callback?.Invoke(null, webRequest.error);
break;
case UnityWebRequest.Result.DataProcessingError:
_callback?.Invoke(null, webRequest.error);
break;
default:
break;
}
}
}
yield return null;
}
public static IEnumerator Post(string _url, string _post_json_str, Action<string, string> _callback)

View File

@ -71,10 +71,19 @@ public class ApiManager : Singleton<ApiManager>
public OnDataUpdate m_OnYardLastUpdate { get; set; }
/// <summary>
/// 接口请求方式
/// </summary>
public static string method = "0";
public override void Awake()
{
base.Awake();
using (StreamReader _reader = new StreamReader(Application.streamingAssetsPath+"/method.txt"))
{
method = _reader.ReadLine();
}
//var _ip_str_url = Path.Combine(Application.streamingAssetsPath, "configure.json");
//StartCoroutine(RequestBase.Get(_ip_str_url, (_data, _error) =>
//{
@ -178,6 +187,7 @@ public class ApiManager : Singleton<ApiManager>
{
Debug.Log("相机接口等待执行");
yield return new WaitUntil(() => { return !string.IsNullOrEmpty(IpAddress) && ApiDic.Count > 0; });
Debug.Log("相机接口执行....");
var _get_camera_list = IpAddress + ApiDic["GetCameraList"];
#if UNITY_EDITOR
@ -214,6 +224,7 @@ public class ApiManager : Singleton<ApiManager>
{
Debug.Log("堆场接口等待执行");
yield return new WaitUntil(() => { return !string.IsNullOrEmpty(IpAddress) && ApiDic.Count > 0; });
Debug.Log("堆场接口执行....");
var _get_yard_information = IpAddress + ApiDic["GetYardList"];
#if UNITY_EDITOR
@ -276,6 +287,7 @@ public class ApiManager : Singleton<ApiManager>
{
Debug.Log("计量秤接口等待执行");
yield return new WaitUntil(() => { return !string.IsNullOrEmpty(IpAddress) && ApiDic.Count > 0; });
Debug.Log("计量秤接口执行....");
var _get_belt_scale = IpAddress + ApiDic["GetWeighterList"];
#if UNITY_EDITOR
if (CallForTest.instance != null)

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 415cef8da861edb4d87c897f97db48b3
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: