Domain Tools

While creating my spamvertized site blocking BHO, Vali IE, I came across a problem whereby I needed to determine the 'registrar' domain of a given URL. For example, given the URL http://www.test.com/somepage.html I needed some code to work out that the actual domain name was test.com. At first glance this seems an easy task, but when you take two level TLDs into account (such as .co.uk or .uk.com) the problem becomes more difficult. I therefore created a function which will load in a list of known two level domains and check a URI against them. If it fails to find the TLD in the list then it will assume it is a top level TLD (such as .com).

I realised this might be useful for other people, so I've bundled the code into a .NET dll for you to include in your project. I've generically named it 'Domain Tools', in case I add anything else later, but at the moment the functionality is restricted to the method described above.

Update: Thank you to James Kirk who spotted a problem with using the DLL through ASP.NET not being able to find the domain list. I've now added a shared property allowing you to explicitly set the path to the DLL in Application_Start.

 Download Domain Tools v1.1 (GPG Sig)

Brief Documentation:

Functions:

GetDomainFromUrl:

This function accepts a URI and determines the 'registrar' domain for it. For example, the domain http://www.microsoft.com/page.html would return microsoft.com. The function requires a text file called two-level-tlds.txt containing a list of all the two level TLDs you want to check for (e.g. .co.uk). An example is included in the archive. This file either needs to reside in the same folder as the DLL, or you will need to set the TldListPath property of the control before using the function.

    Imports Org.Mvps.Leon
    Module Test
        Sub Main()
    	    DomainTools.TldListPath = "C:\two-level-tlds.txt"
            Dim uriTest As New Uri("http://leon.mvps.org/somepage.html")
            Console.WriteLine(DomainTools.GetDomainFromUrl(uriTest))
	    ' Returns mvps.org
        End Sub
    End Module
    

Home