实例介绍
【实例简介】
【实例截图】
【核心代码】
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 = 3000; private int maxTry = 3; private int currentTry = 0; #endregion public static FrmValidateCode frmValidateCode; #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 { Random r = new Random(); return (r.Next(delay / 1000, delay / 1000 * 2)) * 1000; } 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); } 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; if (html.Contains("validateCode")) { if (string.IsNullOrEmpty(QQ.FriendQQ)) { QQ.FriendQQ = QQ.CurrentQQ; } if (frmValidateCode == null) { frmValidateCode = new FrmValidateCode();//将主窗体对象传递过去 frmValidateCode.ShowDialog(); } else { frmValidateCode.Activate(); if (!frmValidateCode.Visible) { frmValidateCode.ShowDialog(); } } return null; } new Log().WriteLog(html); //记录每一次请求的html代码 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(NetworkDelay); 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; if (html.Contains("validateCode")) { if (string.IsNullOrEmpty(QQ.FriendQQ)) { QQ.FriendQQ = QQ.CurrentQQ; } if (frmValidateCode == null) { frmValidateCode = new FrmValidateCode();//将主窗体对象传递过去 frmValidateCode.ShowDialog(); } else { frmValidateCode.Activate(); if (!frmValidateCode.Visible) { frmValidateCode.ShowDialog(); } } return null; } 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) { 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; CookieCollection cc = cookieContainer.GetCookies(new Uri(url)); 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; } } #endregion }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论