HttpUtility.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.IO;
  7. namespace WeiXin.Library.Utility
  8. {
  9. class HttpUtility
  10. {
  11. /// <summary>
  12. /// 发送请求
  13. /// </summary>
  14. /// <param name="url">Url地址</param>
  15. /// <param name="data">数据</param>
  16. public static string SendHttpRequest(string url, string data)
  17. {
  18. return SendPostHttpRequest(url, "application/x-www-form-urlencoded", data);
  19. }
  20. /// <summary>
  21. ///
  22. /// </summary>
  23. /// <param name="url"></param>
  24. /// <returns></returns>
  25. public static string GetData(string url)
  26. {
  27. return SendGetHttpRequest(url, "application/x-www-form-urlencoded");
  28. }
  29. /// <summary>
  30. /// 发送请求
  31. /// </summary>
  32. /// <param name="url">Url地址</param>
  33. /// <param name="method">方法(post或get)</param>
  34. /// <param name="method">数据类型</param>
  35. /// <param name="requestData">数据</param>
  36. public static string SendPostHttpRequest(string url, string contentType, string requestData)
  37. {
  38. WebRequest request = (WebRequest)HttpWebRequest.Create(url);
  39. request.Method = "POST";
  40. byte[] postBytes = null;
  41. request.ContentType = contentType;
  42. postBytes = Encoding.UTF8.GetBytes(requestData);
  43. request.ContentLength = postBytes.Length;
  44. using (Stream outstream = request.GetRequestStream())
  45. {
  46. outstream.Write(postBytes, 0, postBytes.Length);
  47. }
  48. string result = string.Empty;
  49. using (WebResponse response = request.GetResponse())
  50. {
  51. if (response != null)
  52. {
  53. using (Stream stream = response.GetResponseStream())
  54. {
  55. using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
  56. {
  57. result = reader.ReadToEnd();
  58. }
  59. }
  60. }
  61. }
  62. return result;
  63. }
  64. /// <summary>
  65. /// 发送请求
  66. /// </summary>
  67. /// <param name="url">Url地址</param>
  68. /// <param name="method">方法(post或get)</param>
  69. /// <param name="method">数据类型</param>
  70. /// <param name="requestData">数据</param>
  71. public static string SendGetHttpRequest(string url, string contentType)
  72. {
  73. WebRequest request = (WebRequest)HttpWebRequest.Create(url);
  74. request.Method = "GET";
  75. request.ContentType = contentType;
  76. string result = string.Empty;
  77. using (WebResponse response = request.GetResponse())
  78. {
  79. if (response != null)
  80. {
  81. using (Stream stream = response.GetResponseStream())
  82. {
  83. using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
  84. {
  85. result = reader.ReadToEnd();
  86. }
  87. }
  88. }
  89. }
  90. return result;
  91. }
  92. }
  93. }