c#下载网页源码的两种方法
2018-06-23 23:01:56来源:未知 阅读 ()
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
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 使用 Apache SSI(Server Side Includes) 制作多语言版静态网 2020-06-01
- 网页搜索框里显示字段鼠标点击后就隐藏的方法 2020-05-29
- 【2020Python修炼记】前端开发之 网页设计超级酷炫小技巧 2020-05-14
- 如何配置Tomcat上web.xml让浏览器能直接下载txt,xml类型文 2020-04-14
- 使用css将网页变成黑白色 2020-04-04
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