using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine;
|
using System;
|
using System.Net;
|
using System.Text;
|
using System.IO;
|
using System.Net.Security;
|
using System.Security.Cryptography.X509Certificates;
|
|
public class HttpBehaviour : MonoBehaviour
|
{
|
|
string url;
|
string method;
|
string content;
|
string contentType;
|
|
int totalRetryTimes = 3;
|
Action<bool, string> callBack;
|
|
bool getResult = false;
|
float timeOut = 0f;
|
CookieContainer cookie;
|
HttpWebRequest request;
|
bool ok = false;
|
string message = string.Empty;
|
public static int ConnectAllTimes = 0;
|
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();
|
GameObject.DontDestroyOnLoad(carrier);
|
carrier.hideFlags = HideFlags.HideInHierarchy;
|
var behaviour = carrier.AddComponent<HttpBehaviour>();
|
behaviour.Begin(_url, _method, _content, _contentType, _retry, _result);
|
}
|
|
void Begin(string _url, string _method, string _content, string _contentType, int _retry = 3, Action<bool, string> _result = null)
|
{
|
this.url = _url;
|
this.method = _method;
|
this.content = _content;
|
this.contentType = _contentType;
|
this.totalRetryTimes = _retry;
|
this.callBack = _result;
|
this.timeOut = Time.time + 5f;
|
|
try
|
{
|
cookie = new CookieContainer();
|
request = (HttpWebRequest)WebRequest.Create(_url);
|
request.ServicePoint.Expect100Continue = false;
|
request.Method = _method;
|
request.ContentType = _contentType;
|
request.CookieContainer = cookie;
|
request.Timeout = 2000;
|
request.ReadWriteTimeout = 2000;
|
request.Proxy = null;
|
request.KeepAlive = true; //设置为true,部分请求节点大文件会有问题,在请求完成后手动Abort释放
|
}
|
catch (Exception ex)
|
{
|
if (request != null)
|
{
|
request.Abort();
|
}
|
|
ok = false;
|
message = ex.Message;
|
getResult = true;
|
}
|
|
if (_method == "POST")
|
{
|
var data = Encoding.UTF8.GetBytes(_content);
|
request.ContentLength = data.Length;
|
try
|
{
|
var stream = request.BeginGetRequestStream(GetRequestStreamCallback, null);
|
}
|
catch (System.Exception ex)
|
{
|
if (request != null)
|
{
|
request.Abort();
|
}
|
ok = false;
|
message = ex.Message;
|
getResult = true;
|
}
|
}
|
else
|
{
|
try
|
{
|
request.BeginGetResponse(OnHttpWebResponse, null);
|
}
|
catch (System.Exception ex)
|
{
|
if (request != null)
|
{
|
request.Abort();
|
}
|
ok = false;
|
message = ex.Message;
|
getResult = true;
|
}
|
}
|
|
}
|
|
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 (request != null)
|
{
|
request.Abort();
|
}
|
ok = false;
|
message = "TimeOut";
|
getResult = true;
|
}
|
|
if (getResult)
|
{
|
try
|
{
|
DebugEx.LogFormat("Http 数据通信 {0},请求数据结果:{1},内容:{2}", this.url, ok, message);
|
if (ok)
|
{
|
ConnectAllTimes = 0;
|
}
|
else
|
{
|
ConnectAllTimes++;
|
}
|
if (callBack != null)
|
{
|
callBack(ok, message);
|
|
}
|
}
|
catch (Exception ex)
|
{
|
Debug.LogError(ex);
|
}
|
finally
|
{
|
callBack = null;
|
request?.Abort();
|
Destroy(this.gameObject);
|
}
|
|
}
|
}
|
|
private void GetRequestStreamCallback(IAsyncResult ar)
|
{
|
Stream postStream;
|
try
|
{
|
postStream = request.EndGetRequestStream(ar);
|
byte[] byteArray = Encoding.UTF8.GetBytes(content);
|
postStream.Write(byteArray, 0, byteArray.Length);
|
postStream.Close();
|
request.BeginGetResponse(OnHttpWebResponse, request);
|
}
|
catch (Exception ex)
|
{
|
if (request != null)
|
{
|
request.Abort();
|
}
|
ok = false;
|
message = ex.Message;
|
getResult = true;
|
}
|
}
|
|
private void OnHttpWebResponse(IAsyncResult _result)
|
{
|
HttpWebResponse response = null;
|
try
|
{
|
response = request.EndGetResponse(_result) as HttpWebResponse;
|
response.Cookies = cookie.GetCookies(response.ResponseUri);
|
Stream myResponseStream = response.GetResponseStream();
|
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
|
string retString = myStreamReader.ReadToEnd();
|
myStreamReader.Close();
|
myResponseStream.Close();
|
response.Close();
|
request.Abort();
|
ok = true;
|
message = retString;
|
}
|
catch (System.Exception ex)
|
{
|
if (response != null)
|
{
|
response.Close();
|
}
|
|
if (request != null)
|
{
|
request.Abort();
|
}
|
ok = false;
|
message = ex.Message;
|
getResult = true;
|
}
|
finally
|
{
|
getResult = true;
|
}
|
}
|
|
|
}
|