从字符串在C#中提取基地网址是什么?字符串、网址、基地

2023-09-03 17:06:52 作者:ㄟㄏ

我目前工作的一个项目使用.NET 1.1框架,我停留在这一点上。我有一个像 http://www.example.com/mypage/default.aspx ,或者一个字符串可能是 http://www.example.edu/mypage/default.aspx 或的 http://www.example.eu/mypage/default.aspx 。我怎样才能提取基本URL从这样的字符串。

I am currently working on a project with .NET 1.1 framework and I am stuck at this point. I have a string like "http://www.example.com/mypage/default.aspx" or it might be "http://www.example.edu/mypage/default.aspx" or "http://www.example.eu/mypage/default.aspx". How can I extract the base URl from this kind of a string.

感谢

推荐答案

您可以使用的 URI 类来获取主机的名称。

You can use URI class to get the host name.

var uri = new Uri("http://www.example.com/mypage/default.aspx");    
var host = uri.Host;

修改:您可以使用的 uri.Scheme 和的 uri.Port 得到。方案如: (HTTP,FTP)和.Port得到这样(8080)的端口号

Edit You can use uri.Scheme and uri.Port to get the .Scheme e.g. (http, ftp) and .Port to get the port number like (8080)

string host = uri.Host;
string scheme = uri.Scheme;
int port = uri.Port;