What I want to happen: 将域的方法作为字符串传递,并在域解析时返回true . 如果没有,则为假 . 基本目标是查看域是否存在 .
What happens: 大多数有效的域字符串都返回true . 但是,尽管使用nslookup解析,但有些会返回false .
我't understand why certain domains are failing to resolve when they look fine when using command prompt nslookup and nslookup sites. (I'已使用https://centralops.net/,http://www.kloth.net/services/nslookup.php和http://network-tools.com/nslook/)
Method (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;
}
如果您打算测试它,我建议在 生产环境 代码中用one of these. I 've left the IP for the google DNS server in there as an example but I am using my company'的DNS服务器替换 dnsCli
IPAddress . 我've found that changing the DNS server in no way impacts the method'的行为 .
我正在为 DnsClient
, DnsMessage
, DomainName
, DnsRecordBase
和 ARecord
,类/对象使用最新版本的Arsoft.Tools.Net(2.2.8) . 我们在旧版本(1.8.1)中遇到了同样的问题 .
一些未能解决的域名是:
appraisallinks-amc.com
resurgenstech.com
orpheusvr.com
trovvit.com
Additional info: 我已经尝试了一些非常长的查询超时限制,超过5分钟,并且它们没有任何区别 . 因此,我确信这不是暂停问题 .
1 回答
“您可以尝试使用不同的库,DnsClient(nuget),请参阅dnsclient.michaco.net . 有问题的域名似乎工作得很好” - @MichaC
问题实际上是我正在使用的Arsoft.Tools.Net库 . 切换到DnsClient.Net解决了这个问题 .