Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a link is internal or external

Tags:

c#

.net

Hello i am building something like a webspider in C#. In my research i came across a problem were i need to determen if a link is internal or external, inbound or outbound. So i needed to create a function to do the work for me. So i came up with the following function but i am not sure if its the best possible algorythm in order to accomplish that task. So i would like your opinions upon this problem.

I asume that links with no http:// or https:// in front of the link are internal and if i have a domain http://www.blahblah.com then a link like test should still be internal despite the fact that it has http:// in front, but a link like http://www.somethingelse.com/?var1=http://www.blahblah.com/test is external sto i am checking the first letters only.

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;
        }
like image 428
themhz Avatar asked Jan 04 '12 11:01

themhz


1 Answers

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

this is what i would say from the top of my head :)

like image 126
Volker Mauel Avatar answered Oct 21 '22 03:10

Volker Mauel