c#下载网页源码的两种方法

2018-06-18 04:49:09来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

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);去除中文乱码 
WebClient

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 }  
HttpRequest

转自http://blog.csdn.net/adsdassadfasdfasdf/article/details/5831347

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:VS2010 快捷键--我的总结

下一篇:MyKTV项目,走起!