| | |
| | | using System.Collections; |
| | | using System.Collections.Generic; |
| | | using UnityEngine; |
| | | using System; |
| | | using System.Collections;
|
| | | using System.Collections.Generic;
|
| | | using UnityEngine;
|
| | | using System;
|
| | | using System.Net;
|
| | | using System.Text; |
| | | using System.IO; |
| | | |
| | | using System.Text;
|
| | | using System.IO;
|
| | | using System.Net.Security;
|
| | | using System.Security.Cryptography.X509Certificates;
|
| | |
|
| | | public class HttpBehaviour : MonoBehaviour
|
| | | {
|
| | |
|
| | |
| | | bool ok = false;
|
| | | string message = string.Empty;
|
| | |
|
| | | static HttpBehaviour()
|
| | | {
|
| | | ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidationCallback;
|
| | | }
|
| | |
|
| | | public static void Create(string _url, string _method, string _content, string _contentType, int _retry = 3, Action<bool, string> _result = null)
|
| | | {
|
| | | var carrier = new GameObject();
|
| | |
| | | behaviour.Begin(_url, _method, _content, _contentType, _retry, _result);
|
| | | }
|
| | |
|
| | | public void Begin(string _url, string _method, string _content, string _contentType, int _retry = 3, Action<bool, string> _result = null)
|
| | | void Begin(string _url, string _method, string _content, string _contentType, int _retry = 3, Action<bool, string> _result = null)
|
| | | {
|
| | | this.url = _url;
|
| | | this.method = _method;
|
| | |
| | |
|
| | | }
|
| | |
|
| | | private static bool RemoteCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
| | | {
|
| | | if (sslPolicyErrors == SslPolicyErrors.None)
|
| | | {
|
| | | return true;
|
| | | }
|
| | |
|
| | | var acceptCertificate = true;
|
| | | if ((sslPolicyErrors & SslPolicyErrors.RemoteCertificateNotAvailable) == SslPolicyErrors.RemoteCertificateNotAvailable)
|
| | | {
|
| | | acceptCertificate = false;
|
| | | }
|
| | | else
|
| | | {
|
| | | if ((sslPolicyErrors & SslPolicyErrors.RemoteCertificateNameMismatch) == SslPolicyErrors.RemoteCertificateNameMismatch)
|
| | | {
|
| | | acceptCertificate = false;
|
| | | }
|
| | |
|
| | | if ((sslPolicyErrors & SslPolicyErrors.RemoteCertificateChainErrors) == SslPolicyErrors.RemoteCertificateChainErrors)
|
| | | {
|
| | | foreach (X509ChainStatus item in chain.ChainStatus)
|
| | | {
|
| | | if (item.Status != X509ChainStatusFlags.RevocationStatusUnknown &&
|
| | | item.Status != X509ChainStatusFlags.OfflineRevocation)
|
| | | {
|
| | | break;
|
| | | }
|
| | |
|
| | | if (item.Status != X509ChainStatusFlags.NoError)
|
| | | {
|
| | | acceptCertificate = false;
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | if (acceptCertificate == false)
|
| | | {
|
| | | acceptCertificate = true;
|
| | | }
|
| | |
|
| | | return acceptCertificate;
|
| | | }
|
| | |
|
| | | void Update()
|
| | | {
|
| | | if (Time.time > timeOut && !getResult)
|
| | |
| | |
|
| | | if (getResult)
|
| | | {
|
| | | if (callBack != null)
|
| | | try
|
| | | {
|
| | | callBack(ok, message);
|
| | | callBack = null;
|
| | | DebugEx.LogFormat("Http 数据通信 {0},请求数据结果:{1},内容:{2}", this.url, ok, message);
|
| | |
|
| | | if (callBack != null)
|
| | | {
|
| | | callBack(ok, message);
|
| | | }
|
| | | }
|
| | | catch (Exception ex)
|
| | | {
|
| | | Debug.Log(ex);
|
| | | }
|
| | | finally
|
| | | {
|
| | | callBack = null;
|
| | | Destroy(this.gameObject);
|
| | | }
|
| | |
|
| | | Destroy(this.gameObject);
|
| | | }
|
| | | }
|
| | |
|
| | |
| | | }
|
| | | }
|
| | |
|
| | | |
| | | } |
| | |
|
| | | }
|