在MSDN上提到GetHostByName
已经过时,替代方法是GetHostEntry
。它们有什么区别?
看起来GetHostEntry会进行更多的错误检查,并且还支持网络跟踪
反编译的GetHostByName:
public static IPHostEntry GetHostByName(string hostName)
{
if (hostName == null)
throw new ArgumentNullException("hostName");
Dns.s_DnsPermission.Demand();
IPAddress address;
if (IPAddress.TryParse(hostName, out address))
return Dns.GetUnresolveAnswer(address);
else
return Dns.InternalGetHostByName(hostName, false);
}
GetHostEntry反编译:
public static IPHostEntry GetHostEntry(string hostNameOrAddress)
{
if (Logging.On)
Logging.Enter(Logging.Sockets, "DNS", "GetHostEntry", hostNameOrAddress);
Dns.s_DnsPermission.Demand();
if (hostNameOrAddress == null)
throw new ArgumentNullException("hostNameOrAddress");
IPAddress address;
IPHostEntry ipHostEntry;
if (IPAddress.TryParse(hostNameOrAddress, out address))
{
if (((object) address).Equals((object) IPAddress.Any) || ((object) address).Equals((object) IPAddress.IPv6Any))
throw new ArgumentException(SR.GetString("net_invalid_ip_addr"), "hostNameOrAddress");
ipHostEntry = Dns.InternalGetHostByAddress(address, true);
}
else
ipHostEntry = Dns.InternalGetHostByName(hostNameOrAddress, true);
if (Logging.On)
Logging.Exit(Logging.Sockets, "DNS", "GetHostEntry", (object) ipHostEntry);
return ipHostEntry;
}
inet_aton
(相当于IPAddress.Parse
),gethostbyname
(由Dns.GetHostByName
包装)和gethostbyaddr
(由Dns.GetHostByAddress
包装)。Microsoft随后基于这些添加了Dns.GetHostEntry
实用程序函数。Dns.GetHostByName
和Dns.GetHostEntry
之间的哲学差异后,我得出结论,微软决定他们暴露给DNS查找的主要API应该尝试只返回实际的DNS条目。gethostbyname
可以使用IP地址或主机名。如果您提供IP地址,则它明确记录为解析IP地址。但是,它也明确记录为仅支持IPv4地址。因此,鼓励开发人员改用函数getaddrinfo
,它执行更复杂的查找,涉及您要连接的服务以及支持除IPv4之外的地址族。GetHostByName
已过时,但是他们没有将查找与服务数据库绑定,而是决定创建一个函数,返回您要求的实际物理DNS主机条目。如果您提供了包含有效地址的字符串,则不足以使GetHostEntry
失败,因为这是它的全部目的。因此,如果您将主机名传递给GetHostEntry
,它将执行正向DNS查找,如果您将IP地址传递给GetHostEntry
,它将执行反向DNS查找。无论哪种方式,返回的结构都会告诉您DNS条目名称和相关地址,但是如果没有关联条目,则仅返回错误。GetHostEntry
并不适用,因为如果用户键入临时IP地址,则可能无法解析它,即使它是一个IP地址,您也有足够的信息进行连接。在这种情况下,GetHostByName
正是您需要的函数,但Microsoft选择已将其弃用。鉴于弃用,惯用语将是复制@Faisai Mansoor在反编译的GetHostByName
函数中显示的“先尝试解析”的方法:// Microsoft's internal code for GetHostByName:
if (IPAddress.TryParse(hostName, out address))
return Dns.GetUnresolveAnswer(address);
else
return Dns.InternalGetHostByName(hostName, false);
// 最初的回答
if (!IPAddress.TryParse(userInput, out var addressToWhichToConnect))
addressToWhichToConnect = Dns.GetHostEntry(userInput).AddressList.First();
GetHostByName()
更改为GetHostEntry()
。 - AryoGetHostByAddress
也会失败。粗略地说,前端的GetHostEntry
旨在仅返回_实际的DNS条目_,而GetHostByName
旨在以任何方式提供IP地址。因此,您传递给GetHostByName
的任何IP地址都将被解析并返回,但是只有在您可以获得反向DNS响应时,GetHostEntry
才会将该IP返回给您。这使得GetHostEntry
不太适合用于处理来自用户输入的目标地址的通用处理。 - Jonathan Gilbert