vs Dns.GetHostEntry Resolve 获得Aliases为空的问题

chadguo 2009-03-17 01:47:02
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来代替,那么这两个功能应该是相同的吧,为什么会出现上面的情况呢?
...全文
183 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
shinaterry 2009-08-02
  • 打赏
  • 举报
回复
非也非也,Dns.GetHostEntry也是可以得到"别名",不过需要点小技巧~
chadguo 2009-06-14
  • 打赏
  • 举报
回复
不管它了吧,在以后使用的时候知道这一点,看情况选择就是了,结帐去了,谢谢上面各位了
lovvver 2009-06-12
  • 打赏
  • 举报
回复
由于内部代码只能分析,不能跟踪,所以这个问题只好问微软啦,说不定是个bug呢。
lovvver 2009-06-12
  • 打赏
  • 举报
回复
的确是这样的。
关键是这个内部方法里面,没有渠道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;
}

chadguo 2009-06-12
  • 打赏
  • 举报
回复
看上去问题就在于InternalGetHostByName(hostName, false);中第二个参数的false与true上,但是具体是怎么执行的呢?我没有找到InternalGetHostByName的说明,另外在获取别名的时候是用Dns.GetHostEntry还是用 Dns.Resolve呢?
duping9626 2009-04-27
  • 打赏
  • 举报
回复
还真是,通过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;
}



foveveryoung 2009-04-27
  • 打赏
  • 举报
回复
今天我也遇到了这个问题。等待答案。
chadguo 2009-03-18
  • 打赏
  • 举报
回复
怎么没人回复我啊。。。
等待帮助中。。
chadguo 2009-03-18
  • 打赏
  • 举报
回复
怎么没人回复我啊。。。
等待帮助中。。
chadguo 2009-03-17
  • 打赏
  • 举报
回复
什么意思,能说的清楚点吗

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧