using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Text; namespace Engine.Http { /// Http消息请求对象 public class HttpRequest { public HandlerRequestTimeout requestTimeout; private const int ParamsMax = 30; /// 请求地址 private string mUrl = ""; /// 请求类型编号 private int nCode = 0; /// 参数的键列表 private ObjectArray mParamKetList = new ObjectArray(ParamsMax); /// 参数的值列表 private ObjectArray mParamValueList = new ObjectArray(ParamsMax); /// HTTP请求的方式,从HttpMethod中的静态常量取值 public string httpMethod { set; get; } /// 是否启用MD5加密 public bool isMD5 { set; get; } /// 启用MD5加密时的加密值 public char MD5Encode = '0'; /// 包含url和参数的字符串 private string mRquestParamConetent; /// 构造函数 public HttpRequest() { } /// 创建请求参数对象 public static HttpRequest OnCreateRequest() { return new HttpRequest(); } /// 重置消息参数对象 public void Reset() { this.ClearParam(); mRquestParamConetent = null; requestTimeout = null; } /// 请求地址 public string URL { get { return mUrl; } set { mUrl = value; } } /// 请求类型编号 public int Code { get { return nCode; } set { nCode = value; } } /// /// 设置纯JSON的参数 /// /// public void SetJSONParamContent(string value) { CDebug.Log("HttpRequest SetJSONParamContent: 设置了纯json作为参数,内容如下 " + value); mRquestParamConetent = value; } /// 请求的参数体 public string RequestParamContent { get { if (mRquestParamConetent == null) { StringBuilder strContent = new StringBuilder(); //参数体 StringBuilder strMd5 = new StringBuilder(); int paramConst = mParamKetList.ElementLength; for (int i = 0; i < paramConst; i++) { this.ParamAddStringBuilder(strContent, mParamKetList.Get(i), mParamValueList.Get(i), i != 0); strMd5.Append(mParamValueList.Get(i)); } if (isMD5) { //MD5相关参数 //this.ParamAddStringBuilder(strContent, MD5Encode, strMd5.ToString()); this.ParamAddStringBuilder(strContent, HttpConstConfig.KEY_SIG, this.GetMd5String(strMd5.ToString())); } //最后一个参数验证串 strMd5.Append(HttpConstConfig.WEB_HTTP_SECRET_KEY); mRquestParamConetent = strContent.ToString(); } //返回 return mRquestParamConetent; } } public void ResetRequestParamContent() { mRquestParamConetent = null; } /// 参数添加到消息体内 private void ParamAddStringBuilder(StringBuilder strContent, string key, string value, bool isApart = true) { if (isApart) { strContent.Append("&"); } strContent.Append(key); strContent.Append("="); strContent.Append(value); } /// 参数个数 public int ParamConst { get { int nConst = 0; int paramConst = mParamKetList.ElementLength; for (int i = 0; i < paramConst; i++) { if (mParamKetList.Get(i) != null) { nConst++; } } return nConst; } } /// 添加参数 public void AddParam(string key, string value) { mParamKetList.Add(key); mParamValueList.Add(value); } /// 删除参数 public void DelParam(string key) { int paramConst = mParamKetList.ElementLength; for (int i = 0; i < paramConst; i++) { if (mParamKetList.Get(i) == key) { mParamKetList.Set(i, null); mParamValueList.Set(i, null); break; } } } /// 清楚参数 public void ClearParam() { mParamKetList.SetEmpty(); mParamValueList.SetEmpty(); } ///获取MD5码 private string GetMd5String(string strConetent) { byte[] result = System.Text.Encoding.ASCII.GetBytes(strConetent); System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] output = md5.ComputeHash(result); md5.Clear(); StringBuilder retStr = new StringBuilder(); for (int i = 0; i < output.Length; i++) { retStr.Append(System.Convert.ToString(output[i], 16).PadLeft(2, MD5Encode)); } return retStr.ToString().PadLeft(32, MD5Encode); } public string URLAndParamsForGet { get { return URL + "?" + RequestParamContent; } } } }