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VwAAxKNfAQAQj34FAEA8+hUAAPHoVwAAxKNfAQAQj34FAEA8+hUAAPHoVwAAxKNfAQAQj34FAEA8+hUAAPHoVwAAxKNfAQAQj34FAEA8+hUAAPHoVwAAxKNfAQAQj34FAEA8+hUAAPHoVwAAxKNfAQAQj34FAEA8+hUAAPHoVwAAxKNfAQAQj34FAEA8+hUAAPHoVwAAxKNfAQAQj34FAEA8+hUAAPHoVwAAxKNfAQAQj34FAEA8+hUAAPHoVwAAxIuhfq31Xb3TjwYAAALU+q7GSr96L16pOt9OCCGEJEa8F6/ERL8CANBr0a8AAIhHvwIAIB79CgCAePQrAADi0a8AAIhHvwIAIB79CgCAePQrAADi0a8AAIhHvwIAIB79CgCAeGH6tdTTQgghhJDoc88RL/1KCCGECA79SgghhIgP/UoIIYSID/1KCCGEiA/9SgghhIhPT7/ec8RLCCGEEFGRAADArfD/Vp17f0aHU8QAAAAASUVORK5CYII=" alt="" />

利用ARSoft.Tools.Net实现一个客户端DNS解析服务,通过这个工具,可看到当前客户端对某个域名的解析地址是否正确。

ARSoft.Tools.Net是一个非常强大的开源DNS控件库,包含.Net SPF validation, SenderID validation以及DNS Client、DNS Server接口。

使用该接口可轻松实现DNS客户请求端及服务器解析端。项目地址:http://arsofttoolsnet.codeplex.com/或参考:http://i.isclab.org/?p=327

工具的主要代码:

         private void btnDNS_Click(object sender, EventArgs e)
{
int QUERY_TIMEOUT = ;
string domain = txtDomain.Text;
string dnsAddr = txtDNSAddr.Text;
txtResult.Clear();
btnDNS.Enabled = false;
btnDNS.Text = "正在解析...";
new Thread(() =>
{
if (string.IsNullOrWhiteSpace(domain))
{
MessageBox.Show("待解析的域名不可为空");
return;
}
if (string.IsNullOrWhiteSpace(dnsAddr))
{
MessageBox.Show("DNS地址不可为空");
return;
}
//初始化DnsClient,第一个参数为DNS服务器的IP,第二个参数为超时时间
//DnsClient dnsClient = new DnsClient(IPAddress.Parse("8.8.8.8"), QUERY_TIMEOUT);
DnsClient dnsClient = new DnsClient(IPAddress.Parse(dnsAddr), QUERY_TIMEOUT);
//解析域名。将域名请求发送至DNS服务器解析,第一个参数为需要解析的域名,第二个参数为解析类型, RecordType.A为IPV4类型
//DnsMessage dnsMessage = dnsClient.Resolve("sso.zt-express.com", RecordType.A);
DnsMessage dnsMessage = dnsClient.Resolve(domain, RecordType.A);
//若返回结果为空,或者存在错误,则该请求失败。
if (dnsMessage == null || (dnsMessage.ReturnCode != ReturnCode.NoError && dnsMessage.ReturnCode != ReturnCode.NxDomain))
{
// return null;
//Console.WriteLine("没有解析成功");
this.Invoke(new Action(() =>
{
txtResult.Text = "没有解析成功";
btnDNS.Enabled = true;
btnDNS.Text = "解析"; }));
}
else
{
//循环遍历返回结果,将返回的IPV4记录添加到结果集List中。
foreach (DnsRecordBase dnsRecord in dnsMessage.AnswerRecords)
{
ARecord aRecord = dnsRecord as ARecord;
if (aRecord != null)
{
//resultIpList.Add(aRecord.Address.ToString());
//Console.WriteLine(aRecord.Address.ToString());
this.Invoke(new Action(() =>
{
txtResult.AppendText(aRecord.Address.ToString() + "\r\n");
}));
}
else
{
continue;
}
}
this.Invoke(new Action(() =>
{
btnDNS.Enabled = true;
btnDNS.Text = "解析";
})); } }).Start(); }

工具下载地址:http://files.cnblogs.com/files/hnsongbiao/Release.rar

上一篇:javascript高级技巧篇(作用域安全、防篡改、惰性载入、节流、自定义事件,拖放)


下一篇:Linux编程获取本地IP