Exemplo n.º 1
0
        /// <summary>
        /// 执行网络请求
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="request"></param>
        /// <returns></returns>
        public virtual string DoExecute <T>(ApiRequestBase <T> request)
            where T : ApiResponses
        {
            var url = request.GetUrl();
            //Log(url);
            var result = String.Empty;

            switch (request.Method)
            {
            case "FILE":
                result = HttpHelper.HttpUploadFile(url, request.GetPostContent());
                break;

            case "POST":
                result = HttpHelper.HttpPost(url, request.GetPostContent());
                break;

            case "POSTFILE":
                result = HttpHelper.FilePostRequest(url, request.GetPostContent());
                break;

            case "GET":
            default:
                result = HttpHelper.HttpGet(url);
                break;
            }

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 执行请求
        /// </summary>
        /// <typeparam name="T">响应</typeparam>
        /// <param name="request">请求</param>
        /// <returns>响应</returns>
        public T Execute <T>(ApiRequestBase <T> request)
            where T : ApiResponses, new()
        {
            request.Validate();

            var execResult = DoExecute(request);
            T   result     = null;

            try
            {
                result = JsonConvert.DeserializeObject <T>(execResult);
            }
            catch (Exception ex)
            {
                Log(ex);
                result = null;
            }

            if (result == null)
            {
                if (request.NotHasResponse)
                {
                    return(new T());
                }
                else
                {
                    //  throw new WebException();

                    return(new T());
                }
            }

            return(result);
        }