2012-05-11 27 views
0

這是類:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using HtmlAgilityPack; 
using System.Net; 

namespace GatherLinks 
{ 
    /// <summary> 
    /// A result encapsulating the Url and the HtmlDocument 
    /// </summary> 
    class WebPage 
    { 
     public Uri Url { get; set; } 

     /// <summary> 
     /// Get every WebPage.Internal on a web site (or part of a web site) visiting all internal links just once 
     /// plus every external page (or other Url) linked to the web site as a WebPage.External 
     /// </summary> 
     /// <remarks> 
     /// Use .OfType WebPage.Internal to get just the internal ones if that's what you want 
     /// </remarks> 
     public static IEnumerable<WebPage> GetAllPagesUnder(Uri urlRoot) 
     { 
      var queue = new Queue<Uri>(); 
      var allSiteUrls = new HashSet<Uri>(); 

      queue.Enqueue(urlRoot); 
      allSiteUrls.Add(urlRoot); 

      while (queue.Count > 0) 
      { 
       Uri url = queue.Dequeue(); 

       HttpWebRequest oReq = (HttpWebRequest)WebRequest.Create(url); 
       oReq.UserAgent = @"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5"; 

       HttpWebResponse resp = (HttpWebResponse)oReq.GetResponse(); 

       WebPage result; 

       if (resp.ContentType.StartsWith("text/html", StringComparison.InvariantCultureIgnoreCase)) 
       { 
        HtmlDocument doc = new HtmlDocument(); 
        try 
        { 
         var resultStream = resp.GetResponseStream(); 
         doc.Load(resultStream); // The HtmlAgilityPack 
         result = new Internal() { Url = url, HtmlDocument = doc }; 
        } 
        catch (System.Net.WebException ex) 
        { 
         result = new WebPage.Error() { Url = url, Exception = ex }; 
        } 
        catch (Exception ex) 
        { 
         ex.Data.Add("Url", url); // Annotate the exception with the Url 
         throw; 
        } 

        // Success, hand off the page 
        yield return new WebPage.Internal() { Url = url, HtmlDocument = doc }; 

        // And and now queue up all the links on this page 
        foreach (HtmlNode link in doc.DocumentNode.SelectNodes(@"//a[@href]")) 
        { 
         HtmlAttribute att = link.Attributes["href"]; 
         if (att == null) continue; 
         string href = att.Value; 
         if (href.StartsWith("javascript", StringComparison.InvariantCultureIgnoreCase)) continue;  // ignore javascript on buttons using a tags 

         Uri urlNext = new Uri(href, UriKind.RelativeOrAbsolute); 

         // Make it absolute if it's relative 
         if (!urlNext.IsAbsoluteUri) 
         { 
          urlNext = new Uri(urlRoot, urlNext); 
         } 

         if (!allSiteUrls.Contains(urlNext)) 
         { 
          allSiteUrls.Add(urlNext);    // keep track of every page we've handed off 

          if (urlRoot.IsBaseOf(urlNext)) 
          { 
           queue.Enqueue(urlNext); 
          } 
          else 
          { 
           yield return new WebPage.External() { Url = urlNext }; 
          } 
         } 
        } 
       } 
      } 
     } 

     ///// <summary> 
     ///// In the future might provide all the images too?? 
     ///// </summary> 
     //public class Image : WebPage 
     //{ 
     //} 

     /// <summary> 
     /// Error loading page 
     /// </summary> 
     public class Error : WebPage 
     { 
      public int HttpResult { get; set; } 
      public Exception Exception { get; set; } 
     } 

     /// <summary> 
     /// External page - not followed 
     /// </summary> 
     /// <remarks> 
     /// No body - go load it yourself 
     /// </remarks> 
     public class External : WebPage 
     { 
     } 

     /// <summary> 
     /// Internal page 
     /// </summary> 
     public class Internal : WebPage 
     { 
      /// <summary> 
      /// For internal pages we load the document for you 
      /// </summary> 
      public virtual HtmlDocument HtmlDocument { get; internal set; } 
     } 
    } 
} 

它永遠不會停止在這條線:

public Uri Url { get; set; } 

而且從來沒有停止對其他任何這一類的線。只有當我刪除線:

public Uri Url { get; set; } 

然後它停止在其他線路上。但我不明白爲什麼它不停止在第一行?我該如何解決它?

我試過了解有關自動屬性,但我不明白它是什麼,我不想在這個類中使用它。

+1

斷點只會停在方法內的代碼上。 –

回答

1

斷點是not supportedauto-implemented properties。嘗試在GetAllPagesUnder方法的第一行中進行設置。

+0

@AndrasZoltan - 我錯過了繼承類。謝謝。 – Oded

+0

耶不奇怪,有很多要看:) –

+0

@AndrasZoltan - 嘿。第一次我想使用[堆棧溢出不會讀所有](http://meta.stackexchange.com/a/129787/140505)... – Oded

0

這是從你的問題很模糊 - 但我有一種預感,在某處你的程序你打電話:

var results = WebPage.GetAllPagesUnder([some uri]); 

而您希望斷點時調用該方法被打?

它不會 - 它會產生一個枚舉器。代碼實際上沒有做任何事情,除非你可以枚舉;或者可能通過ToArrayToList或類似的東西來加載它。

至於自動屬性,不,你不能斷點 - 但你不應該再需要 - 只是斷點設置屬性的代碼。如果你真的想要,請堅持在私人支持字段中並手動實現該屬性。如果沒有,給這個類一個構造函數,並且設置Uri,然後用斷點代替。

1

將斷點添加到您聲明此類的位置。

WebPage wp =new WebPage(); 

因爲Asif如上所述,它不會在聲明中停止。

或之後宣佈類設置的URL變量

wp.Url="blahblahblah.html"; 

編輯:我不知道不工作的自動屬性斷點。 更改

public Uri Url{get;set;} 

private Uri _Url=new Uri(); 
public Url URL{get{return _Url;}set{_Url = value;}} 

你在這裏做的是創建一個私有變量名_url和與財產網址來訪問它

用途是

Url="blahblahblah"; 

與您目前使用的相同

0

你可以喜歡它是described hereUrl屬性設置一個斷點。

設置斷點自動屬性不起作用的標準方法。