Пример #1
0
        private T DoExecute <T>(CustomRequest <T> request, string session) where T : CustomResponse
        {
            long start = DateTime.Now.Ticks;

            // 添加协议级请求参数
            CustomDictionary parameters = new CustomDictionary();

            if (request.GetQueryParameters() != null)
            {
                parameters.AddAll(request.GetQueryParameters());
            }

            if (string.IsNullOrEmpty(appKey) && string.IsNullOrEmpty(appSecret))
            {
                //parameters.Add("accept", "application/json");
                //parameters.Add("content-type", "application/json");
                //request.AddHeaderParameter("accept", "application/json");
                //request.AddHeaderParameter("content-type", "application/json");
            }
            else
            {
                //parameters.Add(Constants.METHOD, request.GetApiName());
                //parameters.Add(Constants.VERSION, request.Version);
                //parameters.Add(Constants.APP_KEY, appKey);
                //parameters.Add(Constants.TIMESTAMP, request.Timestamp);
                //parameters.Add(Constants.FORMAT, format);
                //parameters.Add(Constants.SIGN_METHOD, signMethod);
                //parameters.Add(Constants.SESSION, session);
                //parameters.Add(Constants.PARTNER_ID, Constants.SDK_VERSION);
                //parameters.Add(Constants.QM_CUSTOMER_ID, request.CustomerId);
                //request.AddHeaderParameter("accept", "application/xml");
                //request.AddHeaderParameter("content-type", "application/xml");
            }

            //json
            //parameters.Add();

            // 添加头部参数
            if (this.useGzipEncoding)
            {
                request.AddHeaderParameter(Constants.ACCEPT_ENCODING, Constants.CONTENT_ENCODING_GZIP);
            }

            try
            {
                string reqBody = request.Body;
                if (string.IsNullOrEmpty(reqBody))
                {
                    //XmlWriter writer = new XmlWriter(Constants.QM_ROOT_TAG_REQ, typeof(QimenRequest<T>));
                    //reqBody = writer.Write(request);
                    if (string.IsNullOrEmpty(appKey) && string.IsNullOrEmpty(appSecret))
                    {
                        reqBody = JsonConvert.SerializeObject(request);
                    }
                    else
                    {
                        reqBody = XmlSerializeHelper.XmlSerialize(request);
                    }
                }

                // 添加签名参数

                string fullUrl = WebUtils.BuildRequestUrl(serverUrl, parameters);
                string rspBody = webUtils.DoPost(fullUrl, Encoding.UTF8.GetBytes(reqBody), Constants.QM_CONTENT_TYPE, request.GetHeaderParameters());

                // 解释响应结果
                T rsp = null;
                if (disableParser)
                {
                    rsp      = Activator.CreateInstance <T>();
                    rsp.Body = rspBody;
                }
                else
                {
                    if (string.IsNullOrEmpty(appKey) && string.IsNullOrEmpty(appSecret))
                    {
                        rsp = JsonConvert.DeserializeObject <T>(rspBody);
                    }
                    else
                    {
                        if (Constants.FORMAT_XML.Equals(format, StringComparison.OrdinalIgnoreCase))
                        {
                            XmlDeserializeHelper <T> helper = new XmlDeserializeHelper <T>();
                            rsp = helper.Parse(rspBody);
                        }
                    }
                }

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