/// <summary> /// 处理远程请求方法,并返回需要的实体 /// </summary> /// <typeparam name="T">需要返回的实体类型</typeparam> /// <param name="request">远程请求组件的request基本信息</param> /// <param name="funcFormat">获取实体格式化方法</param> /// <param name="client">自定义请求客户端,当前主要是因为标准库没有提供证书设置选项,所以通过上层运行时传入设置委托,在使用证书的子类中构造客户端传入 </param> /// <returns>实体类型</returns> protected async Task <T> RestCommonAsync <T>(OsHttpRequest request, Func <HttpResponseMessage, Task <T> > funcFormat = null, HttpClient client = null) where T : WxPayBaseResp, new() { var t = default(T); try { 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) { var errorKey = LogUtil.Error(string.Concat("基类请求出错,错误信息:", ex.Message), "RestCommon", ModuleNames.PayCenter); t = new T() { ret = -1, msg = string.Concat("当前请求出错,错误码:", errorKey) }; } return(t); }
/// <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 OsHttpRequest { 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.ObjectStateError, content)); } #endregion }
/// <summary> /// 下载对账单 /// </summary> /// <param name="billReq"></param> /// <returns></returns> public async Task <ResultMo <string> > DownloadBillAsync(WxPayDownloadBillReq billReq) { var dics = billReq.GetDics(); dics.Add("appid", ApiConfig.AppId); dics.Add("mch_id", ApiConfig.MchId); CompleteDicSign(dics); var req = new OsHttpRequest(); req.HttpMothed = HttpMothed.POST; req.AddressUrl = string.Concat(m_ApiUrl, "/pay/downloadbill"); req.CustomBody = dics.ProduceXml(); var response = await req.RestSend(); if (response.IsSuccessStatusCode) { var content = await response.Content.ReadAsStringAsync(); if (content.StartsWith("<xml>")) { return(new ResultMo <string>(content)); } return(new ResultMo <string>(ResultTypes.ObjectStateError, content)); } return(new ResultMo <string>() { Ret = 0, Message = "当前请求出错!" }); }
/// <summary> /// 处理远程请求方法,并返回需要的实体 /// </summary> /// <typeparam name="T">需要返回的实体类型</typeparam> /// <param name="request">远程请求组件的request基本信息</param> /// <param name="formatFunc">处理内容委托</param> /// <param name="client">自定义httpclient</param> /// <param name="moduleName">模块名称</param> /// <returns>实体类型</returns> public static async Task <T> RestCommon <T>(this OsHttpRequest request, Func <HttpResponseMessage, Task <T> > formatFunc, HttpClient client = null, string moduleName = ModuleNames.Default) where T : ResultMo, new() { T t; try { var resp = await request.RestSend(client); t = await formatFunc(resp); } catch (Exception ex) { t = new T() { ret = (int)ResultTypes.InnerError, msg = ex.Message }; LogUtil.Error(string.Concat("基类请求出错,错误信息:", ex.Message), "RestCommon", moduleName); } return(t ?? new T() { ret = -1, msg = "未发现结果" }); }
private static async Task <HttpResponseMessage> GetTest() { var req = new OsHttpRequest(); req.AddressUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxaa9e6cb3f03afa97&secret=0fc0c6f735a90fda1df5fc840e010144&code=ssss&grant_type=authorization_code"; req.HttpMothed = HttpMothed.GET; return(await req.RestSend()); }
private static async Task <HttpResponseMessage> GetTest1() { var req = new OsHttpRequest { AddressUrl = "http://www.baidu.com", HttpMethod = HttpMethod.Get }; return(await req.RestSend()); }
private string GetAsync() { var req = new OsHttpRequest(); req.AddressUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxaa9e6cb3f03afa97&secret=0fc0c6f735a90fda1df5fc840e010144&code=ssss&grant_type=authorization_code"; req.HttpMothed = HttpMothed.GET; var result = req.RestSend().WaitResult(); return(result.Content.ReadAsStringAsync().WaitResult()); }
/// <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>(OsHttpRequest request, Func <HttpResponseMessage, Task <T> > funcFormat = null, bool needCert = false, bool checkSign = true) 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) { var errorKey = LogUtil.Error(string.Concat("基类请求出错,错误信息:", ex.Message), "RestCommon", ModuleNames.PayCenter); t = new T { ret = -1, msg = string.Concat("当前请求出错,错误码:", errorKey) }; } if (t == null || !t.IsSuccess()) { return t ?? new T { ret = (int)ResultTypes.ObjectNull, msg = "未发现结果信息!" } } ; if (t.return_code.ToUpper() == "SUCCESS" && t.result_code.ToUpper() == "SUCCESS") { return(t); } t.ret = -1; t.msg = string.Concat(t.return_msg, t.err_code_des); return(t); }
static void Main(string[] args) { var req = new OsHttpRequest(); req.Uri = new Uri("http://www.baidu.com"); var result = req.RestSend(); result.Wait(); Console.WriteLine(result.Result.Content); Console.Read(); }
public void GetTest() { var req = new OsHttpRequest(); req.AddressUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxaa9e6cb3f03afa97&secret=0fc0c6f735a90fda1df5fc840e010144&code=ssss&grant_type=authorization_code"; req.HttpMothed = HttpMothed.GET; var result = req.RestSend().WaitResult(); var resp = result.Content.ReadAsStringAsync().WaitResult(); Assert.IsTrue(!string.IsNullOrEmpty(resp)); }
/// <summary> /// 处理远程请求方法,并返回需要的实体 /// </summary> /// <typeparam name="T">需要返回的实体类型</typeparam> /// <param name="request">远程请求组件的request基本信息</param> /// <param name="formatFunc">处理内容委托</param> /// <param name="client">自定义httpclient</param> /// <param name="moduleName">模块名称</param> /// <returns>实体类型</returns> public static async Task <T> RestCommon <T>(this OsHttpRequest request, Func <HttpResponseMessage, Task <T> > formatFunc, HttpClient client = null, string moduleName = ModuleNames.Default) where T : ResultMo, new() { var resp = await request.RestSend(client); var t = await formatFunc(resp); return(t ?? new T() { ret = -1, msg = "未发现结果" }); }
/// <summary> /// 处理远程请求方法,并返回需要的实体 /// </summary> /// <typeparam name="T">需要返回的实体类型</typeparam> /// <param name="request">远程请求组件的request基本信息</param> /// <param name="funcFormat">获取实体格式化方法</param> /// <param name="client">自定义请求客户端,当前主要是因为标准库没有提供证书设置选项,所以通过上层运行时传入设置委托,在使用证书的子类中构造客户端传入 </param> /// <returns>实体类型</returns> protected async Task <T> RestCommon <T>(OsHttpRequest request, Func <HttpResponseMessage, Task <T> > funcFormat = null, HttpClient client = null) where T : WxPayBaseResp, new() { T t = default(T); string errorKey = null; try { 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); } if (t.return_code.ToUpper() != "SUCCESS") { //通信结果处理,这个其实没意义,脱裤子放屁 t.Ret = 0; t.Message = t.return_msg; } else if (!t.IsSuccess) { // 请求数据结果处理 t.Message = GetErrMsg(t.err_code?.ToUpper()); } } } catch (Exception ex) { t = new T() { Ret = (int)ResultTypes.InnerError, Message = ex.Message }; errorKey = LogUtil.Error(string.Concat("基类请求出错,错误信息:", ex.Message), "RestCommon", ModuleNames.PayCenter); } return(t ?? new T() { Ret = 0, Message = string.Concat("当前请求出错,错误码:", errorKey) }); }
private static async Task <HttpResponseMessage> Test() { //OsHttpRequest req = new OsHttpRequest(); //req.AddressUrl = "http://www.baidu.com"; //req.HttpMothed = HttpMothed.GET; //return await req.RestSend(); var req = new OsHttpRequest(); req.AddressUrl = "http://localhost:59489/"; req.HttpMothed = HttpMothed.POST; var imageFile = new FileStream("E:\\111.png", FileMode.Open, FileAccess.Read); req.FileParameters.Add(new FileParameter("media", imageFile, "111.png", "image/jpeg")); //video/mpeg4 //req.FormParameters.Add(new FormParameter("description", "{\"title\":\"title\", \"introduction\":\"introduction\"}")); return(await req.RestSend()); }
private static void Main(string[] args) { var req = new OsHttpRequest(); req.Uri = new Uri("http://www.baidu.com"); var result = req.RestSend(); result.Wait(); var content = result.Result.Content.ReadAsStringAsync(); content.Wait(); Console.WriteLine(content.Result); Console.Read(); var client = new HttpClient(); var resTask = client.GetAsync("http://www.baidu.com"); }
/// <summary> /// 处理远程请求方法,并返回需要的实体 /// </summary> /// <typeparam name="T">需要返回的实体类型</typeparam> /// <param name="request">远程请求组件的request基本信息</param> /// <param name="funcFormat">获取实体格式化方法</param> /// <returns>实体类型</returns> public static async Task <T> RestCommon <T>(OsHttpRequest request, Func <HttpResponseMessage, Task <T> > funcFormat = null) where T : ResultMo, new() { T t = default(T); try { var resp = await request.RestSend(); if (resp.IsSuccessStatusCode) { if (funcFormat != null) { t = await funcFormat(resp); } else { var contentStr = await resp.Content.ReadAsStringAsync(); t = JsonConvert.DeserializeObject <T>(contentStr); } if (!t.IsSuccess) { t.Message = GetErrMsg(t.Ret); } } } catch (Exception ex) { // ignored t = new T() { Ret = (int)ResultTypes.InnerError, Message = ex.Message }; LogUtil.Error(string.Concat("基类请求出错,错误信息:", ex.Message), "RestCommon", ModuleNames.SocialCenter); } return(t ?? new T() { Ret = 0 }); }
private static async Task <HttpResponseMessage> Test() { //OsHttpRequest req = new OsHttpRequest(); //req.AddressUrl = "http://www.baidu.com"; //req.HttpMothed = HttpMothed.GET; //return await req.RestSend(); var req = new OsHttpRequest(); req.AddressUrl = "http://localhost:59489/"; req.HttpMethod = HttpMethod.Post; // 文件上传测试 //var imageFile = new FileStream("E:\\111.png", FileMode.Open, FileAccess.Read); //req.FileParameters.Add(new FileParameter("media", imageFile, "111.png", "image/jpeg")); // 表单参数测试 //req.FormParameters.Add(new FormParameter("description", "测试")); return(await req.RestSend()); }
public async Task <HttpResponseMessage> GetResp(string url) { var reqHandler = new HttpClientHandler { AllowAutoRedirect = true, MaxAutomaticRedirections = 5, UseCookies = false, UseProxy = false }; reqHandler.ServerCertificateCustomValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true; HttpClient client = new HttpClient(reqHandler); var req = new OsHttpRequest(); // req.RequestSet = msg => msg.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); req.HttpMothed = HttpMothed.GET; req.AddressUrl = url; return(await req.RestSend(client)); }
/// <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>(OsHttpRequest request, Func <HttpResponseMessage, Task <T> > funcFormat = null, bool needCert = false, bool checkSign = true) 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) { LogUtil.Error(string.Concat("基类请求出错,错误信息:", ex.Message), "RestCommon", ModuleNames.PayCenter); t = new T { ret = (int)ResultTypes.InnerError, msg = "微信支付请求失败" }; } return(t); }
/// <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>(OsHttpRequest 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); var contentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded") { CharSet = ApiConfig.Charset }; request.RequestSet = message => message.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)ResultTypes.ObjectStateError, 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 = LogUtil.Error(string.Concat("基类请求出错,错误信息:", ex.Message), "Z_RestCommon", ModuleNames.SocialCenter); t = new T() { ret = (int)ResultTypes.InnerError, msg = string.Concat("基类请求出错,请检查网络是否正常,错误码:", logCode) }; } return(t); }