c#下载网页源码的两种方法
2018-06-22 06:50:21来源:未知 阅读 ()
1、WebClient:
System.Net.WebClient wc = new System.Net.WebClient(); Byte[] pageData = wc.DownloadData("网页地址"); string s= System.Text.Encoding.Default.GetString(pageData); //s = System.Text.Encoding.UTF8.GetString(pageData);去除中文乱码
2、HttpRequest:
1 static class WebFunc 2 { 3 private static CookieContainer cookie = new CookieContainer(); 4 private static string contentType = "application/x-www-form-urlencoded"; 5 private static 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, */*"; 6 private static 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)"; 7 8 /// <summary> 9 /// 10 /// </summary> 11 /// <param name="url">网页地址</param> 12 /// <param name="encoding">编码方式</param> 13 /// <returns></returns> 14 public static string GetHtmlEx(string url, Encoding encoding) 15 { 16 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 17 request.UserAgent = userAgent; 18 request.ContentType = contentType; 19 request.CookieContainer = cookie; 20 request.Accept = accept; 21 request.Method = "get"; 22 23 WebResponse response = request.GetResponse(); 24 Stream responseStream = response.GetResponseStream(); 25 StreamReader reader = new StreamReader(responseStream, encoding); 26 String html = reader.ReadToEnd(); 27 response.Close(); 28 29 return html; 30 } 31 }
转自http://blog.csdn.net/adsdassadfasdfasdf/article/details/5831347
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:本地测试IIS,Post调用接口
下一篇:VS2010 快捷键--我的总结
- PHP session实现购物车功能 2020-03-23
- asp.net网页里面为什么找不到CS文件 2020-03-09
- HTML标签删除HTML示例代码 2020-03-08
- mvc file控件无刷新异步上传操作源码 2020-02-27
- MVC异步分页代码分享 2020-02-21
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash