少年修仙传客户端代码仓库
client_Hale
2018-09-25 ec901bfd42ee3f46f5b5269e2a838da1ef111455
System/HttpRequest/HttpBehaviour.cs
@@ -1,11 +1,13 @@
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
{
@@ -24,6 +26,11 @@
    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();
@@ -33,7 +40,7 @@
        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;
@@ -107,6 +114,51 @@
    }
    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)
@@ -122,14 +174,25 @@
        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);
        }
    }
@@ -194,5 +257,5 @@
        }
    }
}
}