Post.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. 文件名:Post.cs
  17. 文件功能描述:Post
  18. 创建标识:Senparc - 20150211
  19. 修改标识:Senparc - 20150303
  20. 修改描述:整理接口
  21. 修改标识:Senparc - 20150312
  22. 修改描述:开放代理请求超时时间
  23. 修改标识:zhanghao-kooboo - 20150316
  24. 修改描述:增加
  25. 修改标识:Senparc - 20150407
  26. 修改描述:发起Post请求方法修改,为了上传永久视频素材
  27. 修改标识:Senparc - 20160720
  28. 修改描述:增加了PostFileGetJsonAsync的异步方法(与之前的方法多一个参数)
  29. 修改标识:Senparc - 20170409
  30. 修改描述:v4.11.9 修改Download方法
  31. 修改标识:Senparc - 20180928
  32. 修改描述:将 CO2NET 已经移植的方法标记为过期
  33. 修改标识:Senparc - 20190129
  34. 修改描述:统一 CommonJsonSend.Send<T>() 方法请求接口
  35. ----------------------------------------------------------------*/
  36. using System;
  37. using System.Collections.Generic;
  38. using System.IO;
  39. using System.Net;
  40. using System.Security.Cryptography.X509Certificates;
  41. using System.Text;
  42. using System.Threading.Tasks;
  43. using Senparc.Weixin.Entities;
  44. using Senparc.Weixin.Exceptions;
  45. using Senparc.CO2NET.Helpers;
  46. #if NET35 || NET40 || NET45
  47. using System.Web.Script.Serialization;
  48. using Senparc.Weixin.HttpUtility;
  49. #endif
  50. #if !NET35 && !NET40
  51. using System.Net.Http;
  52. #endif
  53. namespace Senparc.Weixin.HttpUtility
  54. {
  55. /// <summary>
  56. /// Post 请求处理
  57. /// </summary>
  58. public static class Post
  59. {
  60. /// <summary>
  61. /// 获取Post结果
  62. /// </summary>
  63. /// <typeparam name="T"></typeparam>
  64. /// <param name="returnText"></param>
  65. /// <returns></returns>
  66. public static T GetResult<T>(string returnText)
  67. {
  68. if (returnText.Contains("errcode"))
  69. {
  70. //可能发生错误
  71. WxJsonResult errorResult = SerializerHelper.GetObject<WxJsonResult>(returnText);
  72. if (errorResult.errcode != ReturnCode.请求成功)
  73. {
  74. //发生错误
  75. throw new ErrorJsonResultException(
  76. string.Format("微信Post请求发生错误!错误代码:{0},说明:{1}",
  77. (int)errorResult.errcode,
  78. errorResult.errmsg),
  79. null, errorResult);
  80. }
  81. }
  82. T result = SerializerHelper.GetObject<T>(returnText);
  83. //TODO:加入特殊情况下的回调处理
  84. return result;
  85. }
  86. }
  87. }