HttpHelper.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.IO;
  6. using System.Threading;
  7. namespace Gets
  8. {
  9. public class HttpHelper
  10. {
  11. #region 私有变量
  12. private CookieContainer cc;
  13. private string contentType = "application/x-www-form-urlencoded";
  14. private string accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*";
  15. private string userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
  16. private Encoding encoding = Encoding.GetEncoding("utf-8");
  17. private int delay = 300;
  18. private int maxTry = 3;
  19. private int currentTry = 0;
  20. #endregion
  21. #region 属性
  22. /// <summary>
  23. /// Cookie容器
  24. /// </summary>
  25. public CookieContainer CookieContainer
  26. {
  27. get
  28. {
  29. return cc;
  30. }
  31. }
  32. /// <summary>
  33. /// 获取网页源码时使用的编码
  34. /// </summary>
  35. /// <value></value>
  36. public Encoding Encoding
  37. {
  38. get
  39. {
  40. return encoding;
  41. }
  42. set
  43. {
  44. encoding = value;
  45. }
  46. }
  47. public int NetworkDelay
  48. {
  49. get
  50. {
  51. return delay;
  52. }
  53. set
  54. {
  55. delay = value;
  56. }
  57. }
  58. public int MaxTry
  59. {
  60. get
  61. {
  62. return maxTry;
  63. }
  64. set
  65. {
  66. maxTry = value;
  67. }
  68. }
  69. #endregion
  70. #region 构造函数
  71. /// <summary>
  72. /// Initializes a new instance of the <see cref="HttpHelper"/> class.
  73. /// </summary>
  74. public HttpHelper()
  75. {
  76. cc = new CookieContainer();
  77. }
  78. /// <summary>
  79. /// Initializes a new instance of the <see cref="HttpHelper"/> class.
  80. /// </summary>
  81. /// <param name="cc">The cc.</param>
  82. public HttpHelper(CookieContainer cc)
  83. {
  84. this.cc = cc;
  85. }
  86. /// <summary>
  87. /// Initializes a new instance of the <see cref="HttpHelper"/> class.
  88. /// </summary>
  89. /// <param name="contentType">Type of the content.</param>
  90. /// <param name="accept">The accept.</param>
  91. /// <param name="userAgent">The user agent.</param>
  92. public HttpHelper(string contentType, string accept, string userAgent)
  93. {
  94. this.contentType = contentType;
  95. this.accept = accept;
  96. this.userAgent = userAgent;
  97. }
  98. /// <summary>
  99. /// Initializes a new instance of the <see cref="HttpHelper"/> class.
  100. /// </summary>
  101. /// <param name="cc">The cc.</param>
  102. /// <param name="contentType">Type of the content.</param>
  103. /// <param name="accept">The accept.</param>
  104. /// <param name="userAgent">The user agent.</param>
  105. public HttpHelper(CookieContainer cc, string contentType, string accept, string userAgent)
  106. {
  107. this.cc = cc;
  108. this.contentType = contentType;
  109. this.accept = accept;
  110. this.userAgent = userAgent;
  111. }
  112. #endregion
  113. #region 公共方法
  114. /// <summary>
  115. /// 获取指定页面的HTML代码
  116. /// </summary>
  117. /// <param name="url">指定页面的路径</param>
  118. /// <param name="postData">回发的数据</param>
  119. /// <param name="isPost">是否以post方式发送请求</param>
  120. /// <param name="cookieCollection">Cookie集合</param>
  121. /// <returns></returns>
  122. public string GetHtml(string url, string postData, bool isPost, CookieContainer cookieContainer)
  123. {
  124. if (string.IsNullOrEmpty(postData))
  125. {
  126. return GetHtml(url, cookieContainer);
  127. }
  128. Thread.Sleep(delay);
  129. currentTry++;
  130. try
  131. {
  132. byte[] byteRequest = Encoding.Default.GetBytes(postData);
  133. HttpWebRequest httpWebRequest;
  134. httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
  135. httpWebRequest.CookieContainer = cookieContainer;
  136. httpWebRequest.ContentType = contentType;
  137. httpWebRequest.Referer = url;
  138. httpWebRequest.Accept = accept;
  139. httpWebRequest.UserAgent = userAgent;
  140. httpWebRequest.Method = isPost ? "POST" : "GET";
  141. httpWebRequest.ContentLength = byteRequest.Length;
  142. Stream stream = httpWebRequest.GetRequestStream();
  143. stream.Write(byteRequest, 0, byteRequest.Length);
  144. stream.Close();
  145. HttpWebResponse httpWebResponse;
  146. httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
  147. Stream responseStream = httpWebResponse.GetResponseStream();
  148. StreamReader streamReader = new StreamReader(responseStream, encoding);
  149. string html = streamReader.ReadToEnd();
  150. streamReader.Close();
  151. responseStream.Close();
  152. currentTry = 0;
  153. return html;
  154. }
  155. catch (Exception e)
  156. {
  157. Console.ForegroundColor = ConsoleColor.Red;
  158. Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + e.Message);
  159. Console.ForegroundColor = ConsoleColor.White;
  160. if (currentTry <= maxTry)
  161. {
  162. GetHtml(url, postData, isPost, cookieContainer);
  163. }
  164. currentTry = 0;
  165. return string.Empty;
  166. }
  167. }
  168. /// <summary>
  169. /// 获取指定页面的HTML代码
  170. /// </summary>
  171. /// <param name="url">指定页面的路径</param>
  172. /// <param name="cookieCollection">Cookie集合</param>
  173. /// <returns></returns>
  174. public string GetHtml(string url, CookieContainer cookieContainer)
  175. {
  176. Thread.Sleep(delay);
  177. currentTry++;
  178. try
  179. {
  180. HttpWebRequest httpWebRequest;
  181. httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
  182. httpWebRequest.CookieContainer = cookieContainer;
  183. httpWebRequest.ContentType = contentType;
  184. httpWebRequest.Referer = url;
  185. httpWebRequest.Accept = accept;
  186. httpWebRequest.UserAgent = userAgent;
  187. httpWebRequest.Method = "GET";
  188. HttpWebResponse httpWebResponse;
  189. httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
  190. Stream responseStream = httpWebResponse.GetResponseStream();
  191. StreamReader streamReader = new StreamReader(responseStream, encoding);
  192. string html = streamReader.ReadToEnd();
  193. streamReader.Close();
  194. responseStream.Close();
  195. currentTry = 0;
  196. return html;
  197. }
  198. catch (Exception e)
  199. {
  200. Console.ForegroundColor = ConsoleColor.Red;
  201. Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + e.Message);
  202. Console.ForegroundColor = ConsoleColor.White;
  203. if (currentTry <= maxTry)
  204. {
  205. GetHtml(url, cookieContainer);
  206. }
  207. currentTry = 0;
  208. return string.Empty;
  209. }
  210. }
  211. /// <summary>
  212. /// 获取指定页面的HTML代码
  213. /// </summary>
  214. /// <param name="url">指定页面的路径</param>
  215. /// <returns></returns>
  216. public string GetHtml(string url)
  217. {
  218. return GetHtml(url, cc);
  219. }
  220. /// <summary>
  221. /// 获取指定页面的HTML代码
  222. /// </summary>
  223. /// <param name="url">指定页面的路径</param>
  224. /// <param name="postData">回发的数据</param>
  225. /// <param name="isPost">是否以post方式发送请求</param>
  226. /// <returns></returns>
  227. public string GetHtml(string url, string postData, bool isPost)
  228. {
  229. return GetHtml(url, postData, isPost, cc);
  230. }
  231. /// <summary>
  232. /// 获取指定页面的Stream
  233. /// </summary>
  234. /// <param name="url">指定页面的路径</param>
  235. /// <param name="postData">回发的数据</param>
  236. /// <param name="isPost">是否以post方式发送请求</param>
  237. /// <param name="cookieCollection">Cookie集合</param>
  238. /// <returns></returns>
  239. public Stream GetStream(string url, CookieContainer cookieContainer)
  240. {
  241. Thread.Sleep(delay);
  242. currentTry++;
  243. try
  244. {
  245. HttpWebRequest httpWebRequest;
  246. httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
  247. httpWebRequest.CookieContainer = cookieContainer;
  248. httpWebRequest.ContentType = contentType;
  249. httpWebRequest.Referer = url;
  250. httpWebRequest.Accept = accept;
  251. httpWebRequest.UserAgent = userAgent;
  252. httpWebRequest.Method = "GET";
  253. HttpWebResponse httpWebResponse;
  254. httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
  255. Stream responseStream = httpWebResponse.GetResponseStream();
  256. currentTry = 0;
  257. return responseStream;
  258. }
  259. catch (Exception e)
  260. {
  261. Console.ForegroundColor = ConsoleColor.Red;
  262. Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + e.Message);
  263. Console.ForegroundColor = ConsoleColor.White;
  264. if (currentTry <= maxTry)
  265. {
  266. GetHtml(url, cookieContainer);
  267. }
  268. currentTry = 0;
  269. return null;
  270. }
  271. }
  272. public int DownloadFile(string url ,string fileName,System.IO.FileMode fm,int bufferSize)
  273. {
  274. try
  275. {
  276. var s = GetStream(url, this.CookieContainer);
  277. System.IO.FileStream fs = new System.IO.FileStream(fileName, fm);
  278. byte[] buffer = new byte[bufferSize];
  279. int count = s.Read(buffer, 0, buffer.Length);
  280. while (count > 0)
  281. {
  282. fs.Write(buffer, 0, count);
  283. count = s.Read(buffer, 0, buffer.Length);
  284. }
  285. s.Close();
  286. fs.Close();
  287. return 1;
  288. }
  289. catch (Exception err)
  290. {
  291. return 0;
  292. throw new Exception(err.Message + "下载文件失败");
  293. }
  294. }
  295. public int DownloadFile(string url, string fileName)
  296. {
  297. return DownloadFile(url, fileName, FileMode.Create, 1024);
  298. }
  299. #endregion
  300. }
  301. }