【发布时间】:2017-11-22 02:54:13
【问题描述】:
我想要发生的事情: 将域作为字符串传递给该方法,如果域解析,则让它返回 true。如果没有,则为假。基本目标是查看域是否存在。
会发生什么: 大多数有效的域字符串都返回 true。然而,尽管使用 nslookup 解析,但有些返回 false。
我不明白为什么某些域在使用命令提示符 nslookup 和 nslookup 站点时无法解析。 (我用过https://centralops.net/、http://www.kloth.net/services/nslookup.php和http://network-tools.com/nslook/)
方法(C#):
//no blank domains
public bool CheckDomain(string sDomain)
{
if (string.IsNullOrWhiteSpace(sDomain)) return false;
for (int i = 1; i <= mQueryRetry; i++)
{
try
{
System.Net.IPAddress dnsCli = System.Net.IPAddress.Parse("8.8.8.8")
DnsClient myClient = new DnsClient(dnsCli);
DnsMessage dnsMessage = myClient.Resolve(ARSoft.Tools.Net.DomainName.Parse(sDomain), RecordType.A);
if ((dnsMessage == null) || ((dnsMessage.ReturnCode != ReturnCode.NoError) && (dnsMessage.ReturnCode != ReturnCode.NxDomain)))
{
throw new Exception("DNS request failed");
}
else
{
foreach (DnsRecordBase dnsRecord in dnsMessage.AnswerRecords)
{
ARecord aRecord = dnsRecord as ARecord;
if (aRecord != null)
{
return true;
}
}
}
}
catch (Exception ex)
{
// Back off and try again after 1 seconds
if (i != mQueryRetry)
{
System.Threading.Thread.Sleep(1000);
}
else
{
System.Diagnostics.Trace.WriteLine(string.Format("Domain: {0} error: {1}", sDomain, ex.Message));
}
}
}
System.Diagnostics.Trace.Flush();
return false;
}
如果您打算对其进行测试,我建议将 dnsCli IPAddress 替换为 one of these. 我已将 google DNS 服务器的 IP 留在那里作为示例,但我在生产代码中使用了我公司的 DNS 服务器.我发现更改 DNS 服务器不会影响该方法的行为。
我正在为 DnsClient、DnsMessage、DomainName、DnsRecordBase 和 ARecord 类/对象使用最新版本的 Arsoft.Tools.Net (2.2.8)。我们在旧版本 (1.8.1) 上遇到了同样的问题。
一些无法解析的域是:
appraisallinks-amc.com
resurgenstech.com
orpheusvr.com
trovvit.com
其他信息:我尝试了一些可笑的长查询超时限制,超过 5 分钟,但它们没有任何区别。因此,我确信这不是超时问题。
【问题讨论】:
-
您可以尝试使用不同的库,即 DnsClient (nuget),请参阅 dnsclient.michaco.net。有问题的域名似乎可以正常工作
-
Plus One,一个带代码的 DNS 问题。
标签: c# dns cross-domain nslookup arsoft.tools.net