改善网络请求异常捕获

This commit is contained in:
sin365 2025-01-06 01:01:58 +08:00
parent 7c2709f335
commit 55a8e02a3a
5 changed files with 55 additions and 36 deletions

View File

@ -130,9 +130,9 @@ namespace AxibugEmuOnline.Client.ClientCore
if (!request.downloadHandler.isDone)
yield break;
if (request.downloadHandler.Err != null)
if (request.downloadHandler.bHadErr)
{
App.log.Error(request.downloadHandler.Err);
App.log.Error(request.downloadHandler.ErrInfo);
yield break;
}

View File

@ -88,7 +88,7 @@ public static class AxiHttp
public static long index = 0;
static int singlePkgMaxRead = 1024;
public class WaitAxiRequest : UnityEngine.CustomYieldInstruction
public class WaitAxiRequest : UnityEngine.CustomYieldInstruction
{
public AxiRespInfo mReqAsync;
public WaitAxiRequest(AxiRespInfo reqAsync)
@ -117,7 +117,22 @@ public static class AxiHttp
{
public bool isDone = false;
public AxiDownLoadMode downloadMode = AxiDownLoadMode.NotDownLoad;
public string Err = null;
public bool bHadErr
{
get
{
return
isDone = true
&&
(
!string.IsNullOrEmpty(ErrInfo)
||
code != 200
);
}
}
public string ErrInfo;
//public string Err = null;
public string host = "";//host主机头
public string url = "";//pathAndQuery
public int port = 80;
@ -231,10 +246,10 @@ public static class AxiHttp
bool foward_302 = true;
string ourErrMsg = "";
if (!ParseURI(strURI, ref bSSL, ref strHost, ref strIP, ref port, ref strRelativePath,ref ourErrMsg))
if (!ParseURI(strURI, ref bSSL, ref strHost, ref strIP, ref port, ref strRelativePath, ref ourErrMsg))
{
Log("ParseURI False");
respinfo.Err = ourErrMsg;
respinfo.ErrInfo = ourErrMsg;
respinfo.code = 0;
respinfo.isDone = true;
return;
@ -496,7 +511,7 @@ public static class AxiHttp
}
catch (Exception ex)
{
respinfo.Err = $"ex : {ex.ToString()}";
respinfo.ErrInfo = $"ex : {ex.ToString()}";
}
finally
{
@ -534,7 +549,7 @@ public static class AxiHttp
if (!ParseURI(strURI, ref bSSL, ref strHost, ref strIP, ref port, ref strRelativePath, ref ourErrMsg))
{
Log("ParseURI False");
respinfo.Err = ourErrMsg;
respinfo.ErrInfo = ourErrMsg;
respinfo.code = 0;
respinfo.isDone = true;
return;
@ -778,7 +793,7 @@ public static class AxiHttp
}
catch (Exception ex)
{
respinfo.Err = $"ex : {ex.ToString()}";
respinfo.ErrInfo = $"ex : {ex.ToString()}";
}
finally
{
@ -806,9 +821,9 @@ public static class AxiHttp
{
if (i == 0)
{
respinfo.code = Tools.convertToInt(headers[i].Split(' ')[1]);
if (respinfo.code != 200 && respinfo.code != 301 && respinfo.code != 302)
respinfo.ErrInfo = "code:" + respinfo.code;
}
else
{
@ -1043,7 +1058,7 @@ public static class AxiHttp
{
strIPRet = GetDnsIP(strAddressRet).ToString();
}
catch(Exception ex)
catch (Exception ex)
{
errMsg = ex.ToString();
return false;
@ -1164,7 +1179,6 @@ public static class AxiHttp
Log($"BeginConnect {host}:{port} timeoutMSec=>{timeoutMSec}");
tcpclient.BeginConnect(host, port, new AsyncCallback(CallBackMethod), tcpclient);
if (TimeoutObject.WaitOne(timeoutMSec, false))
{
if (IsConnectionSuccessful)

View File

@ -36,7 +36,7 @@ namespace AxibugEmuOnline.Client
}
AxiHttpProxy.ShowAxiHttpDebugInfo(request.downloadHandler);
if (request.downloadHandler.Err == null)
if (!request.downloadHandler.bHadErr)
{
Directory.CreateDirectory(path);
File.WriteAllBytes($"{path}/{url.GetHashCode()}", request.downloadHandler.data);

View File

@ -54,7 +54,7 @@ namespace AxibugEmuOnline.Client
App.log.Info($"search->{oldsearch} ->{searchKey}");
//searchKey = HttpUtility.UrlDecode(searchKey);
}
////避免特殊字符和个别文字编码问题
//避免特殊字符和个别文字编码问题
//byte[] gb2312Bytes = Encoding.Default.GetBytes(searchKey);
//byte[] utf8Bytes = Encoding.Convert(Encoding.Default, Encoding.UTF8, gb2312Bytes);
//// 将UTF-8编码的字节数组转换回字符串此时是UTF-8编码的字符串
@ -71,13 +71,16 @@ namespace AxibugEmuOnline.Client
yield break;
}
if (request.downloadHandler.Err != null)
if (!request.downloadHandler.bHadErr)
{
App.log.Error(request.downloadHandler.Err);
callback.Invoke(null);
var resp = JsonUtility.FromJson<Resp_GameList>(request.downloadHandler.text);
callback.Invoke(resp);
yield break;
}
App.log.Error(request.downloadHandler.ErrInfo);
callback.Invoke(null);
/*
UnityWebRequest request = UnityWebRequest.Get($"{WebSiteApi}/NesRomList?Page={page}&PageSize={pageSize}&SearchKey={searchKey}");
yield return request.SendWebRequest();
@ -88,8 +91,6 @@ namespace AxibugEmuOnline.Client
yield break;
}*/
var resp = JsonUtility.FromJson<Resp_GameList>(request.downloadHandler.text);
callback.Invoke(resp);
}
private IEnumerator GetNesRomListFlow(int page, int pageSize, Action<Resp_GameList> callback)
{
@ -103,12 +104,16 @@ namespace AxibugEmuOnline.Client
yield break;
}
if (request.downloadHandler.Err != null)
//请求成功
if (!request.downloadHandler.bHadErr)
{
App.log.Error(request.downloadHandler.Err);
callback.Invoke(null);
var resp = JsonUtility.FromJson<Resp_GameList>(request.downloadHandler.text);
callback.Invoke(resp);
yield break;
}
App.log.Error(request.downloadHandler.ErrInfo);
callback.Invoke(null);
/*
UnityWebRequest request = UnityWebRequest.Get($"{WebSiteApi}/NesRomList?Page={page}&PageSize={pageSize}");
yield return request.SendWebRequest();
@ -119,8 +124,6 @@ namespace AxibugEmuOnline.Client
yield break;
}
*/
var resp = JsonUtility.FromJson<Resp_GameList>(request.downloadHandler.text);
callback.Invoke(resp);
}
public IEnumerator GetNesRomInfo(int RomID, Action<Resp_RomInfo> callback)
@ -134,13 +137,17 @@ namespace AxibugEmuOnline.Client
yield break;
}
if (request.downloadHandler.Err != null)
//成功
if (!request.downloadHandler.bHadErr)
{
App.log.Error(request.downloadHandler.Err);
callback.Invoke(null);
var resp = JsonUtility.FromJson<Resp_RomInfo>(request.downloadHandler.text);
callback.Invoke(resp);
yield break;
}
App.log.Error(request.downloadHandler.ErrInfo);
callback.Invoke(null);
/*
UnityWebRequest request = UnityWebRequest.Get($"{WebSiteApi}/RomInfo?PType={PlatformType.Nes}&RomID={RomID}");
yield return request.SendWebRequest();
@ -151,8 +158,6 @@ namespace AxibugEmuOnline.Client
yield break;
}*/
var resp = JsonUtility.FromJson<Resp_RomInfo>(request.downloadHandler.text);
callback.Invoke(resp);
}
enum PlatformType : byte

View File

@ -136,7 +136,7 @@ namespace AxibugEmuOnline.Client
var request = downloadRequest;
downloadRequest = null;
if (request.downloadHandler.Err == null)
if (!request.downloadHandler.bHadErr)
callback(request.downloadHandler.data);
else
callback(null);