/// <summary> /// 用HttpWebRequest取得网页源码 /// 对于带BOM的网页很有效,不管是什么编码都能正确识别 /// </summary> /// <param name="url">网页地址" </param> /// <returns>返回网页源文件</returns> public static string GetHtmlSource2(string url) { //处理内容 string html = ""; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Accept = "*/*"; //接受任意文件 request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)"; // 模拟使用IE在浏览 http://www.52mvc.com request.AllowAutoRedirect = true;//是否允许302 //request.CookieContainer = new CookieContainer();//cookie容器, request.Referer = url; //当前页面的引用 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream, Encoding.Default); html = reader.ReadToEnd(); stream.Close(); return html; } //该片段来自于http://outofmemory.cn
收藏
0人收藏
- 2014-05-03 18:22:40jQuery结合C#上传文件的代码 by bargain
- 2014-05-26 13:21:15C# linux FTP client [客户端] by 叠搭宝箱
- 2014-08-25 10:24:46IE(外部浏览器操作) by bargain
- 2014-09-04 10:46:18C# 判断指定URL地址是否可以正常访问 by Jlinhai
- 2014-12-03 10:08:30net判断当前页面是否接收到了Post或Get请求 by 掌心雷
- 2014-05-04 16:50:07C# 使用带用户名和密码的 HTTP PUT 示例 by Jlinhai
- 2014-05-06 17:38:26C#分页显示服务器上指定目录下的所有图片代码 by 流云诸葛
- 2014-05-08 19:00:25net判断是否来自搜索引擎链接 by bargain
- 2014-07-30 10:51:15C#使用Gmail账号发送邮件 by bargain
- 2014-08-11 10:35:48C#网络通信示例 by 迟浩东
- 2014-11-16 11:08:52C# 一个最简单的 TCP 通讯的客户端例子 by 他山之石
相关聚客文章