C#获取页面的HTML
类别:编程学习 浏览量:719
时间:2013-9-16 C#获取页面的HTML
C#获取页面的HTMLC#提取HTML代码的方法:
public class HTMLHelper { 私有字段 /// <summary> /// 获取HTML /// </summary> /// <param name="url">地址</param> /// <param name="postData">post 提交的字符串</param> /// <param name="isPost">是否是post</param> /// <param name="cookieContainer">CookieContainer</param> public static string GetHtml(string url, string postData, bool isPost, CookieContainer cookieContainer) { if (string.IsNullOrEmpty(postData)) return GetHtml(url, cookieContainer); Thread.Sleep(NetworkDelay); currentTry++; HttpWebRequest httpWebRequest = null; HttpWebResponse httpWebResponse = null; try { byte[] byteRequest = Encoding.Default.GetBytes(postData); httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url); httpWebRequest.CookieContainer = cookieContainer; httpWebRequest.ContentType = contentType; httpWebRequest.ServicePoint.ConnectionLimit = maxTry; 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)httpWebRequest.GetResponse(); Stream responseStream = httpWebResponse.GetResponseStream(); StreamReader streamReader = new StreamReader(responseStream, encoding); string html = streamReader.ReadToEnd(); streamReader.Close(); responseStream.Close(); currentTry = 0; httpWebRequest.Abort(); httpWebResponse.Close(); return html; } catch (Exception e) { if (currentTry <= maxTry) GetHtml(url, postData, isPost, cookieContainer); currentTry--; if (httpWebRequest != null) httpWebRequest.Abort(); if (httpWebResponse != null) httpWebResponse.Close(); return string.Empty; } } /// <summary> /// 获取HTML /// </summary> /// <param name="url">地址</param> /// <param name="cookieContainer">CookieContainer</param> public static string GetHtml(string url, CookieContainer cookieContainer) { Thread.Sleep(NetworkDelay); currentTry++; HttpWebRequest httpWebRequest = null; HttpWebResponse httpWebResponse = null; try { httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url); httpWebRequest.CookieContainer = cookieContainer; httpWebRequest.ContentType = contentType; httpWebRequest.ServicePoint.ConnectionLimit = maxTry; httpWebRequest.Referer = url; httpWebRequest.Accept = accept; httpWebRequest.UserAgent = userAgent; httpWebRequest.Method = "GET"; httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); Stream responseStream = httpWebResponse.GetResponseStream(); StreamReader streamReader = new StreamReader(responseStream, encoding); string html = streamReader.ReadToEnd(); streamReader.Close(); responseStream.Close(); currentTry--; httpWebRequest.Abort(); httpWebResponse.Close(); return html; } catch (Exception e) { if (currentTry <= maxTry) GetHtml(url, cookieContainer); currentTry--; if (httpWebRequest != null) httpWebRequest.Abort(); if (httpWebResponse != null) httpWebResponse.Close(); return string.Empty; } } }
您可能感兴趣
- C# 获取磁盘空间大小
- C# Checklistbox的用法
- C#递归的应用
- C#中is 运算符与as运算符的区别和作用
- C#中List和数组之间的转换
- C#中ToString()常见的格式
- C#中yield
- C#如何读取csv格式文件
- C#中static静态变量的用法
- C#中属性的用法
- C#中string.format的格式和用法
- C#中float的取值范围和精度
- C#排序算法的比较
- C# Directory类的操作
- 数据库字段类型对应C#数据类型
- C#获取上个月第一天和最后一天
- 我们现在吃的苹果是哪里来的 原来现代苹果引入中国仅有一百多年(我们现在吃的苹果是哪里来的)
- 买绿宝不能只挑黄绿色 菜农教你3招挑,个个皮薄肉脆,香甜爆汁(买绿宝不能只挑黄绿色)
- 大果肉搭配薄瓜皮, 绿宝 脆甜爽口,不愧是甜瓜中的 佼佼者(大果肉搭配薄瓜皮)
- 河南尉氏县因地制宜发展果蔬种植 水坡镇绿宝甜瓜变 金瓜(河南尉氏县因地制宜发展果蔬种植)
- 谢广坤,你这么欺负谢腾飞,良心不会痛吗(你这么欺负谢腾飞)
- 乡村爱情15 宋晓峰怀疑自己孩子,腾飞与姜奶奶亲子鉴定出结果(宋晓峰怀疑自己孩子)
热门推荐
- dedecms标签怎么调用(DedeCMS织梦后台模板列表如何按字母排序方法代码)
- laravel 后台管理框架(关于Laravel-admin的基础用法总结和自定义model详解)
- SqlServer 表单查询问题及解决方法(SqlServer 表单查询问题及解决方法)
- php符串使用教程(php使用scandir函数扫描指定目录下所有文件示例)
- 如何让Select下拉框具有输入功能
- mysql数据库间的连接(MySQL中表的几种连接方式)
- jQuery里$(this)和this的区别有哪些
- mysql xml转换json(Mysql将查询结果集转换为JSON数据的实例代码)
- sql怎么查询字段合并(SQL函数将某个字段合并在一起的操作)
- nginx代理转发域名(Nginx域名转发使用场景代码实例)