public virtual T Execute <T>(IQimenCloudRequest <T> request) where T : QimenCloudResponse
 {
     return(DoExecute <T>(request, null, DateTime.Now));
 }
 public virtual T Execute <T>(IQimenCloudRequest <T> request, string session) where T : QimenCloudResponse
 {
     return(DoExecute <T>(request, session, DateTime.Now));
 }
        private T DoExecute <T>(IQimenCloudRequest <T> request, string session, DateTime timestamp) where T : QimenCloudResponse
        {
            long start = DateTime.Now.Ticks;

            // 提前检查业务参数
            try
            {
                request.Validate();
            }
            catch (TopException e)
            {
                return(CreateErrorResponse <T>(e.ErrorCode, e.ErrorMsg));
            }

            // 兼容 老奇门1.0 的 XML BODY
            String apiBody = null;

            Object[] requestXmlBodyType = request.GetType().GetCustomAttributes(typeof(RequestXmlBodyAttribute), false);
            Boolean  isXmlBody          = (requestXmlBodyType != null && requestXmlBodyType.Length > 0);

            if (isXmlBody)
            {
                XmlWriter writer = new XmlWriter(Constants.QM_ROOT_TAG_REQ, typeof(IQimenCloudRequest <T>));
                apiBody = writer.Write(request);
            }

            // 添加协议级请求参数
            TopDictionary txtParams = new TopDictionary(request.GetParameters());

            txtParams.Add(Constants.METHOD, request.GetApiName());
            txtParams.Add(Constants.SIGN_METHOD, signMethod);
            txtParams.Add(Constants.APP_KEY, appKey);
            txtParams.Add(Constants.FORMAT, format);
            txtParams.Add(Constants.VERSION, "2.0");
            txtParams.Add(Constants.PARTNER_ID, GetSdkVersion());
            txtParams.Add(Constants.TIMESTAMP, timestamp);
            txtParams.Add(Constants.TARGET_APP_KEY, request.GetTargetAppKey());
            txtParams.Add(Constants.SESSION, session);
            txtParams.AddAll(this.systemParameters);

            // 添加签名参数
            if (isXmlBody)
            {
                txtParams.Add(Constants.SIGN, TopUtils.SignTopRequest(txtParams, apiBody, appSecret, signMethod));
            }
            else
            {
                txtParams.Add(Constants.SIGN, TopUtils.SignTopRequest(txtParams, appSecret, signMethod));
            }

            // 添加头部参数
            if (this.useGzipEncoding)
            {
                request.GetHeaderParameters()[Constants.ACCEPT_ENCODING] = Constants.CONTENT_ENCODING_GZIP;
            }

            string realServerUrl = GetServerUrl(this.serverUrl, request.GetApiName(), session);

            string reqUrl;

            if (isXmlBody)
            {
                reqUrl = WebUtils.BuildRequestUrl(realServerUrl, txtParams);
            }
            else
            {
                reqUrl = WebUtils.BuildRequestUrl(realServerUrl, txtParams);
            }

            try
            {
                string body;
                if (isXmlBody)
                {
                    body = webUtils.DoPost(reqUrl, Encoding.UTF8.GetBytes(apiBody), Constants.QM_CONTENT_TYPE, request.GetHeaderParameters());
                }
                else
                {
                    body = webUtils.DoPost(realServerUrl, txtParams, request.GetHeaderParameters());
                }

                // 解释响应结果
                T rsp;
                if (disableParser)
                {
                    rsp            = Activator.CreateInstance <T>();
                    rsp.Body       = body;
                    rsp.RequestUrl = reqUrl;
                }
                else
                {
                    if (Constants.FORMAT_XML.Equals(format))
                    {
                        ITopParser <T> tp = new QimenCloudXmlParser <T>();
                        rsp = tp.Parse(body);
                    }
                    else
                    {
                        ITopParser <T> tp = new QimenCloudSimplifyJsonParser <T>();
                        rsp = tp.Parse(body);
                    }
                    rsp.RequestUrl = reqUrl;
                }

                // 追踪错误的请求
                if (rsp.IsError)
                {
                    TimeSpan latency = new TimeSpan(DateTime.Now.Ticks - start);
                    TraceApiError(appKey, request.GetApiName(), serverUrl, txtParams, latency.TotalMilliseconds, rsp.Body);
                }
                return(rsp);
            }
            catch (Exception e)
            {
                TimeSpan latency = new TimeSpan(DateTime.Now.Ticks - start);
                TraceApiError(appKey, request.GetApiName(), serverUrl, txtParams, latency.TotalMilliseconds, e.GetType() + ": " + e.Message);
                throw e;
            }
        }