| | |
| | | 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; |
| | | |
| | | |
| | |
| | | |
| | | 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) |
| | | { |
| | |
| | | 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; |
| | |
| | | this.contentType = _contentType; |
| | | this.totalRetryTimes = _retry; |
| | | this.callBack = _result; |
| | | this.timeOut = Time.time + 5f; |
| | | this.timeOut = Time.time + 20f; |
| | | |
| | | try |
| | | { |
| | |
| | | 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释放 |
| | | } |
| | |
| | | |
| | | 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; |
| | |
| | | finally |
| | | { |
| | | callBack = null; |
| | | #if !UNITY_WEBGL |
| | | request?.Abort(); |
| | | #endif |
| | | Destroy(this.gameObject); |
| | | } |
| | | |
| | | } |
| | | } |
| | | |
| | | #if !UNITY_WEBGL |
| | | private void GetRequestStreamCallback(IAsyncResult ar) |
| | | { |
| | | Stream postStream; |
| | |
| | | getResult = true; |
| | | } |
| | | } |
| | | #endif |
| | | |
| | | |
| | | } |