HttpMsg.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Runtime.Serialization;
  6. using System.Runtime.Serialization.Formatters.Binary;
  7. using System;
  8. namespace Engine.Http
  9. {
  10. public delegate void HandlerResponser(HttpResponse responder);
  11. public delegate void HandlerRequestTimeout(HttpRequest request);
  12. /// <summary>http消息</summary>
  13. internal class HttpMsg
  14. {
  15. /// <summary>Http请求消息头</summary>
  16. private static Dictionary<string, string> MsgHeaders = new Dictionary<string, string>() { { "Content-Type", "application/x-www-form-urlencoded" } };
  17. /// <summary>消息对象</summary>
  18. private WWW mHttp3W;
  19. /// <summary>消息请求参数对象</summary>
  20. public HttpRequest mHttpRequest;
  21. /// <summary>是否使用中</summary>
  22. public bool IsUseIng = false;
  23. /// <summary>请求是否完成</summary>
  24. public bool IsRequestComplete = false;
  25. /// <summary>请求进度,例如进度是10%返回值是10</summary>
  26. public int RequestProgress = 0;
  27. /// <summary>响应对象</summary>
  28. public HttpResponse mHttpResponse;
  29. /// <summary>响应时间</summary>
  30. private int mResponseTime = 0;
  31. private bool mIsTimeout = false;
  32. private Action<HttpResponse> httpCallBack;//回调函数 dudu add 2020-06-28
  33. /// <summary>开始下载</summary>
  34. public HttpMsg() { }
  35. /// <summary>发送消息</summary>
  36. public void SendMsg(HttpRequest requestParam, Action<HttpResponse> httpCallBack = null)
  37. {
  38. //使用中
  39. IsUseIng = true;
  40. //保存请求参数对象
  41. mHttpRequest = requestParam;
  42. this.httpCallBack = httpCallBack;
  43. // Debug.Log("mHttpRequest.httpMethod ===>" + mHttpRequest.httpMethod);
  44. //创建请求对象 创建后请求已经开始发送
  45. if (mHttpRequest.httpMethod == HttpMethod.POST)
  46. {
  47. // Debug.Log("mHttpRequest.httpMethod ===>" + mHttpRequest.URL);
  48. //byte[] bytes = ObjectToBytes(mHttpRequest.postData);
  49. //&format=json
  50. mHttp3W = new WWW(mHttpRequest.URL, System.Text.UTF8Encoding.UTF8.GetBytes(mHttpRequest.RequestParamContent), MsgHeaders);
  51. }else
  52. {
  53. Debug.Log("mHttpRequest.httpMethod ===>" + mHttpRequest.URLAndParamsForGet);
  54. mHttp3W = new WWW(mHttpRequest.URLAndParamsForGet);
  55. }
  56. CDebug.Log("http网络请求已发送:" + mHttpRequest.URL + "?"+ (mHttpRequest.httpMethod == HttpMethod.POST));
  57. }
  58. public void SendCallBack()
  59. {
  60. if(httpCallBack != null)
  61. {
  62. httpCallBack.Invoke(mHttpResponse);
  63. }
  64. }
  65. public static byte[] ObjectToBytes(object obj) {
  66. using (MemoryStream ms = new MemoryStream()) {
  67. IFormatter formatter = new BinaryFormatter();
  68. formatter.Serialize(ms, obj);
  69. return ms.GetBuffer();
  70. }
  71. }
  72. /// <summary>帧函数,检查请求是否完成</summary>
  73. public void OnUpdate(int nTime)
  74. {
  75. if (mHttp3W == null || IsRequestComplete || mIsTimeout)
  76. { return; }
  77. mResponseTime += nTime;
  78. if (mHttp3W.isDone)
  79. {
  80. //下载完成后的第二帧修改完成 标记
  81. if (RequestProgress == 100)
  82. {
  83. IsRequestComplete = true;
  84. }
  85. RequestProgress = 100;
  86. if (IsRequestComplete)
  87. {
  88. //创建响应对象
  89. mHttpResponse = HttpResponse.OnCreateResponse(mHttp3W.text, mHttpRequest);
  90. }
  91. }
  92. else
  93. {
  94. RequestProgress = (int)(mHttp3W.progress * 100);
  95. if(mResponseTime >= HttpManager.ResponseOverTimeLimit)
  96. {
  97. mResponseTime = 0;
  98. mIsTimeout = true;
  99. /*if (mHttpRequest.requestTimeout != null)//TODO 请求超时
  100. {
  101. mHttpRequest.requestTimeout(mHttpRequest);
  102. }*/
  103. }
  104. }
  105. }
  106. /// <summary>重置请求对象</summary>
  107. public void Reset()
  108. {
  109. IsUseIng = false;
  110. mHttp3W = null;
  111. mHttpRequest.Reset();
  112. mHttpRequest = null;
  113. if(mHttpResponse != null)
  114. mHttpResponse.Reset();
  115. mHttpResponse = null;
  116. RequestProgress = 0;
  117. mResponseTime = 0;
  118. mIsTimeout = false;
  119. IsRequestComplete = false;
  120. }
  121. public float ResponseTime
  122. {
  123. get
  124. {
  125. return mResponseTime;
  126. }
  127. }
  128. public bool IsTimeout
  129. {
  130. get
  131. {
  132. return mIsTimeout;
  133. }
  134. }
  135. }
  136. }