PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Senparc.Weixin.MP/Senparc.Weixin.MP/HttpUtility/RequestUtility.cs

https://github.com/mjhuangzk/WeiXinMPSDK
C# | 186 lines | 112 code | 19 blank | 55 comment | 17 complexity | 42572b93c8b2af62184712b75c7bc76c MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Web;
  8. using Senparc.Weixin.MP.Helpers;
  9. namespace Senparc.Weixin.MP.HttpUtility
  10. {
  11. public static class RequestUtility
  12. {
  13. /// <summary>
  14. /// 使用Get方法获取字符串结果(暂时没有加入Cookie)
  15. /// </summary>
  16. /// <param name="url"></param>
  17. /// <returns></returns>
  18. public static string HttpGet(string url, Encoding encoding = null)
  19. {
  20. WebClient wc = new WebClient();
  21. if (encoding != null)
  22. {
  23. wc.Encoding = encoding;
  24. }
  25. return wc.DownloadString(url);
  26. }
  27. /// <summary>
  28. /// 使用Post方法获取字符串结果
  29. /// </summary>
  30. /// <returns></returns>
  31. public static string HttpPost(string url, CookieContainer cookieContainer = null, Dictionary<string, string> formData = null, Encoding encoding = null)
  32. {
  33. string dataString = GetQueryString(formData);
  34. var formDataBytes = formData == null ? new byte[0] : Encoding.UTF8.GetBytes(dataString);
  35. MemoryStream ms = new MemoryStream();
  36. ms.Write(formDataBytes, 0, formDataBytes.Length);
  37. ms.Seek(0, SeekOrigin.Begin);//设置指针读取位置
  38. return HttpPost(url, cookieContainer, ms, false, encoding);
  39. }
  40. /// <summary>
  41. /// 使用Post方法获取字符串结果
  42. /// </summary>
  43. /// <param name="url"></param>
  44. /// <returns></returns>
  45. public static string HttpPost(string url, CookieContainer cookieContainer = null, string fileName = null, Encoding encoding = null)
  46. {
  47. //读取文件
  48. var fileStream = FileHelper.GetFileStream(fileName);
  49. return HttpPost(url, cookieContainer, fileStream, true, encoding);
  50. }
  51. /// <summary>
  52. /// 使用Post方法获取字符串结果
  53. /// </summary>
  54. /// <param name="url"></param>
  55. /// <param name="cookieContainer"></param>
  56. /// <param name="postStream"></param>
  57. /// <param name="isFile">postStreams是否是文件流</param>
  58. /// <returns></returns>
  59. public static string HttpPost(string url, CookieContainer cookieContainer = null, Stream postStream = null, bool isFile = false, Encoding encoding = null)
  60. {
  61. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  62. request.Method = "POST";
  63. request.ContentType = "application/x-www-form-urlencoded";
  64. request.ContentLength = postStream != null ? postStream.Length : 0;
  65. if (cookieContainer != null)
  66. {
  67. request.CookieContainer = cookieContainer;
  68. }
  69. if (postStream != null)
  70. {
  71. //上传文件流
  72. Stream requestStream = request.GetRequestStream();
  73. byte[] buffer = new byte[1024];
  74. int bytesRead = 0;
  75. while ((bytesRead = postStream.Read(buffer, 0, buffer.Length)) != 0)
  76. {
  77. requestStream.Write(buffer, 0, bytesRead);
  78. }
  79. postStream.Close();//关闭文件访问
  80. }
  81. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  82. if (cookieContainer != null)
  83. {
  84. response.Cookies = cookieContainer.GetCookies(response.ResponseUri);
  85. }
  86. using (Stream responseStream = response.GetResponseStream())
  87. {
  88. using (StreamReader myStreamReader = new StreamReader(responseStream, encoding ?? Encoding.GetEncoding("utf-8")))
  89. {
  90. string retString = myStreamReader.ReadToEnd();
  91. return retString;
  92. }
  93. }
  94. }
  95. /// <summary>
  96. /// 请求是否发起自微信客户端的浏览器
  97. /// </summary>
  98. /// <param name="httpContext"></param>
  99. /// <returns></returns>
  100. public static bool IsWeixinClientRequest(this HttpContext httpContext)
  101. {
  102. return !string.IsNullOrEmpty(httpContext.Request.UserAgent) &&
  103. httpContext.Request.UserAgent.Contains("MicroMessenger");
  104. }
  105. /// <summary>
  106. /// 组装QueryString的方法
  107. /// 参数之间用&连接,首位没有符号,如:a=1&b=2&c=3
  108. /// </summary>
  109. /// <param name="formData"></param>
  110. /// <returns></returns>
  111. public static string GetQueryString(this Dictionary<string, string> formData)
  112. {
  113. if (formData == null || formData.Count == 0)
  114. {
  115. return "";
  116. }
  117. StringBuilder sb = new StringBuilder();
  118. var i = 0;
  119. foreach (var kv in formData)
  120. {
  121. i++;
  122. sb.AppendFormat("{0}={1}", kv.Key, kv.Value);
  123. if (i < formData.Count)
  124. {
  125. sb.Append("&");
  126. }
  127. }
  128. return sb.ToString();
  129. }
  130. /// <summary>
  131. /// 封装System.Web.HttpUtility.HtmlEncode
  132. /// </summary>
  133. /// <param name="html"></param>
  134. /// <returns></returns>
  135. public static string HtmlEncode(this string html)
  136. {
  137. return System.Web.HttpUtility.HtmlEncode(html);
  138. }
  139. /// <summary>
  140. /// 封装System.Web.HttpUtility.HtmlDecode
  141. /// </summary>
  142. /// <param name="html"></param>
  143. /// <returns></returns>
  144. public static string HtmlDecode(this string html)
  145. {
  146. return System.Web.HttpUtility.HtmlDecode(html);
  147. }
  148. /// <summary>
  149. /// 封装System.Web.HttpUtility.UrlEncode
  150. /// </summary>
  151. /// <param name="url"></param>
  152. /// <returns></returns>
  153. public static string UrlEncode(this string url)
  154. {
  155. return System.Web.HttpUtility.UrlEncode(url);
  156. }
  157. /// <summary>
  158. /// 封装System.Web.HttpUtility.UrlDecode
  159. /// </summary>
  160. /// <param name="url"></param>
  161. /// <returns></returns>
  162. public static string UrlDecode(this string url)
  163. {
  164. return System.Web.HttpUtility.UrlDecode(url);
  165. }
  166. }
  167. }