123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Net;
- using System.IO;
- using System.Threading;
- namespace Gets
- {
- public class HttpHelper
- {
- #region 私有变量
- private CookieContainer cc;
- private string contentType = "application/x-www-form-urlencoded";
- 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, */*";
- 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)";
- private Encoding encoding = Encoding.GetEncoding("utf-8");
- private int delay = 300;
- private int maxTry = 3;
- private int currentTry = 0;
- #endregion
- #region 属性
- /// <summary>
- /// Cookie容器
- /// </summary>
- public CookieContainer CookieContainer
- {
- get
- {
- return cc;
- }
- }
- /// <summary>
- /// 获取网页源码时使用的编码
- /// </summary>
- /// <value></value>
- public Encoding Encoding
- {
- get
- {
- return encoding;
- }
- set
- {
- encoding = value;
- }
- }
- public int NetworkDelay
- {
- get
- {
- return delay;
- }
- set
- {
- delay = value;
- }
- }
- public int MaxTry
- {
- get
- {
- return maxTry;
- }
- set
- {
- maxTry = value;
- }
- }
- #endregion
- #region 构造函数
- /// <summary>
- /// Initializes a new instance of the <see cref="HttpHelper"/> class.
- /// </summary>
- public HttpHelper()
- {
- cc = new CookieContainer();
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="HttpHelper"/> class.
- /// </summary>
- /// <param name="cc">The cc.</param>
- public HttpHelper(CookieContainer cc)
- {
- this.cc = cc;
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="HttpHelper"/> class.
- /// </summary>
- /// <param name="contentType">Type of the content.</param>
- /// <param name="accept">The accept.</param>
- /// <param name="userAgent">The user agent.</param>
- public HttpHelper(string contentType, string accept, string userAgent)
- {
- this.contentType = contentType;
- this.accept = accept;
- this.userAgent = userAgent;
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="HttpHelper"/> class.
- /// </summary>
- /// <param name="cc">The cc.</param>
- /// <param name="contentType">Type of the content.</param>
- /// <param name="accept">The accept.</param>
- /// <param name="userAgent">The user agent.</param>
- public HttpHelper(CookieContainer cc, string contentType, string accept, string userAgent)
- {
- this.cc = cc;
- this.contentType = contentType;
- this.accept = accept;
- this.userAgent = userAgent;
- }
- #endregion
- #region 公共方法
- /// <summary>
- /// 获取指定页面的HTML代码
- /// </summary>
- /// <param name="url">指定页面的路径</param>
- /// <param name="postData">回发的数据</param>
- /// <param name="isPost">是否以post方式发送请求</param>
- /// <param name="cookieCollection">Cookie集合</param>
- /// <returns></returns>
- public string GetHtml(string url, string postData, bool isPost, CookieContainer cookieContainer)
- {
- if (string.IsNullOrEmpty(postData))
- {
- return GetHtml(url, cookieContainer);
- }
- Thread.Sleep(delay);
- currentTry++;
- try
- {
- byte[] byteRequest = Encoding.Default.GetBytes(postData);
- HttpWebRequest httpWebRequest;
- httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
- httpWebRequest.CookieContainer = cookieContainer;
- httpWebRequest.ContentType = contentType;
- httpWebRequest.Referer = url;
- httpWebRequest.Accept = accept;
- httpWebRequest.UserAgent = userAgent;
- httpWebRequest.Method = isPost ? "POST" : "GET";
- httpWebRequest.ContentLength = byteRequest.Length;
- Stream stream = httpWebRequest.GetRequestStream();
- stream.Write(byteRequest, 0, byteRequest.Length);
- stream.Close();
- HttpWebResponse httpWebResponse;
- httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
- Stream responseStream = httpWebResponse.GetResponseStream();
- StreamReader streamReader = new StreamReader(responseStream, encoding);
- string html = streamReader.ReadToEnd();
- streamReader.Close();
- responseStream.Close();
- currentTry = 0;
- return html;
- }
- catch (Exception e)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + e.Message);
- Console.ForegroundColor = ConsoleColor.White;
- if (currentTry <= maxTry)
- {
- GetHtml(url, postData, isPost, cookieContainer);
- }
- currentTry = 0;
- return string.Empty;
- }
- }
- /// <summary>
- /// 获取指定页面的HTML代码
- /// </summary>
- /// <param name="url">指定页面的路径</param>
- /// <param name="cookieCollection">Cookie集合</param>
- /// <returns></returns>
- public string GetHtml(string url, CookieContainer cookieContainer)
- {
- Thread.Sleep(delay);
- currentTry++;
- try
- {
- HttpWebRequest httpWebRequest;
- httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
- httpWebRequest.CookieContainer = cookieContainer;
- httpWebRequest.ContentType = contentType;
- httpWebRequest.Referer = url;
- httpWebRequest.Accept = accept;
- httpWebRequest.UserAgent = userAgent;
- httpWebRequest.Method = "GET";
- HttpWebResponse httpWebResponse;
- httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
- Stream responseStream = httpWebResponse.GetResponseStream();
- StreamReader streamReader = new StreamReader(responseStream, encoding);
- string html = streamReader.ReadToEnd();
- streamReader.Close();
- responseStream.Close();
- currentTry = 0;
- return html;
- }
- catch (Exception e)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + e.Message);
- Console.ForegroundColor = ConsoleColor.White;
- if (currentTry <= maxTry)
- {
- GetHtml(url, cookieContainer);
- }
- currentTry = 0;
- return string.Empty;
- }
- }
- /// <summary>
- /// 获取指定页面的HTML代码
- /// </summary>
- /// <param name="url">指定页面的路径</param>
- /// <returns></returns>
- public string GetHtml(string url)
- {
- return GetHtml(url, cc);
- }
- /// <summary>
- /// 获取指定页面的HTML代码
- /// </summary>
- /// <param name="url">指定页面的路径</param>
- /// <param name="postData">回发的数据</param>
- /// <param name="isPost">是否以post方式发送请求</param>
- /// <returns></returns>
- public string GetHtml(string url, string postData, bool isPost)
- {
- return GetHtml(url, postData, isPost, cc);
- }
- /// <summary>
- /// 获取指定页面的Stream
- /// </summary>
- /// <param name="url">指定页面的路径</param>
- /// <param name="postData">回发的数据</param>
- /// <param name="isPost">是否以post方式发送请求</param>
- /// <param name="cookieCollection">Cookie集合</param>
- /// <returns></returns>
- public Stream GetStream(string url, CookieContainer cookieContainer)
- {
- Thread.Sleep(delay);
- currentTry++;
- try
- {
- HttpWebRequest httpWebRequest;
- httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
- httpWebRequest.CookieContainer = cookieContainer;
- httpWebRequest.ContentType = contentType;
- httpWebRequest.Referer = url;
- httpWebRequest.Accept = accept;
- httpWebRequest.UserAgent = userAgent;
- httpWebRequest.Method = "GET";
- HttpWebResponse httpWebResponse;
- httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
- Stream responseStream = httpWebResponse.GetResponseStream();
- currentTry = 0;
- return responseStream;
- }
- catch (Exception e)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + e.Message);
- Console.ForegroundColor = ConsoleColor.White;
- if (currentTry <= maxTry)
- {
- GetHtml(url, cookieContainer);
- }
- currentTry = 0;
- return null;
- }
- }
- public int DownloadFile(string url ,string fileName,System.IO.FileMode fm,int bufferSize)
- {
- try
- {
- var s = GetStream(url, this.CookieContainer);
- System.IO.FileStream fs = new System.IO.FileStream(fileName, fm);
- byte[] buffer = new byte[bufferSize];
- int count = s.Read(buffer, 0, buffer.Length);
- while (count > 0)
- {
- fs.Write(buffer, 0, count);
- count = s.Read(buffer, 0, buffer.Length);
- }
- s.Close();
- fs.Close();
- return 1;
- }
- catch (Exception err)
- {
- return 0;
- throw new Exception(err.Message + "下载文件失败");
- }
- }
- public int DownloadFile(string url, string fileName)
- {
- return DownloadFile(url, fileName, FileMode.Create, 1024);
- }
- #endregion
- }
- }
|