Top level domain from URL in C#

后端 未结 7 1289
误落风尘
误落风尘 2020-11-29 10:11

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

相关标签:
7条回答
  • 2020-11-29 11:03

    The following code uses the Uri class to obtain the host name, and then obtains the second level host (examplecompany.com) from Uri.Host by splitting the host name on periods.

    var uri = new Uri("http://www.poker.winner4ever.examplecompany.com/");
    var splitHostName = uri.Host.Split('.');
    if (splitHostName.Length >= 2)
    {
        var secondLevelHostName = splitHostName[splitHostName.Length - 2] + "." +
                                  splitHostName[splitHostName.Length - 1];
    }
    
    0 讨论(0)
提交回复
热议问题