c# – GetHostEntry和GetHostByName之间的区别?

前端之家收集整理的这篇文章主要介绍了c# – GetHostEntry和GetHostByName之间的区别?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
MSDN,它提到GetHostByName已过时.替换是GetHostEntry.它们有什么区别?

解决方法

看起来像GetHostEntry做了更多的错误检查,也支持 Network Tracing

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,(object) ipHostEntry);
  return ipHostEntry;
}
原文链接:https://www.f2er.com/csharp/244397.html

猜你在找的C#相关文章