CommonJsonSend.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #region Apache License Version 2.0
  2. /*----------------------------------------------------------------
  3. Copyright 2019 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
  4. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  5. except in compliance with the License. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software distributed under the
  8. License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  9. either express or implied. See the License for the specific language governing permissions
  10. and limitations under the License.
  11. Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
  12. ----------------------------------------------------------------*/
  13. #endregion Apache License Version 2.0
  14. /*----------------------------------------------------------------
  15. Copyright (C) 2019 Senparc
  16. 文件名:CommonJsonSend.cs
  17. 文件功能描述:通过CommonJsonSend中的方法调用接口
  18. 创建标识:Senparc - 20151012
  19. 修改标识:Senparc - 20170606
  20. 修改描述:v14.4.11 完善CommonJsonSend.SendAsync()方法参数
  21. 修改标识:Senparc - 20190129
  22. 修改描述:v6.3.8 修复 CommonJsonSend.Send() 方法中的异常请求结果自动抛出
  23. ----------------------------------------------------------------*/
  24. using System;
  25. using System.IO;
  26. using System.Text;
  27. using System.Threading.Tasks;
  28. using Senparc.CO2NET.Extensions;
  29. using Senparc.CO2NET.Helpers;
  30. using Senparc.CO2NET.Helpers.Serializers;
  31. using Senparc.Weixin.Entities;
  32. using Senparc.Weixin.Exceptions;
  33. using Senparc.CO2NET.HttpUtility;
  34. namespace Senparc.Weixin.CommonAPIs
  35. {
  36. /// <summary>
  37. /// 所有高级接口共用的向微信服务器发送 API 请求的方法
  38. /// </summary>
  39. public static class CommonJsonSend
  40. {
  41. #region 公共私有方法
  42. /// <summary>
  43. /// 设定条件,当API结果没有返回成功信息时抛出异常
  44. /// </summary>
  45. static Action<string, string> getFailAction = (apiUrl, returnText) =>
  46. {
  47. WeixinTrace.SendApiLog(apiUrl, returnText);
  48. if (returnText.Contains("errcode"))
  49. {
  50. //可能发生错误
  51. WxJsonResult errorResult = SerializerHelper.GetObject<WxJsonResult>(returnText);
  52. if (errorResult.errcode != ReturnCode.请求成功)
  53. {
  54. //发生错误
  55. throw new ErrorJsonResultException(
  56. string.Format("微信 GET 请求发生错误!错误代码:{0},说明:{1}",
  57. (int)errorResult.errcode, errorResult.errmsg), null, errorResult, apiUrl);
  58. }
  59. }
  60. };
  61. /// <summary>
  62. /// 设定条件,当API结果没有返回成功信息时抛出异常
  63. /// </summary>
  64. static Action<string, string> postFailAction = (apiUrl, returnText) =>
  65. {
  66. if (returnText.Contains("errcode"))
  67. {
  68. //可能发生错误
  69. WxJsonResult errorResult = SerializerHelper.GetObject<WxJsonResult>(returnText);
  70. if (errorResult.errcode != ReturnCode.请求成功)
  71. {
  72. //发生错误
  73. throw new ErrorJsonResultException(
  74. string.Format("微信 POST 请求发生错误!错误代码:{0},说明:{1}",
  75. (int)errorResult.errcode,
  76. errorResult.errmsg),
  77. null, errorResult, apiUrl);
  78. }
  79. }
  80. };
  81. #endregion
  82. #region 同步方法
  83. /// <summary>
  84. /// 向需要AccessToken的API发送消息的公共方法
  85. /// </summary>
  86. /// <param name="accessToken">这里的AccessToken是通用接口的AccessToken,非OAuth的。如果不需要,可以为null,此时urlFormat不要提供{0}参数</param>
  87. /// <param name="urlFormat"></param>
  88. /// <param name="data">如果是Get方式,可以为null</param>
  89. /// <param name="sendType"></param>
  90. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  91. /// <param name="checkValidationResult"></param>
  92. /// <param name="jsonSetting"></param>
  93. /// <returns></returns>
  94. public static WxJsonResult Send(string accessToken, string urlFormat, object data, CommonJsonSendType sendType = CommonJsonSendType.POST, int timeOut = CO2NET.Config.TIME_OUT, bool checkValidationResult = false, JsonSetting jsonSetting = null)
  95. {
  96. return Send<WxJsonResult>(accessToken, urlFormat, data, sendType, timeOut, checkValidationResult, jsonSetting);
  97. }
  98. /// <summary>
  99. /// 向需要AccessToken的API发送消息的公共方法
  100. /// </summary>
  101. /// <param name="accessToken">这里的AccessToken是通用接口的AccessToken,非OAuth的。如果不需要,可以为null,此时urlFormat不要提供{0}参数</param>
  102. /// <param name="urlFormat">用accessToken参数填充{0}</param>
  103. /// <param name="data">如果是Get方式,可以为null</param>
  104. /// <param name="sendType"></param>
  105. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  106. /// <param name="checkValidationResult">验证服务器证书回调自动验证</param>
  107. /// <param name="jsonSetting"></param>
  108. /// <returns></returns>
  109. public static T Send<T>(string accessToken, string urlFormat, object data, CommonJsonSendType sendType = CommonJsonSendType.POST, int timeOut = CO2NET.Config.TIME_OUT, bool checkValidationResult = false, JsonSetting jsonSetting = null)
  110. {
  111. //TODO:此方法可以设定一个日志记录开关
  112. try
  113. {
  114. var url = string.IsNullOrEmpty(accessToken) ? urlFormat : string.Format(urlFormat, accessToken.AsUrlData());
  115. switch (sendType)
  116. {
  117. case CommonJsonSendType.GET:
  118. return Get.GetJson<T>(url, afterReturnText: getFailAction);
  119. case CommonJsonSendType.POST:
  120. var jsonString = SerializerHelper.GetJsonString(data, jsonSetting);
  121. using (MemoryStream ms = new MemoryStream())
  122. {
  123. var bytes = Encoding.UTF8.GetBytes(jsonString);
  124. ms.Write(bytes, 0, bytes.Length);
  125. ms.Seek(0, SeekOrigin.Begin);
  126. WeixinTrace.SendApiPostDataLog(url, jsonString);//记录Post的Json数据
  127. //PostGetJson方法中将使用WeixinTrace记录结果
  128. return Post.PostGetJson<T>(url, null, ms,
  129. timeOut: timeOut,
  130. afterReturnText: postFailAction,
  131. checkValidationResult: checkValidationResult);
  132. }
  133. //TODO:对于特定的错误类型自动进行一次重试,如40001(目前的问题是同样40001会出现在不同的情况下面)
  134. default:
  135. throw new ArgumentOutOfRangeException("sendType");
  136. }
  137. }
  138. catch (ErrorJsonResultException ex)
  139. {
  140. ex.Url = urlFormat;
  141. throw;
  142. }
  143. }
  144. #endregion
  145. #if !NET35 && !NET40
  146. #region 异步方法
  147. /// <summary>
  148. /// 向需要AccessToken的API发送消息的公共方法
  149. /// </summary>
  150. /// <param name="accessToken">这里的AccessToken是通用接口的AccessToken,非OAuth的。如果不需要,可以为null,此时urlFormat不要提供{0}参数</param>
  151. /// <param name="urlFormat"></param>
  152. /// <param name="data">如果是Get方式,可以为null</param>
  153. /// <param name="sendType"></param>
  154. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  155. /// <param name="checkValidationResult">验证服务器证书回调自动验证</param>
  156. /// <param name="jsonSetting"></param>
  157. /// <returns></returns>
  158. public static async Task<WxJsonResult> SendAsync(string accessToken, string urlFormat, object data, CommonJsonSendType sendType = CommonJsonSendType.POST, int timeOut = CO2NET.Config.TIME_OUT, bool checkValidationResult = false, JsonSetting jsonSetting = null)
  159. {
  160. return await SendAsync<WxJsonResult>(accessToken, urlFormat, data, sendType, timeOut, checkValidationResult, jsonSetting);
  161. }
  162. /// <summary>
  163. /// 向需要AccessToken的API发送消息的公共方法
  164. /// </summary>
  165. /// <param name="accessToken">这里的AccessToken是通用接口的AccessToken,非OAuth的。如果不需要,可以为null,此时urlFormat不要提供{0}参数</param>
  166. /// <param name="urlFormat"></param>
  167. /// <param name="data">如果是Get方式,可以为null。在POST方式中将被转为JSON字符串提交</param>
  168. /// <param name="sendType">发送类型,POST或GET,默认为POST</param>
  169. /// <param name="timeOut">代理请求超时时间(毫秒)</param>
  170. /// <param name="checkValidationResult">验证服务器证书回调自动验证</param>
  171. /// <param name="jsonSetting">JSON字符串生成设置</param>
  172. /// <returns></returns>
  173. public static async Task<T> SendAsync<T>(string accessToken, string urlFormat, object data,
  174. CommonJsonSendType sendType = CommonJsonSendType.POST, int timeOut = CO2NET.Config.TIME_OUT,
  175. bool checkValidationResult = false, JsonSetting jsonSetting = null)
  176. {
  177. try
  178. {
  179. var url = string.IsNullOrEmpty(accessToken) ? urlFormat : string.Format(urlFormat, accessToken.AsUrlData());
  180. switch (sendType)
  181. {
  182. case CommonJsonSendType.GET:
  183. return await Get.GetJsonAsync<T>(url, afterReturnText: getFailAction);
  184. case CommonJsonSendType.POST:
  185. var jsonString = SerializerHelper.GetJsonString(data, jsonSetting);
  186. using (MemoryStream ms = new MemoryStream())
  187. {
  188. var bytes = Encoding.UTF8.GetBytes(jsonString);
  189. await ms.WriteAsync(bytes, 0, bytes.Length);
  190. ms.Seek(0, SeekOrigin.Begin);
  191. WeixinTrace.SendApiPostDataLog(url, jsonString);//记录Post的Json数据
  192. //PostGetJson方法中将使用WeixinTrace记录结果
  193. return await Post.PostGetJsonAsync<T>(url, null, ms,
  194. timeOut: timeOut,
  195. afterReturnText: postFailAction,
  196. checkValidationResult: checkValidationResult);
  197. }
  198. default:
  199. throw new ArgumentOutOfRangeException("sendType");
  200. }
  201. }
  202. catch (ErrorJsonResultException ex)
  203. {
  204. ex.Url = urlFormat;
  205. throw;
  206. }
  207. }
  208. #endregion
  209. #endif
  210. }
  211. }