HttpRequest.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Engine.Http
  6. {
  7. /// <summary>Http消息请求对象</summary>
  8. public class HttpRequest
  9. {
  10. public HandlerRequestTimeout requestTimeout;
  11. private const int ParamsMax = 30;
  12. /// <summary>请求地址</summary>
  13. private string mUrl = "";
  14. /// <summary>请求类型编号</summary>
  15. private int nCode = 0;
  16. /// <summary>参数的键列表</summary>
  17. private ObjectArray mParamKetList = new ObjectArray(ParamsMax);
  18. /// <summary>参数的值列表</summary>
  19. private ObjectArray mParamValueList = new ObjectArray(ParamsMax);
  20. /// <summary>HTTP请求的方式,从HttpMethod中的静态常量取值</summary>
  21. public string httpMethod { set; get; }
  22. /// <summary>是否启用MD5加密</summary>
  23. public bool isMD5 { set; get; }
  24. /// <summary>启用MD5加密时的加密值</summary>
  25. public char MD5Encode = '0';
  26. /// <summary>包含url和参数的字符串</summary>
  27. private string mRquestParamConetent;
  28. /// <summary>构造函数</summary>
  29. public HttpRequest()
  30. {
  31. }
  32. /// <summary>创建请求参数对象</summary>
  33. public static HttpRequest OnCreateRequest()
  34. {
  35. return new HttpRequest();
  36. }
  37. /// <summary>重置消息参数对象</summary>
  38. public void Reset()
  39. {
  40. this.ClearParam();
  41. mRquestParamConetent = null;
  42. requestTimeout = null;
  43. }
  44. /// <summary>请求地址</summary>
  45. public string URL
  46. {
  47. get { return mUrl; }
  48. set { mUrl = value; }
  49. }
  50. /// <summary>请求类型编号</summary>
  51. public int Code
  52. {
  53. get { return nCode; }
  54. set { nCode = value; }
  55. }
  56. /// <summary>
  57. /// 设置纯JSON的参数
  58. /// </summary>
  59. /// <param name="value"></param>
  60. public void SetJSONParamContent(string value)
  61. {
  62. CDebug.Log("HttpRequest SetJSONParamContent: 设置了纯json作为参数,内容如下 " + value);
  63. mRquestParamConetent = value;
  64. }
  65. /// <summary>请求的参数体</summary>
  66. public string RequestParamContent
  67. {
  68. get
  69. {
  70. if (mRquestParamConetent == null)
  71. {
  72. StringBuilder strContent = new StringBuilder();
  73. //参数体
  74. StringBuilder strMd5 = new StringBuilder();
  75. int paramConst = mParamKetList.ElementLength;
  76. for (int i = 0; i < paramConst; i++)
  77. {
  78. this.ParamAddStringBuilder(strContent, mParamKetList.Get<string>(i), mParamValueList.Get<string>(i), i != 0);
  79. strMd5.Append(mParamValueList.Get<string>(i));
  80. }
  81. if (isMD5)
  82. {
  83. //MD5相关参数
  84. //this.ParamAddStringBuilder(strContent, MD5Encode, strMd5.ToString());
  85. this.ParamAddStringBuilder(strContent, HttpConstConfig.KEY_SIG, this.GetMd5String(strMd5.ToString()));
  86. }
  87. //最后一个参数验证串
  88. strMd5.Append(HttpConstConfig.WEB_HTTP_SECRET_KEY);
  89. mRquestParamConetent = strContent.ToString();
  90. }
  91. //返回
  92. return mRquestParamConetent;
  93. }
  94. }
  95. public void ResetRequestParamContent()
  96. {
  97. mRquestParamConetent = null;
  98. }
  99. /// <summary>参数添加到消息体内</summary>
  100. private void ParamAddStringBuilder(StringBuilder strContent, string key, string value, bool isApart = true)
  101. {
  102. if (isApart)
  103. {
  104. strContent.Append("&");
  105. }
  106. strContent.Append(key);
  107. strContent.Append("=");
  108. strContent.Append(value);
  109. }
  110. /// <summary>参数个数</summary>
  111. public int ParamConst
  112. {
  113. get
  114. {
  115. int nConst = 0;
  116. int paramConst = mParamKetList.ElementLength;
  117. for (int i = 0; i < paramConst; i++)
  118. {
  119. if (mParamKetList.Get<string>(i) != null)
  120. {
  121. nConst++;
  122. }
  123. }
  124. return nConst;
  125. }
  126. }
  127. /// <summary>添加参数</summary>
  128. public void AddParam(string key, string value)
  129. {
  130. mParamKetList.Add(key);
  131. mParamValueList.Add(value);
  132. }
  133. /// <summary>删除参数</summary>
  134. public void DelParam(string key)
  135. {
  136. int paramConst = mParamKetList.ElementLength;
  137. for (int i = 0; i < paramConst; i++)
  138. {
  139. if (mParamKetList.Get<string>(i) == key)
  140. {
  141. mParamKetList.Set(i, null);
  142. mParamValueList.Set(i, null);
  143. break;
  144. }
  145. }
  146. }
  147. /// <summary>清楚参数</summary>
  148. public void ClearParam()
  149. {
  150. mParamKetList.SetEmpty();
  151. mParamValueList.SetEmpty();
  152. }
  153. ///<summary>获取MD5码</summary>
  154. private string GetMd5String(string strConetent)
  155. {
  156. byte[] result = System.Text.Encoding.ASCII.GetBytes(strConetent);
  157. System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  158. byte[] output = md5.ComputeHash(result);
  159. md5.Clear();
  160. StringBuilder retStr = new StringBuilder();
  161. for (int i = 0; i < output.Length; i++)
  162. {
  163. retStr.Append(System.Convert.ToString(output[i], 16).PadLeft(2, MD5Encode));
  164. }
  165. return retStr.ToString().PadLeft(32, MD5Encode);
  166. }
  167. public string URLAndParamsForGet
  168. {
  169. get
  170. {
  171. return URL + "?" + RequestParamContent;
  172. }
  173. }
  174. }
  175. }