三国卡牌客户端基础资源仓库
yyl
2026-09-21 d4d8252725281bf90d19622da61bd92dec237b31
Assets/Launch/HttpRequest/HttpBehaviour.cs
@@ -3,11 +3,13 @@
using UnityEngine;
using UnityEngine.Networking;
using System;
#if !UNITY_WEBGL
using System.Net;
using System.Text;
using System.IO;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
#endif
using System.Text;
using System.IO;
using Cysharp.Threading.Tasks;
@@ -24,15 +26,19 @@
        bool getResult = false;
        float timeOut = 0f;
#if !UNITY_WEBGL
        CookieContainer cookie;
        HttpWebRequest request;
#endif
        bool ok = false;
        string message = string.Empty;
        public static int ConnectAllTimes = 0;
#if !UNITY_WEBGL
        static HttpBehaviour()
        {
            ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidationCallback;
        }
#endif
        public static void Create(string _url, string _method, string _content, string _contentType, int _retry = 3, Action<bool, string> _result = null)
        {
@@ -105,6 +111,7 @@
            return (ok, message);
        }
#if !UNITY_WEBGL
        void Begin(string _url, string _method, string _content, string _contentType, int _retry = 3, Action<bool, string> _result = null)
        {
            this.url = _url;
@@ -113,7 +120,7 @@
            this.contentType = _contentType;
            this.totalRetryTimes = _retry;
            this.callBack = _result;
            this.timeOut = Time.time + 5f;
            this.timeOut = Time.time + 20f;
            try
            {
@@ -123,8 +130,8 @@
                request.Method = _method;
                request.ContentType = _contentType;
                request.CookieContainer = cookie;
                request.Timeout = 2000;
                request.ReadWriteTimeout = 2000;
                request.Timeout = 20000;
                request.ReadWriteTimeout = 20000;
                request.Proxy = null;
                request.KeepAlive = true;  //设置为true,部分请求节点大文件会有问题,在请求完成后手动Abort释放
            }
@@ -223,15 +230,18 @@
            return acceptCertificate;
        }
#endif
        void Update()
        {
            if (Time.time > timeOut && !getResult)
            {
#if !UNITY_WEBGL
                if (request != null)
                {
                    request.Abort();
                }
#endif
                ok = false;
                message = "TimeOut";
                getResult = true;
@@ -263,13 +273,16 @@
                finally
                {
                    callBack = null;
#if !UNITY_WEBGL
                    request?.Abort();
#endif
                    Destroy(this.gameObject);
                }
            }
        }
#if !UNITY_WEBGL
        private void GetRequestStreamCallback(IAsyncResult ar)
        {
            Stream postStream;
@@ -330,6 +343,7 @@
                getResult = true;
            }
        }
#endif
    }