2016-08-11 5 views
0

Ich machte ein URL-Checker-Programm, das URLs aus einer Liste überprüft und gültige Sublinks rekursiv zurückgibt. Es überprüft alle Unterlinks. Ich habe mehr als 100 Hauptseite so und ich würde diese Seiten mit allen Subs überprüfen. Es dauert durchschnittlich 3 Minuten für einen Standort. besonders der erste Html-Code (bei der Beispielzeichenfolge "HtmlCode = client.DownloadString (Link);") braucht mal die anderen schneller als zuerst. Ich würde mein Programm wiederholt ausführen und es steuern Links und senden mir eine Warnung für defekte Links. Wie könnte ich mein Programm schneller machen?Wie kann ich mein URL-Checker-Programm schneller machen?

public Form1() 
    { 
     InitializeComponent(); 

     List<KeyValuePair<string, bool>> UrlS = Link_Bul_v2("http://www.asdfhg.com", new List<string>(), new List<KeyValuePair<string, bool>>()); 
     List<KeyValuePair<string, bool>> UrlS2 = Link_Bul_v2("http://www.huuene.com", new List<string>(), new List<KeyValuePair<string, bool>>()); 

    } 



    string UrlSadelesti(string Link) 
    { 
     return Link.Replace("https://", "").Replace("http://", "").Replace("www.", ""); 
    } 

    WebClient client = new WebClient(); 
    private List<KeyValuePair<string, bool>> Link_Bul_v2(string Link, List<string> HList, List<KeyValuePair<string, bool>> CList) 
    { 


     try 
     { 

      string HtmlCode = client.DownloadString(Link); 

      //if(HtmlCode. 

      CList.Add(new KeyValuePair<string, bool>(Link, true)); 
      var Link_Short = UrlSadelesti(Link).Split('/')[0]; 
      //HList.Add(Link_Short); 

      string DLink; 
      int IndexH = 0; 
      while (true) 
      { 
       IndexH = HtmlCode.IndexOf("href", IndexH); 
       if (IndexH == -1) { break; } 

       if (HtmlCode[IndexH - 1] == '.') 
       { 
        IndexH = IndexH + 1; 

       } 
       else 
       { 

        var IndexD1 = HtmlCode.IndexOf('"', IndexH); 

        var IndexD2 = HtmlCode.IndexOf('"', IndexD1 + 1); 

        var length = IndexD2 - (IndexD1 + 1); 

        DLink = HtmlCode.Substring(IndexD1 + 1, length); 

        IndexH = IndexD2; 


        if ((DLink.Contains(".css") == false)) 
        { 
         if (DLink.Contains("http://") || DLink.Contains("https://")) 
         { 
          if (DLink.Contains(Link_Short) == false) 
           continue; 

         } 
         if (DLink.Contains("#")) 
         { 
          continue; 
         } 
         DLink = UrlSadelesti(DLink).Replace(Link_Short, ""); 

         if (DLink.Length == 1) 
          continue; 

         if (HList.Count(p => p.Contains(DLink)) == 0) 
         { 
          if (DLink.Contains("http://") || DLink.Contains("https://")) 
          { 
           HList.Add(Link_Short + "/" + DLink); 
          } 
          else 
          { 
           HList.Add("http://" + Link_Short + "/" + DLink); 
          } 
         } 
        } 
       } 
      } 
     } 
     catch 
     { 
      CList.Add(new KeyValuePair<string, bool>(Link, false)); 
      if (!HList.Contains(Link)) 
       HList.Add(Link); 

      if (UrlSadelesti(Link).Split('/').Count() > 1) 
      { 
       Link_Bul_v2("http://" + UrlSadelesti(Link).Split('/')[0], HList, CList); 
      } 
     } 
     foreach (string s in HList) 
     { 
      if (CList.Count(p => p.Key.Contains(s)) == 0) 
      { 
       Link_Bul_v2(s, HList, CList); 
      } 
     } 
     return CList; 

    } 
+0

keine Antwort, aber Xenu (http://xenus-link-sleuth.en.softonic.com/) sieht genau so aus, wie du versuchst zu tun –

+0

danke, ich weiß, es gibt Programme, die das machen, aber ich versuche, meine C# zu entwickeln. – witchqueen

Antwort

0

Eine Möglichkeit wäre, jede Verbindung parallel zu überprüfen:
How to: Write a Simple Parallel.ForEach Loop

using System.Threading; 
using System.Threading.Tasks; 

... 

Parallel.ForEach(links, (currentLink) => 
          { 
           // Check "currentLink" 
          }); 

// All links are checked at this point 
+0

Ich habe das versucht, aber ich hatte "WebClient-Instanzen unterstützen nicht mehrere ausstehende" Fehler. – witchqueen

+0

Stellen Sie sicher, dass Sie in jedem der parallelen Threads eine neue 'WebClient'-Instanz erstellen, oder hilft das nicht? – user5226582

Verwandte Themen