我想检查是否存在某些电子邮件域。
当Dns.GetHostEntry(domain)
抛出异常时,我确定该域不存在。
- 我可以说如果
Dns.GetHostEntry(domain)
成功则域确实存在,或者即使Dns.GetHostEntry(domain)
成功并不意味着(仍然)该域存在? - 无法连接时我可以说同样的话
s.Connect
吗?我的意思是如果连接抛出异常,我可以说这样的域不存在吗?
如果 (1) 为真,那么为了检查域是否存在 (1) 就足够了,对吧?
public static bool Lookup(string domain)
{
if (domain == null) throw new ArgumentNullException("domain");
try {
IPHostEntry ipHost = Dns.GetHostEntry(domain);
var endPoint = new IPEndPoint(ipHost.AddressList[0], _dnsPort);
return Transfer(endPoint);
}
catch (SocketException ex)
{
++attempts;
}
return false;
}
public static bool Transfer(IPEndPoint endPoint)
{
int attempts = 0;
while(attempts <= _attempts)
{
try
{
var s = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
s.Connect(endPoint);
}
catch (SocketException ex)
{
++attempts;
}
finally
{
s.Close();
}
}
}