2012-01-04 91 views
6

你好,我在C#中構建了一個類似於webspider的東西。在我的研究中,我遇到了一個問題,如果鏈接是內部或外部,入站或出站,我需要阻止。所以我需要創建一個函數來爲我完成這項工作。所以我想出了以下功能,但我不確定它是否是最好的algorythm爲了完成任務。所以我想你對這個問題的看法。檢查一個鏈接是內部的還是外部的

我asume與沒有HTTP鏈接://或https://在鏈接前面是內部 ,如果我有一個域http://www.blahblah.com然後像測試鏈路仍然應該是內部儘管有HTTP :/ /在前面,但像http://www.somethingelse.com/?var1=http://www.blahblah.com/test鏈接是外部我正在檢查第一個字母只。

private Boolean checklinkifinternal(String link) 
     { 
      Boolean isinternal = false; 

      if (link.IndexOf("http://") == 0 || link.IndexOf("https://") == 0) 
      { 
       //Then probably external 
       if (link.IndexOf("http://" + UrlName) == 0 || link.IndexOf("https://" + UrlName) == 0 || link.IndexOf("http://www." + UrlName) == 0 || link.IndexOf("https://www." + UrlName) == 0) 
       { 
        isinternal = true; 
       } 
      } 
      else 
      { 
       isinternal = true; 
      } 

      return isinternal; 
     } 
+0

如何確定它是使用協議的外部/內部...? – Shai 2012-01-04 11:57:54

+3

http://127.0.0.1或http://192.168.1.1什麼都是內部,但會通過作爲外部 – Lloyd 2012-01-04 11:57:57

+0

的確沒有想到的勞埃德 – themis 2012-01-04 11:59:15

回答

6
Uri.Compare(new Uri("google.de"), new Uri("Google.de"), UriComponents.Host, UriFormat.SafeUnescaped, StringComparison.CurrentCulture); 

這是我就從我的頭:)

+0

至於內部/外部定義。我認爲你把它稱爲「我目前仍然在同一個網站上,而不是」這個主機有2 ips,導致相同的網站「 – 2012-01-04 12:01:23

+0

thanx我會測試這個 – themis 2012-01-04 12:01:44

+0

Volker曼努埃爾是的,我認爲這是是一個可能的事實。如果有1個域匹配,兩個ips應該是同一個站點。負載均衡就是這樣一個事實的例子嗎? – themis 2012-01-04 12:03:03

1

這取決於頂說。如果您使用的是http URI,那麼即使域名相同,指向https URI的鏈接也會計爲內部鏈接嗎? (反之亦然。)你將不得不決定。

此外,您的算法不考慮本地文件系統(使用file://)。

+0

是的,你是正確的,即使是在文件中://test.zip是不是我應該crowl,但我需要以某種方式處理它 – themis 2012-01-04 12:08:13