HttpToolPlus.cs 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. namespace Azylee.YeahWeb.HttpUtils
  8. {
  9. public class HttpToolPlus
  10. {
  11. const string DefaultContentType = "application/x-www-form-urlencoded";
  12. const string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET4.0C; .NET4.0E)";
  13. public static string Get(string url, ref CookieCollection cookie, string contentType = DefaultContentType, bool autoRedirect = false, bool keepAlive = true, string userAgent = DefaultUserAgent)
  14. {
  15. string html = "";
  16. Stream stream = null;
  17. StreamReader reader = null;
  18. try
  19. {
  20. HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
  21. request.Method = "GET";
  22. request.ContentType = DefaultContentType;
  23. request.AllowAutoRedirect = autoRedirect;
  24. request.KeepAlive = keepAlive;
  25. request.UserAgent = DefaultUserAgent;
  26. request.CookieContainer = new CookieContainer();
  27. if (cookie != null) request.CookieContainer.Add(cookie);
  28. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  29. response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
  30. cookie = response.Cookies;
  31. stream = response.GetResponseStream();
  32. reader = new StreamReader(stream, Encoding.Default);
  33. html = reader.ReadToEnd();
  34. }
  35. catch
  36. {
  37. }
  38. finally
  39. {
  40. if (reader != null) reader.Close();
  41. if (stream != null) stream.Close();
  42. }
  43. return html;
  44. }
  45. public static string Post(string url, ref CookieCollection cookie, Dictionary<string, string> data = null, string contentType = DefaultContentType, bool autoRedirect = true, bool keepAlive = true, string userAgent = DefaultUserAgent)
  46. {
  47. string html = "";
  48. Stream stream = null, dataStream = null;
  49. StreamReader reader = null;
  50. try
  51. {
  52. //配置属性
  53. HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
  54. request.Method = "POST";
  55. request.ContentType = DefaultContentType;
  56. request.AllowAutoRedirect = autoRedirect;
  57. request.KeepAlive = keepAlive;
  58. request.UserAgent = DefaultUserAgent;
  59. request.CookieContainer = new CookieContainer();
  60. if (cookie != null) request.CookieContainer.Add(cookie);
  61. //配置参数
  62. if (data != null)
  63. {
  64. StringBuilder dataString = new StringBuilder();
  65. foreach (var item in data)
  66. {
  67. string param = string.Format("&{0}={1}", item.Key, item.Value);
  68. dataString.Append(param);
  69. }
  70. byte[] dataByte = Encoding.UTF8.GetBytes(dataString.ToString().Substring(1));
  71. request.ContentLength = dataByte.Length;
  72. dataStream = request.GetRequestStream();
  73. dataStream.Write(dataByte, 0, dataByte.Length);
  74. }
  75. //请求数据
  76. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  77. response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
  78. cookie = response.Cookies;
  79. stream = response.GetResponseStream();
  80. reader = new StreamReader(stream, Encoding.Default);
  81. html = reader.ReadToEnd();
  82. }
  83. catch
  84. {
  85. }
  86. finally
  87. {
  88. if (reader != null) reader.Close();
  89. if (stream != null) stream.Close();
  90. if (dataStream != null) dataStream.Close();
  91. }
  92. return html;
  93. }
  94. }
  95. }