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;
}

相关文章

C#项目进行IIS部署过程中报错及其一般解决方案_c#iis执行语句...
微信扫码登录PC端网站应用的案例(C#)_c# 微信扫码登录
原文地址:http://msdn.microsoft.com/en-us/magazine/cc163...
前言 随着近些年微服务的流行,有越来越多的开发者和团队所采...
最近因为比较忙,好久没有写博客了,这篇主要给大家分享一下...
在多核CPU在今天和不久的将来,计算机将拥有更多的内核,Mic...