| | |
| | | using System.Collections.Generic; |
| | | using UnityEngine; |
| | | using System; |
| | | using System.Net; |
| | | using System.Net;
|
| | | using System.Text; |
| | | using System.IO; |
| | | |
| | | 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 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); |
| | | } |
| | | |
| | | public 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 = false; |
| | | } |
| | | 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; |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | void Update() |
| | | { |
| | | if (Time.time > timeOut && !getResult) |
| | | { |
| | | if (request != null) |
| | | { |
| | | request.Abort(); |
| | | } |
| | | ok = false; |
| | | message = "TimeOut"; |
| | | getResult = true; |
| | | } |
| | | |
| | | if (getResult) |
| | | { |
| | | if (callBack != null) |
| | | { |
| | | callBack(ok, message); |
| | | callBack = null; |
| | | DesignDebug.LogFormat("Http 数据通信 {0},请求数据结果:{1},内容:{2}", this.url, ok, message); |
| | | } |
| | | |
| | | 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; |
| | | } |
| | | } |
| | | |
| | | 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 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);
|
| | | }
|
| | |
|
| | | public 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 = false;
|
| | | }
|
| | | 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;
|
| | | }
|
| | | }
|
| | |
|
| | | }
|
| | |
|
| | | void Update()
|
| | | {
|
| | | if (Time.time > timeOut && !getResult)
|
| | | {
|
| | | if (request != null)
|
| | | {
|
| | | request.Abort();
|
| | | }
|
| | | ok = false;
|
| | | message = "TimeOut";
|
| | | getResult = true;
|
| | | }
|
| | |
|
| | | if (getResult)
|
| | | {
|
| | | if (callBack != null)
|
| | | {
|
| | | callBack(ok, message);
|
| | | callBack = null;
|
| | | DebugEx.LogFormat("Http 数据通信 {0},请求数据结果:{1},内容:{2}", this.url, ok, message);
|
| | | }
|
| | |
|
| | | 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;
|
| | | }
|
| | | }
|
| | |
|
| | | |
| | | } |