IPHostEntry hostinfo = Dns.GetHostEntry("www.google.com");
            IPAddress[] ip = hostinfo.AddressList;
            String[] names = hostinfo.Aliases;
使用上面的代码获得的names数组长度为0,而把Dns.GetHostEntry改为 Dns.Resolve后得到的结果却是大于0的,在MSDN中说Resolve已经过时应用GetHostEntry来代替,那么这两个功能应该是相同的吧,为什么会出现上面的情况呢?

解决方案 »

  1.   

    还真是,通过Reflector查看了一下public static IPHostEntry GetHostEntry(string hostNameOrAddress)
    {
        IPAddress address;
        IPHostEntry hostByName;
        if (Logging.On)
        {
            Logging.Enter(Logging.Sockets, "DNS", "GetHostEntry", hostNameOrAddress);
        }
        s_DnsPermission.Demand();
        if (hostNameOrAddress == null)
        {
            throw new ArgumentNullException("hostNameOrAddress");
        }
        if (TryParseAsIP(hostNameOrAddress, out address))
        {
            if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any))
            {
                throw new ArgumentException(SR.GetString("net_invalid_ip_addr"), "hostNameOrAddress");
            }
            hostByName = InternalGetHostByAddress(address, true, false);
        }
        else
        {
            hostByName = InternalGetHostByName(hostNameOrAddress, true);
        }
        if (Logging.On)
        {
            Logging.Exit(Logging.Sockets, "DNS", "GetHostEntry", hostByName);
        }
        return hostByName;

    [Obsolete("Resolve is obsoleted for this type, please use GetHostEntry instead. http://go.microsoft.com/fwlink/?linkid=14202")]
    public static IPHostEntry Resolve(string hostName)
    {
        IPAddress address;
        IPHostEntry hostByName;
        if (Logging.On)
        {
            Logging.Enter(Logging.Sockets, "DNS", "Resolve", hostName);
        }
        s_DnsPermission.Demand();
        if (hostName == null)
        {
            throw new ArgumentNullException("hostName");
        }
        if (TryParseAsIP(hostName, out address) && ((address.AddressFamily != AddressFamily.InterNetworkV6) || Socket.LegacySupportsIPv6))
        {
            hostByName = InternalGetHostByAddress(address, false, false);
        }
        else
        {
            hostByName = InternalGetHostByName(hostName, false);
        }
        if (Logging.On)
        {
            Logging.Exit(Logging.Sockets, "DNS", "Resolve", hostByName);
        }
        return hostByName;
      

  2.   

    看上去问题就在于InternalGetHostByName(hostName, false);中第二个参数的false与true上,但是具体是怎么执行的呢?我没有找到InternalGetHostByName的说明,另外在获取别名的时候是用Dns.GetHostEntry还是用 Dns.Resolve呢?
      

  3.   

    的确是这样的。
    关键是这个内部方法里面,没有渠道aliases:private static IPHostEntry NativeToHostEntry(IntPtr nativePointer)
    {
        hostent hostent = (hostent) Marshal.PtrToStructure(nativePointer, typeof(hostent));
        IPHostEntry entry = new IPHostEntry();
        if (hostent.h_name != IntPtr.Zero)
        {
            entry.HostName = Marshal.PtrToStringAnsi(hostent.h_name);
        }
        ArrayList list = new ArrayList();
        IntPtr ptr = hostent.h_addr_list;
        nativePointer = Marshal.ReadIntPtr(ptr);
        while (nativePointer != IntPtr.Zero)
        {
            int newAddress = Marshal.ReadInt32(nativePointer);
            list.Add(new IPAddress(newAddress));
            ptr = IntPtrHelper.Add(ptr, IntPtr.Size);
            nativePointer = Marshal.ReadIntPtr(ptr);
        }
        entry.AddressList = new IPAddress[list.Count];
        list.CopyTo(entry.AddressList, 0);
        list.Clear();
        ptr = hostent.h_aliases;
        nativePointer = Marshal.ReadIntPtr(ptr);
        while (nativePointer != IntPtr.Zero)
        {
            string str = Marshal.PtrToStringAnsi(nativePointer);
            list.Add(str);
            ptr = IntPtrHelper.Add(ptr, IntPtr.Size);
            nativePointer = Marshal.ReadIntPtr(ptr);
        }
        entry.Aliases = new string[list.Count];
        list.CopyTo(entry.Aliases, 0);
        return entry;
      

  4.   

    由于内部代码只能分析,不能跟踪,所以这个问题只好问微软啦,说不定是个bug呢。
      

  5.   

    非也非也,Dns.GetHostEntry也是可以得到"别名",不过需要点小技巧~