Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace default DNS name resolution in Java

Tags:

java

tcp

jvm

dns

I'd like to change the way the JVM resolves domain names in HttpURLConnections.

Instead of going to DNS servers through the default TCP/IP stack I wanted to write my own code to resolve domain names.

Is there a way to replace the default name resolution mechanism?

like image 709
Gabriel Oliveira Avatar asked Aug 15 '26 01:08

Gabriel Oliveira


2 Answers

i have no idea if it would work, but there are system properties

System.setProperty("sun.net.spi.nameservice.nameservers", localhost); System.setProperty("sun.net.spi.nameservice.provider.1", "dns,sun");

perhaps if you have your java application open a port on the dns port (53) and process requests yourself.

Of course this falls down if there is a dns server already on the current machine.

like image 86
MeBigFatGuy Avatar answered Aug 17 '26 15:08

MeBigFatGuy


Construct URLs replacing the hostname with the numeric IP address and then add the Host header manually to the HttpUrlConnection.

http://www.myserver.com/foo.html

Is equivalent to

http://192.168.1.100/foo.html
Host: www.myserver.com

dnsjava gives you more control in resolving DNS names over the standard Java APIs.

like image 33
Uriah Carpenter Avatar answered Aug 17 '26 16:08

Uriah Carpenter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!