C# Dns.GetHostEntry Method(String) 实际上是做什么的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10910622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:43:35  来源:igfitidea点击:

What does Dns.GetHostEntry Method(String) actually do?

c#.netdnshostsystem.net

提问by Greg Tarr

I can't find any proper description in the documentation for what this actually does.

我在文档中找不到任何关于它实际作用的正确描述。

Does it check for the existence of A records or CNAME records or both?

它是否检查 A 记录或 CNAME 记录或两者是否存在?

My understanding is that in .NET 4, this throws a SocketException if the host does not exist, and this is confirmed by my testing.

我的理解是,在 .NET 4 中,如果主机不存在,则会抛出 SocketException,我的测试证实了这一点。

采纳答案by Despertar

This is the list of addresses returned by

这是返回的地址列表

var ips = System.Net.Dns.GetHostEntry("microsoft.com").AddressList;
foreach (var ip in ips)
    Console.WriteLine(ip);

// output
64.4.11.37
65.55.58.201

And these are the A records pulled from network-tools.com, DNS query.

这些是从 network-tools.com 中提取的 A 记录,DNS 查询。

Answer records
microsoft.com       A   64.4.11.37  
microsoft.com       A   65.55.58.201

So I'd say it does pull A records.

所以我会说它确实拉了A记录。

回答by Martin Liversage

Dns.GetHostEntryis built on top of the Windows API and does not use the DNS protocol directly. If IPv6 is enabled it will call getaddrinfo. Otherwise it will call gethostbyaddr. These functions may use the local %SystemRoot%\System32\drivers\etc\hostsfile, DNS or even NETBIOS to resolve a host name to an IP address. Resolving a host name to an IP address using DNS will use CNAME records to find the A record.

Dns.GetHostEntry建立在 Windows API 之上,不直接使用 DNS 协议。如果启用了 IPv6,它将调用getaddrinfo. 否则它会调用gethostbyaddr. 这些函数可以使用本地%SystemRoot%\System32\drivers\etc\hosts文件、DNS 甚至 NETBIOS 将主机名解析为 IP 地址。使用 DNS 将主机名解析为 IP 地址将使用 CNAME 记录来查找 A 记录。

You can test this by resolving www.google.comthat at least right now has a CNAME record that points to www.l.google.com. Using Dns.GetHostEntrywill return the IP addresses from the A records for www.l.google.com.

您可以通过解析www.google.com至少现在有一个指向www.l.google.com. 使用Dns.GetHostEntry将返回 A 记录中的 IP 地址www.l.google.com