如何使用 VB.NET 或 C# 将 ip 地址转换为 url

发布于 2024-10-04 07:56:25 字数 31 浏览 1 评论 0原文

我想将ip地址转换为url。但我不知道该怎么做。

I want to convert ip address to url. But i can't figure it out how to.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空名 2024-10-11 07:56:25

目前还不完全清楚你想要什么 - 一个例子会很有帮助 - 但像这样简单的事情

string url = "http://" + ipAddress;

很可能就足够了。

编辑:好的,听起来您正在尝试查找 IP 地址的名称。在某些方面,这非常简单:

IPHostEntry entry = Dns.GetHostEntry("72.29.94.50");
Console.WriteLine(entry.HostName);

但是,这打印eggheadcafe.com。它打印出完全不同的内容:

72.29.94.50.static.dimenoc.com

就反向 DNS 查找而言,这是完全正确的(运行“nslookup 72.29.94.50”以查看相同的结果)...但这不是您想要的。

问题是,我相信这个 Eggheadcafe.com 是由虚拟主机提供服务的 - 尽管 Eggheadcafe.com 在该 IP 地址上提供服务,其他网站也是如此(至少可能如此)。当您在浏览器中访问 Eggheadcafe.com 时,它会解析为该 IP 地址,但还会在 HTTP 标头中指定主机名。

It's not entirely clear what you want - an example would have been helpful - but something as simple as:

string url = "http://" + ipAddress;

would quite possibly be enough.

EDIT: Okay, it sounds like you're trying to find the name for an IP address. In some ways that's quite simple:

IPHostEntry entry = Dns.GetHostEntry("72.29.94.50");
Console.WriteLine(entry.HostName);

However, this doesn't print eggheadcafe.com. It prints something entirely different:

72.29.94.50.static.dimenoc.com

That's entirely correct in terms of a reverse DNS lookup (run "nslookup 72.29.94.50" to see the same result)... but it's not what you were looking for.

The problem is that I believe this eggheadcafe.com is served by virtual hosting - although eggheadcafe.com is served on that IP address, so are other websites (at least potentially). When you visit eggheadcafe.com in your browser, it resolves to that IP address but also specifies the host name in an HTTP header.

无边思念无边月 2024-10-11 07:56:25

不确定您想要实现什么,但猜测您需要从 IP 解析主机名。

在这种情况下,您可以使用 Dns.GetHostEntry 方法。

Not sure what you want to achieve but guess you need to resolve a host name from IP.

In this case you can use Dns.GetHostEntry method.

も让我眼熟你 2024-10-11 07:56:25

单个 IP 上可能托管多个网站。但您可以使用以下方式获得默认值,

 IPHostEntry IpEntry = Dns.GetHostByAddress(ip); 
 return iphostentry.HostName.ToString();

以下可能有助于开始:

http:// www.c-sharpcorner.com/UploadFile/uchukamen/IPAddHostConverter12052005041212AM/IPAddHostConverter.aspx

There are possibility of multiple website hosted on single IP. But you can get default using

 IPHostEntry IpEntry = Dns.GetHostByAddress(ip); 
 return iphostentry.HostName.ToString();

Following might helpful to start with:

http://www.c-sharpcorner.com/UploadFile/uchukamen/IPAddHostConverter12052005041212AM/IPAddHostConverter.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文