选择用于在 Java 中解析主机名的 DNS 服务器 [英] Choose DNS server for resolving hostnames in Java

查看:40
本文介绍了选择用于在 Java 中解析主机名的 DNS 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过使用像这样的 InetAddress 类,在 Java 中将主机名解析为 IP 地址相当容易:

resolving a hostname to an IP address is rather easy in Java by using the InetAddress class like this:

InetAddress address = InetAddress.getByName("www.example.com");

但是这个方法使用的是运行系统使用的DNS服务器.

But this method uses the DNS server which is used by the running system.

有什么办法可以指定解析时使用的DNS服务器吗?

Is there any way to specify the DNS server that should be used for resolving?

推荐答案

如果你使用 Sun Java,你可以使用这个代码:

If you use Sun Java, you can use this code:

//Override system DNS setting with Google free DNS server
System.setProperty("sun.net.spi.nameservice.nameservers", "8.8.8.8");
System.setProperty("sun.net.spi.nameservice.provider.1", "dns,sun");

参见这篇博文:如何使用 Java System 属性设置自定义 DNS 服务器了解更多详情.

See this blog post: How to set a custom DNS server with Java System properties for more details.

这篇关于选择用于在 Java 中解析主机名的 DNS 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆