Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Top level domain from URL in C#

I am using C# and ASP.NET for this.

We receive a lot of "strange" requests on our IIS 6.0 servers and I want to log and catalog these by domain.

Eg. we get some strange requests like these:

  • http://www.poker.winner4ever.example.com/
  • http://www.hotgirls.example.com/
  • http://santaclaus.example.com/
  • http://m.example.com/
  • http://wap.example.com/
  • http://iphone.example.com/

the latter three are kinda obvious, but I would like to sort them all into one as "example.com" IS hosted on our servers. The rest isn't, sorry :-)

So I am looking for some good ideas for how to retrieve example.com from the above. Secondly I would like to match the m., wap., iphone etc into a group, but that's probably just a quick lookup in a list of mobile shortcuts.I could handcode this list for a start.

But is regexp the answer here or is pure string manipulation the easiest way? I was thinking of "splitting" the URL string by "." and the look for item[0] and item[1]...

Any ideas?

like image 983
BerggreenDK Avatar asked Jan 10 '11 02:01

BerggreenDK


People also ask

Is URL a top-level domain?

A top-level domain (TLD) is the suffix or extension tied to a website. Around half of all websites use the top-level domain com, commonly called “dot” com. Other common TLDs include net, org, and edu. In the website address or URL below, com is the top-level domain.

What are the 4 top-level domains?

gTLD – Generic Top-Level Domain. sTLD – Sponsored Top-Level Domain. ccTLD – Country Code Top-Level Domain. Infrastructure Top-Level Domain.


1 Answers

You can use the following nuget Nager.PublicSuffix package. It uses the same data source that browser vendors use.

nuget

PM> Install-Package Nager.PublicSuffix

Example

var domainParser = new DomainParser(new WebTldRuleProvider());

var domainInfo = domainParser.Parse("sub.test.co.uk");
//domainInfo.Domain = "test";
//domainInfo.Hostname = "sub.test.co.uk";
//domainInfo.RegistrableDomain = "test.co.uk";
//domainInfo.SubDomain = "sub";
//domainInfo.TLD = "co.uk";
like image 62
live2 Avatar answered Sep 28 '22 17:09

live2