用.NET 抓取一个页面
来源:互联网 发布:一手淘宝买家数据 编辑:程序博客网 时间:2024/06/04 18:05
/// <summary> 02 /// Get a response as a string, given a uri string. 03 /// </summary> 04 /// <param name="uriArg">Specifies a uri such as "http://www.google.com" or @"file://X:\dir\myFile.html"</param> 05 /// <returns>web response as a string.</returns> 06 /// <example> 07 /// try 08 /// { 09 /// string uri = "http://www.google.zzz"; // note bad uri with zzz to exercise exception. 10 /// string s = GetWebPageResponse( uri ); 11 /// Console.WriteLine( s ); 12 /// } 13 /// catch ( WebException ex ) 14 /// { 15 /// // wex.Message could be something like: The remote server returned an error: (404) Not Found. 16 /// string s = string.Format( "Request: {0}\nResult: {1}", uri, ex.Message ); 17 /// Console.WriteLine( s ); 18 /// } 19 /// </example> 20 static string GetWebPageResponse(string uriArg) 21 { 22 Stream responseStream = WebRequest.Create(uriArg).GetResponse().GetResponseStream(); 23 24 StreamReader reader = new StreamReader(responseStream); 25 26 return reader.ReadToEnd(); 27 } 28 29 /// <summary> 30 /// Similar to GetWebPageResponse(string uriArg), but uses a user/pw to log in. 31 /// </summary> 32 /// <param name="uriArg">e.g. "http://192.168.2.1"</param> 33 /// <param name="userArg">e.g. "root"</param> 34 /// <param name="pwArg">e.g. "admin"</param> 35 /// <returns>string containing the http response.</returns> 36 /// <example> 37 /// // Example to get a response with DHCP table from my LinkSys router. 38 /// string s = GetWebPageResponse( "http://192.168.2.1/DHCPTable.htm", "root", "admin" ); 39 /// </example> 40 static string GetWebPageResponse(string uriArg, string userArg, string pwArg) 41 { 42 Uri uri = new Uri(uriArg); 43 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri); 44 CredentialCache creds = new CredentialCache(); 45 46 // See http://msdn.microsoft.com/en-us/library/system.directoryservices.protocols.authtype.aspx for list of types. 47 const string authType = "basic"; 48 49 creds.Add(uri, authType, new NetworkCredential(userArg, pwArg)); 50 req.PreAuthenticate = true; 51 req.Credentials = creds.GetCredential(uri, authType); 52 53 Stream responseStream = req.GetResponse().GetResponseStream(); 54 55 StreamReader reader = new StreamReader(responseStream); 56 57 return reader.ReadToEnd(); 58 }
- 用.NET 抓取一个页面
- ASP.Net抓取页面
- 一个PHP页面抓取函数
- 用TWebBrowser抓取页面
- ASP.NET如何跨站抓取页面
- ASP.NET如何跨站抓取页面
- .net 抓取页面内容查询IP
- asp.net抓取目标页面内容
- asp.net抓取页面内容+下载文件
- python爬虫学习之抓取一个页面
- WebMagic(一)--抓取一个简单的页面
- WebMagic(一)--抓取一个简单的页面
- 抓取页面
- asp.net中抓取远程页面,自动采集示例
- 自己写的一个正则表达式抓取页面内容
- 用Python实现页面内容抓取
- 用C#抓取AJAX页面的内容
- Ubuntu14.04 用curl抓取页面乱码
- 1556
- android笔记3(surfaceview)
- Android OpenGL ES
- 电子书RSS
- MFC 保存对话框扩展名
- 用.NET 抓取一个页面
- spring quartz任务调度
- linux文件编程(1)
- JTree添加选中事件
- 在C++中实现多播委托(1)
- windows帐户管理
- GNU make 的主要预定义变量
- 97年世界编程大赛第一名写的程序……看完很无奈 太牛了
- jxlExport