用.NET 抓取一个页面

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


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值