Example #1
0
        /// <summary>
        /// 处理远程请求方法,并返回需要的实体
        /// </summary>
        /// <typeparam name="T">需要返回的实体类型</typeparam>
        /// <param name="request">远程请求组件的request基本信息</param>
        /// <returns>实体类型</returns>
        protected static async Task <T> RestCommonJson <T>(OssHttpRequest request)
            where T : WXBaseResp, new()
        {
            var resp = await request.RestSend(WXPlatConfigProvider.ClientFactory?.Invoke());

            if (!resp.IsSuccessStatusCode)
            {
                return new T()
                       {
                           ret = -(int)resp.StatusCode,
                           msg = resp.ReasonPhrase
                       }
            }
            ;

            var contentStr = await resp.Content.ReadAsStringAsync();

            var t = JsonConvert.DeserializeObject <T>(contentStr);

            if (!t.IsSuccess())
            {
                t.msg = t.errmsg;
            }

            return(t);
        }
Example #2
0
        /// <summary>
        /// 下载对账单
        /// </summary>
        /// <param name="billReq"></param>
        /// <returns></returns>
        public async Task <Resp <string> > DownloadBillAsync(WXPayDownloadBillReq billReq)
        {
            var dics = billReq.GetDics();

            dics.Add("appid", ApiConfig.AppId);
            dics.Add("mch_id", ApiConfig.MchId);
            CompleteDicSign(dics);

            var req = new OssHttpRequest
            {
                HttpMethod = HttpMethod.Post,
                AddressUrl = string.Concat(m_ApiUrl, "/pay/downloadbill"),
                CustomBody = dics.ProduceXml()
            };

            var response = await req.RestSend();

            if (!response.IsSuccessStatusCode)
            {
                return new Resp <string>()
                       {
                           ret = -1, msg = "当前请求出错!"
                       }
            }
            ;

            var content = await response.Content.ReadAsStringAsync();

            return(content.StartsWith("<xml>") ? new Resp <string>(content)
                : new Resp <string>().WithResp(RespTypes.OperateFailed, content));
        }

        #endregion
    }
        /// <summary>
        ///   下载文件方法
        /// </summary>
        protected static async Task <WXFileResp> DownLoadFileAsync(OssHttpRequest req)
        {
            var resp = await req.RestSend(WXPlatConfigProvider.ClientFactory?.Invoke());

            if (!resp.IsSuccessStatusCode)
            {
                return new WXFileResp()
                       {
                           ret = (int)RespTypes.ObjectStateError, msg = "当前请求失败!"
                       }
            }
            ;

            var contentStr = resp.Content.Headers.ContentType.MediaType;

            using (resp)
            {
                if (!contentStr.Contains("application/json"))
                {
                    return new WXFileResp()
                           {
                               content_type = contentStr,
                               file         = await resp.Content.ReadAsByteArrayAsync()
                           }
                }
                ;

                return(JsonConvert.DeserializeObject <WXFileResp>(await resp.Content.ReadAsStringAsync()));
            }
        }

        #endregion
    }
}
Example #4
0
        /// <summary>
        ///   下载文件方法
        /// </summary>
        protected async Task <WXFileResp> DownLoadFileAsync(OssHttpRequest req)
        {
            var configRes = await GetMeta();

            if (!configRes.IsSuccess())
            {
                return(new WXFileResp().WithResp(configRes));
            }

            var appConfig = configRes.data;
            var tokenRes  = await GetAccessToken(appConfig);

            if (!tokenRes.IsSuccess())
            {
                return(new WXFileResp().WithResp(tokenRes));
            }

            req.AddressUrl = string.Concat(req.AddressUrl,
                                           (req.AddressUrl.IndexOf('?') > 0 ? "&" : "?"),
                                           (appConfig.OperateMode == AppOperateMode.ByAgent ? "component_access_token=" : "access_token="),
                                           tokenRes.data);

            var resp = await req.RestSend(WXPlatConfigProvider.ClientFactory?.Invoke());

            if (!resp.IsSuccessStatusCode)
            {
                return new WXFileResp()
                       {
                           ret = (int)RespTypes.ObjectStateError, msg = "当前请求失败!"
                       }
            }
            ;

            var contentStr = resp.Content.Headers.ContentType.MediaType;

            using (resp)
            {
                if (!contentStr.Contains("application/json"))
                {
                    return new WXFileResp()
                           {
                               content_type = contentStr,
                               file         = await resp.Content.ReadAsByteArrayAsync()
                           }
                }
                ;

                return(JsonConvert.DeserializeObject <WXFileResp>(await resp.Content.ReadAsStringAsync()));
            }
        }
Example #5
0
        public void GetTest()
        {
            var req = new OssHttpRequest
            {
                AddressUrl =
                    "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxaa9e6cb3f03afa97&secret=0fc0c6f735a90fda1df5fc840e010144&code=ssss&grant_type=authorization_code"
            };

            req.HttpMethod = HttpMethod.Get;
            var result = req.RestSend().WaitResult();
            var resp   = result.Content.ReadAsStringAsync().WaitResult();

            Assert.IsTrue(!string.IsNullOrEmpty(resp));
        }
Example #6
0
 private static async Task <TRes> Rest <TRes>(OssHttpRequest req, Func <HttpResponseMessage, Task <TRes> > format)
 {
     try
     {
         using (var resp = await req.RestSend())
         {
             if (!resp.IsSuccessStatusCode)
             {
                 LogHelper.Error($"错误信息:接口请求响应状态异常(HttpStatusCode:{resp.StatusCode},地址:{req.AddressUrl})!", "RestApi");
             }
             return(await format(resp));
         }
     }
     catch (Exception error)
     {
         LogHelper.Error(string.Concat("错误信息:", error.Message, "详细信息:", error.StackTrace), nameof(RestApiHelper));
     }
     return(default);
Example #7
0
        public async Task <SendAliSmsResp> Send(SendAliSmsReq sendReq)
        {
            var appConfigRes = await GetMeta();

            if (!appConfigRes.IsSuccess())
            {
                return(new SendAliSmsResp().WithResp(appConfigRes));
            }

            var appConfig = appConfigRes.data;
            var dirs      = new SortedDictionary <string, string>(StringComparer.Ordinal)
            {
                { "Action", "SendSms" },
                { "Version", appConfig.Version },
                { "RegionId", appConfig.RegionId },
                { "PhoneNumbers", string.Join(",", sendReq.PhoneNums) },
                { "SignName", sendReq.sign_name },
                { "TemplateCode", sendReq.template_code }
            };

            if (sendReq.body_paras != null && sendReq.body_paras.Count > 0)
            {
                var temparas = JsonConvert.SerializeObject(sendReq.body_paras);
                dirs.Add("TemplateParam", temparas);
            }

            FillApiPara(appConfig, dirs);

            var req = new OssHttpRequest
            {
                AddressUrl = string.Concat("http://dysmsapi.aliyuncs.com?", GeneratePostData(appConfig, dirs)),
                HttpMethod = HttpMethod.Get
            };

            using (var resp = await req.RestSend())
            {
                var content = await resp.Content.ReadAsStringAsync();

                return(JsonConvert.DeserializeObject <SendAliSmsResp>(content));
            }
        }
Example #8
0
        /// <summary>
        /// 处理远程请求方法,并返回需要的实体
        /// </summary>
        /// <typeparam name="T">需要返回的实体类型</typeparam>
        /// <param name="request">远程请求组件的request基本信息</param>
        /// <returns>实体类型</returns>
        public async Task <T> RestCommonJson <T>(OssHttpRequest request)
            where T : WXBaseResp, new()
        {
            var response = await request.RestSend(WXOauthConfigProvider.ClientFactory?.Invoke());

            using (response)
            {
                if (!response.IsSuccessStatusCode)
                {
                    return new T()
                           {
                               ret = -(int)response.StatusCode,
                               msg = response.ReasonPhrase
                           }
                }
                ;
                var contentStr = await response.Content.ReadAsStringAsync();

                return(JsonConvert.DeserializeObject <T>(contentStr));
            }
        }
Example #9
0
        /// <summary>
        /// 处理远程请求方法,并返回需要的实体
        /// </summary>
        /// <typeparam name="T">需要返回的实体类型</typeparam>
        /// <param name="request">远程请求组件的request基本信息</param>
        /// <param name="funcFormat">获取实体格式化方法</param>
        /// <param name="needCert">是否需要双向证书</param>
        /// <param name="checkSign">是否检查返回签名,个别接口没有</param>
        /// <returns>实体类型</returns>
        protected async Task <T> RestCommonAsync <T>(OssHttpRequest request,
                                                     Func <HttpResponseMessage, Task <T> > funcFormat, bool needCert, bool checkSign)
            where T : WXPayBaseResp, new()
        {
            var t = default(T);

            try
            {
                var client = GetCertHttpClient(needCert);

                var resp = await request.RestSend(client);

                if (resp.IsSuccessStatusCode)
                {
                    if (funcFormat != null)
                    {
                        t = await funcFormat(resp);
                    }
                    else
                    {
                        var contentStr = await resp.Content.ReadAsStringAsync();

                        t = GetRespResult <T>(contentStr);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(string.Concat("基类请求出错,错误信息:", ex.Message), "RestCommon", WXPayConfigProvider.ModuleName);
                t = new T {
                    ret = (int)RespTypes.InnerError, msg = "微信支付请求失败"
                };
            }

            return(t);
        }
Example #10
0
        /// <summary>
        /// 处理远程请求方法,并返回需要的实体
        /// </summary>
        /// <typeparam name="T">需要返回的实体类型</typeparam>
        /// <param name="request">远程请求组件的request基本信息</param>
        /// <param name="respColumnName">响应实体中的内容列表</param>
        /// <param name="funcFormat">获取实体格式化方法</param>
        /// <returns>实体类型</returns>
        public async Task <T> RestCommonAsync <T>(OssHttpRequest request, string respColumnName,
                                                  Func <HttpResponseMessage, Task <T> > funcFormat = null)
            where T : ZPayBaseResp, new()
        {
            var t = default(T);

            try
            {
                request.AddressUrl = string.Concat(m_ApiUrl, "?charset=", ApiConfig.Charset);

                request.RequestSet = r =>
                {
                    if (r.Content == null)
                    {
                        return;
                    }
                    var contentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded")
                    {
                        CharSet = ApiConfig.Charset
                    };
                    r.Content.Headers.ContentType = contentType;
                };

                var resp = await request.RestSend();

                if (resp.IsSuccessStatusCode)
                {
                    if (funcFormat != null)
                    {
                        t = await funcFormat(resp);
                    }
                    else
                    {
                        var contentStr = await resp.Content.ReadAsStringAsync();

                        var resJsonObj = JObject.Parse(contentStr);
                        if (resJsonObj == null)
                        {
                            return new T()
                                   {
                                       ret = (int)RespTypes.OperateFailed,
                                       msg = "基础请求响应不正确,请检查地址或者网络是否正常!"
                                   }
                        }
                        ;

                        t = resJsonObj[respColumnName].ToObject <T>();
                        if (t.IsSuccess())
                        {
                            var sign = resJsonObj["sign"].ToString();

                            var signContent = GetCehckSignContent(respColumnName, contentStr);

                            CheckSign(signContent, sign, t);
                        }
                        else
                        {
                            t.msg = string.Concat(t.msg, "-", t.sub_msg);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                var logCode = LogHelper.Error(string.Concat("基类请求出错,错误信息:", ex.Message), "Z_RestCommon",
                                              ZPayConfigProvider.ModuleName);
                t = new T().WithResp(SysRespTypes.AppError, string.Concat("基类请求出错,请检查网络是否正常,错误码:", logCode));
            }
            return(t);
        }