Get.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. 文件名:Get.cs
  17. 文件功能描述:Get
  18. 创建标识:Senparc - 20150211
  19. 修改标识:Senparc - 20150303
  20. 修改描述:整理接口
  21. 修改标识:zeje - 20160422
  22. 修改描述:v4.5.19 为GetJson方法添加maxJsonLength参数
  23. 修改标识:zeje - 20170305
  24. 修改描述:MP v14.3.132 添加Get.DownloadAsync(string url, string dir)方法
  25. 修改标识:Senparc - 20170409
  26. 修改描述:v4.11.9 修改Download方法
  27. 修改标识:Senparc - 20171101
  28. 修改描述:v4.18.1 修改Get.Download()方法
  29. 修改标识:Senparc - 20180114
  30. 修改描述:v4.18.13 修改 HttpUtility.Get.Download() 方法,
  31. 根据 Content-Disposition 中的文件名储存文件
  32. 修改标识:Senparc - 20180407
  33. 修改描述:v14.10.13 优化 Get.Download() 方法,完善对 FileName 的判断
  34. 修改标识:Senparc - 20180928
  35. 修改描述:将 CO2NET 已经移植的方法标记为过期
  36. 修改标识:Senparc - 20190129
  37. 修改描述:统一 CommonJsonSend.Send<T>() 方法请求接口
  38. ----------------------------------------------------------------*/
  39. using System;
  40. using System.Collections.Generic;
  41. using System.IO;
  42. using System.Net;
  43. #if !NET35 && !NET40
  44. using System.Net.Http;
  45. using System.Threading.Tasks;
  46. #endif
  47. using System.Text;
  48. #if NET35 || NET40 || NET45
  49. using System.Web.Script.Serialization;
  50. #endif
  51. using Senparc.Weixin.Entities;
  52. using Senparc.Weixin.Exceptions;
  53. using System.Text.RegularExpressions;
  54. namespace Senparc.Weixin.HttpUtility
  55. {
  56. /// <summary>
  57. /// Get 请求处理
  58. /// </summary>
  59. public static class Get
  60. {
  61. /// <summary>
  62. /// 获取随机文件名
  63. /// </summary>
  64. /// <returns></returns>
  65. private static string GetRandomFileName()
  66. {
  67. return SystemTime.Now.ToString("yyyyMMdd-HHmmss") + Guid.NewGuid().ToString("n").Substring(0, 6);
  68. }
  69. /// <summary>
  70. /// 获得JSON文本结果之后、序列化之前进行的操作
  71. /// </summary>
  72. internal static Action<string, string> AfterReturnText = (_url, returnText) =>
  73. {
  74. //TODO:已经在 CommonJsonSend 中单独实现
  75. WeixinTrace.SendApiLog(_url, returnText);
  76. if (returnText.Contains("errcode"))
  77. {
  78. //可能发生错误
  79. WxJsonResult errorResult = CO2NET.Helpers.SerializerHelper.GetObject<WxJsonResult>(returnText);
  80. if (errorResult.errcode != ReturnCode.请求成功)
  81. {
  82. //发生错误
  83. throw new ErrorJsonResultException(
  84. string.Format("微信请求发生错误!错误代码:{0},说明:{1}",
  85. (int)errorResult.errcode, errorResult.errmsg), null, errorResult, _url);
  86. }
  87. }
  88. };
  89. }
  90. }