Example #1
0
        /// <summary>
        /// 文件上传功能
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <param name="fileInfo"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        protected T FileUpload <T>(string apiUrl, FileInfo fileInfo, Dictionary <string, string> parameters) where T : class
        {
            Dictionary <string, string> headers = new Dictionary <string, string>();

            headers.Add(token_key, getToken().access_token);
            List <FileInfo> files = new List <FileInfo>();

            files.Add(fileInfo);
            string responseStr = HttpSendUtil.SendHttpForm(baseUrl + apiUrl, headers, files, parameters);
            // 转换为 response 对象
            BaseSignResponse response = JSONUtil.getObjectFromJsonString <BaseSignResponse>(responseStr);

            // 抛出 网关异常
            if (response.return_code != "1000")
            {
                throw new SignApplicationException(response.return_message, response, null);
            }
            // 抛除 服务异常
            if (response.result_code != "0")
            {
                throw new SignServerException(response.result_message, response, null);
            }
            return(response.GetData <T>());
        }
Example #2
0
        /// <summary>
        /// 发起 http 请求并
        /// </summary>
        /// <typeparam name="T">响应数据实体对象</typeparam>
        /// <param name="request">请求对象</param>
        /// <returns></returns>
        protected T Send <T>(SignRequest request) where T : class
        {
            // 创建url
            string url = URLUtil.ToPaeameter(baseUrl + request.apiUrl, request.parames);
            // 头部
            Dictionary <string, string> headers = new Dictionary <string, string>();
            string body = JSONUtil.getJsonStringFromObject(request.requestBody);

            if (request.needSign)
            {
                string sign = SignUtil.createSign(body, appSecret);
                headers.Add(sign_key, sign);
                //Console.WriteLine("请求sign:" + sign);
            }
            if (request.needToken)
            {
                headers.Add(token_key, getToken().access_token);
            }

            // 发起http 请求
            string responseStr = HttpSendUtil.SentHttpRequest(url, request.httpMethod, headers, request.contentType, body);
            // 转换为 response 对象
            BaseSignResponse response = JSONUtil.getObjectFromJsonString <BaseSignResponse>(responseStr);

            // 抛出 网关异常
            if (response.return_code != "1000")
            {
                throw new SignApplicationException(response.return_message, response, body);
            }
            // 抛除 服务异常
            if (response.result_code != "0")
            {
                throw new SignServerException(response.result_message, response, body);
            }
            return(response.GetData <T>());
        }