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

查看:44
本文介绍了如何配置主机名解析以在 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.DatagramSocketjava.net.Socket,并解析响应?

解决方案

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

如果您使用的是 Java 8 或更早版本,您可以:

您可以按照 本网站.

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天全站免登陆