123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- namespace Engine.Http
- {
- /// <summary>Http消息请求对象</summary>
- public class HttpRequest
- {
- public HandlerRequestTimeout requestTimeout;
- private const int ParamsMax = 30;
- /// <summary>请求地址</summary>
- private string mUrl = "";
- /// <summary>请求类型编号</summary>
- private int nCode = 0;
- /// <summary>参数的键列表</summary>
- private ObjectArray mParamKetList = new ObjectArray(ParamsMax);
- /// <summary>参数的值列表</summary>
- private ObjectArray mParamValueList = new ObjectArray(ParamsMax);
- /// <summary>HTTP请求的方式,从HttpMethod中的静态常量取值</summary>
- public string httpMethod { set; get; }
- /// <summary>是否启用MD5加密</summary>
- public bool isMD5 { set; get; }
- /// <summary>启用MD5加密时的加密值</summary>
- public char MD5Encode = '0';
- /// <summary>包含url和参数的字符串</summary>
- private string mRquestParamConetent;
- /// <summary>构造函数</summary>
- public HttpRequest()
- {
- }
- /// <summary>创建请求参数对象</summary>
- public static HttpRequest OnCreateRequest()
- {
- return new HttpRequest();
- }
- /// <summary>重置消息参数对象</summary>
- public void Reset()
- {
- this.ClearParam();
- mRquestParamConetent = null;
- requestTimeout = null;
- }
- /// <summary>请求地址</summary>
- public string URL
- {
- get { return mUrl; }
- set { mUrl = value; }
- }
- /// <summary>请求类型编号</summary>
- public int Code
- {
- get { return nCode; }
- set { nCode = value; }
- }
- /// <summary>
- /// 设置纯JSON的参数
- /// </summary>
- /// <param name="value"></param>
- public void SetJSONParamContent(string value)
- {
- CDebug.Log("HttpRequest SetJSONParamContent: 设置了纯json作为参数,内容如下 " + value);
- mRquestParamConetent = value;
- }
- /// <summary>请求的参数体</summary>
- 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<string>(i), mParamValueList.Get<string>(i), i != 0);
- strMd5.Append(mParamValueList.Get<string>(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;
- }
- /// <summary>参数添加到消息体内</summary>
- private void ParamAddStringBuilder(StringBuilder strContent, string key, string value, bool isApart = true)
- {
- if (isApart)
- {
- strContent.Append("&");
- }
- strContent.Append(key);
- strContent.Append("=");
- strContent.Append(value);
- }
- /// <summary>参数个数</summary>
- public int ParamConst
- {
- get
- {
- int nConst = 0;
- int paramConst = mParamKetList.ElementLength;
- for (int i = 0; i < paramConst; i++)
- {
- if (mParamKetList.Get<string>(i) != null)
- {
- nConst++;
- }
- }
- return nConst;
- }
- }
- /// <summary>添加参数</summary>
- public void AddParam(string key, string value)
- {
- mParamKetList.Add(key);
- mParamValueList.Add(value);
- }
- /// <summary>删除参数</summary>
- public void DelParam(string key)
- {
- int paramConst = mParamKetList.ElementLength;
- for (int i = 0; i < paramConst; i++)
- {
- if (mParamKetList.Get<string>(i) == key)
- {
- mParamKetList.Set(i, null);
- mParamValueList.Set(i, null);
- break;
- }
- }
- }
- /// <summary>清楚参数</summary>
- public void ClearParam()
- {
- mParamKetList.SetEmpty();
- mParamValueList.SetEmpty();
- }
- ///<summary>获取MD5码</summary>
- 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;
- }
- }
- }
- }
|