如何配置主机名解析以在Java中使用自定义DNS服务器? [英] How to configure hostname resolution to use a custom DNS server in Java?

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

问题描述

java.net.InetAddress 解析默认情况下使用本地计算机的默认主机名解析器的主机名:

主机名到IP地址的解析是通过结合使用本地计算机配置信息和网络命名服务(例如域名系统(DNS)和网络信息服务(NIS))来实现的.默认情况下,所使用的特定命名服务是本地计算机配置的.对于任何主机名,将返回其对应的IP地址. [源代码]

如何在不修改本地计算机的默认主机名解析器的情况下配置此行为?

例如,仍然可以配置 java.net.InetAddress ,以便它通过OpenDNS(208.67.222.222,208.67.220.220)或Google Public DNS(2001:4860:4860 :: 8888,2001:4860:4860 :: 8844)解析主机名?

或者是唯一显式创建DNS数据包请求,通过 java.net.Socket ,然后解析响应?

解决方案

Java 9删除了此功能.您将需要使用第三方DNS客户端库.

如果您使用的是Java 8或更早版本,则可以执行以下操作:

您可以设置系统属性 sun.net.spi.nameservice.nameservers ,如java.net.InetAddress resolves hostnames using the local machine's default host-name resolver by default:

Host name-to-IP address resolution is accomplished through the use of a combination of local machine configuration information and network naming services such as the Domain Name System (DNS) and Network Information Service(NIS). The particular naming services(s) being used is by default the local machine configured one. For any host name, its corresponding IP address is returned. [source]

How can we configure this behavior without modifying the local machine's default hostname resolver?

For example, is there anyway to configure java.net.InetAddress such that it resolves host names through OpenDNS (208.67.222.222, 208.67.220.220) or Google Public DNS (2001:4860:4860::8888, 2001:4860:4860::8844)?

Or is the only solution to explicitly create DNS packet requests, send them to the servers through either java.net.DatagramSocket or java.net.Socket, and parse the responses?

解决方案

Java 9 removed this capability. You will need to use a third party DNS client library.

If you are using Java 8 or older you can do:

You can set the system property sun.net.spi.nameservice.nameservers as documented by this site.

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

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