通过 DNS.GetHostEntry 将 IP 地址转换为主机名 [英] Converting IP address to host name via DNS.GetHostEntry

查看:41
本文介绍了通过 DNS.GetHostEntry 将 IP 地址转换为主机名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过传入 ip 地址来获取主机名.我使用以下代码.

I am trying to get the hostname by passing in the ip address. I use the following code.

System.Net.Dns.GetHostEntry("192.168.x.x").HostName

对于某些主机,上述代码正确返回主机名,但对于其他少数主机,它会引发异常找不到此类主机".

For some host the above code is returning correctly the hostname, but for few other host it throws an exception 'No Such host found'.

谁能告诉我为什么某些主机会发生这种情况?

Could any one tell me why is this happening for some hosts?

我在一个 asp.net mvc 应用程序中使用了上面的代码.

I used the above code in an asp.net mvc application.

推荐答案

并非所有 IP 都使用反向 DNS 条目正确设置.这些 IP 通常是懒惰 ISP 的最终消费者,他们不为其客户提供 PTR 记录.如果没有反向进场,您可以打赌也没有正向进场.因此,这些主机根本没有主机名,因此是个例外.您需要为这些主机捕获此异常,并使用其他内容(例如它们的 IP)作为标识符.

Not all IP's are setup properly with a reverse DNS entry. These IP's are typically end consumers on lazy ISP's who don't provide PTR records for their clients. If there's no reverse entry, you can bet there's no forward entry either. As such, these hosts have no hostname at all, hence the exception. You'll need to catch this exception for these hosts and use something else such as their IP as an identifier.

这篇关于通过 DNS.GetHostEntry 将 IP 地址转换为主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆