您可以使用URI https://learn.microsoft.com/en-gb/dotnet/api/system.uri类来获取主机名。
var uri = new Uri("http://www.example.com/mypage/default.aspx");
var host = uri.Host;
Edit您可以使用uri.Scheme https://learn.microsoft.com/en-gb/dotnet/api/system.uri.scheme and uri.Port https://learn.microsoft.com/en-gb/dotnet/api/system.uri.port获取 .Scheme 例如(http, ftp) 和 .Port 来获取端口号,如 (8080)
string host = uri.Host;
string scheme = uri.Scheme;
int port = uri.Port;
您可以使用Uri.GetLeftPart https://learn.microsoft.com/en-gb/dotnet/api/system.uri.getleftpart获取基本 URL。
The 获取左部分 https://learn.microsoft.com/en-gb/dotnet/api/system.uri.getleftpart方法返回一个包含最左边的字符串
URI 字符串的一部分,以part 指定的部分结尾。
var uri = new Uri("http://www.example.com/mypage/default.aspx");
var baseUri = uri.GetLeftPart(System.UriPartial.Authority);
The following examples show a URI and the results of calling GetLeftPart with Scheme, Authority, Path, or Query, MSDN https://learn.microsoft.com/en-gb/dotnet/api/system.uri.getleftpart.