Extract domain name from URL in C#

前端 未结 4 1680
情深已故
情深已故 2021-01-13 02:01

This question has answer in other languages/platforms but I couldn\'t find a robust solution in C#. Here I\'m looking for the part of URL which we use in

4条回答
  •  不要未来只要你来
    2021-01-13 02:15

    I needed the same, so I wrote a class that you can copy and paste into your solution. It uses a hard coded string array of tld's. http://pastebin.com/raw.php?i=VY3DCNhp

    Console.WriteLine(GetDomain.GetDomainFromUrl("http://www.beta.microsoft.com/path/page.htm"));
    

    outputs microsoft.com

    and

    Console.WriteLine(GetDomain.GetDomainFromUrl("http://www.beta.microsoft.co.uk/path/page.htm"));
    

    outputs microsoft.co.uk

提交回复
热议问题