Ejemplo n.º 1
0
        public string DownloadFile <T>(string fileName, IFinanceRequest <T> request) where T : FinanceResponse
        {
            string result = "";

            var     content = JsonConvert.SerializeObject(request);
            JObject json    = null;

            if (content == "")
            {
                json = new JObject();
            }
            else
            {
                json = JsonConvert.DeserializeObject <JObject>(content);
            }
            json.Add("Token", Token);
            content = json.ToString();
            var url = RootPath + request.Method + string.Format("?ver={0}&appkey={1}&sign={2}", Ver, Appkey, SignRequest(content));

            result = HttpDownloadFile(url, content, fileName);

            return(result);
        }
Ejemplo n.º 2
0
        public T Execute <T>(IFinanceRequest <T> request) where T : FinanceResponse
        {
            T         rsp          = default(T);
            Stopwatch watcher      = new Stopwatch();
            int       retryCounter = -1;

            while (retryCounter < maxRetryCount)
            {
                watcher.Restart();
                try
                {
                    retryCounter++;
                    var strRsp = "";
                    var plus   = "";
                    if (request.GetType().IsSubclassOf(typeof(IdRequest)))
                    {
                        plus = ("/" + (request as IdRequest).id);
                    }
                    strRsp = Post(request.Method + plus, JsonConvert.SerializeObject(request));

                    rsp = JsonConvert.DeserializeObject <T>(strRsp);
                    if (rsp.IsError)
                    {
                        watcher.Stop();
                        logger.Info("耗时" + watcher.ElapsedMilliseconds + "ms");
                        if (rsp.ErrMsg == "SERVICE_TIMEOUT")
                        {
                            var message = string.Format("调用超时:{0},正在重试{1}次,超时信息:{2},耗时" + watcher.ElapsedMilliseconds + "ms", request.Method, retryCounter + 1, rsp.Body);
                            logger.Warn(message);
                            Thread.Sleep(retryWaitTime);
                            continue;
                        }
                        else
                        {
                            throw new FinanceApiException(rsp.Result, rsp.ErrMsg);
                        }
                    }
                    else
                    {
                        watcher.Stop();
                        logger.Info("耗时" + watcher.ElapsedMilliseconds + "ms");
                        break;
                    }
                }
                catch (WebException e)
                {
                    watcher.Stop();
                    //调用HTTP出错,重试
                    if (retryCounter < maxRetryCount)
                    {
                        var message = string.Format("网络异常:{0},正在重试{1}次,异常信息:{2},耗时" + watcher.ElapsedMilliseconds + "ms,请求:{3}",
                                                    request.Method, retryCounter + 1, e.Message, JsonConvert.SerializeObject(request));
                        logger.Warn(message);
                        Thread.Sleep(retryWaitTime);
                        continue;
                    }
                    else
                    {
                        logger.Warn("耗时" + watcher.ElapsedMilliseconds + "ms");
                        throw e;
                    }
                }
            }
            return(rsp);
        }