如何在Java中检查域是否存在?
假设我的电子邮件地址是 xyz@yahoo.com
,我想检查 yahoo.com
是否是是否有效域。
Suppose my email address is xyz@yahoo.com
and I want to check if yahoo.com
is a valid domain or not.
有谁能告诉我我可以使用哪种Java API?
Can anyone tell me which Java API I can use for this?
InetAddress
有 getByName()
方法确定主机的IP地址,鉴于主持人的名字。
InetAddress
has getByName()
method to determine the IP address of a host, given the host's name.
如果找不到主机的IP地址(如果给定的主机名无效), 将抛出UnknownHostException
。
If no IP address for the host could be found ( in case the given host name is not valid) , UnknownHostException
will be thrown.
因此,您只需在调用 InetAddress.getByName()时尝试捕获
。如果捕获到 UnknownHostException
UnknownHostException
,则表示您的输入主机名无效。
So , you just try to catch an UnknownHostException
when calling InetAddress.getByName()
. If UnknownHostException
is caught , that means your input host name is invalid.