web123456

Issues using DnsResolver for sending http requests in java code

There are two ways to use DnsResolver in http requests. One is to use the default DnsResolver, and the other is to customize a DnsResolver.

1. Use the default DnsResolver

DnsResolver dnsResolver = new SystemDefaultDnsResolver();
In this way, when the code is running, the host file on the machine where the code is located will be used to parse the destination machine (domain name) in some other ways.

2. Customize DnsResolver

The code is as follows

  1. private static DnsResolver getDnsResolver() {
  2. InMemoryDnsResolver dnsResolver = new InMemoryDnsResolver();
  3. try {
  4. ("", ("1.1.1.1"));
  5. ("", ("2.2.2.2"));
  6. } catch (Exception e) {
  7. ();
  8. }
  9. return dnsResolver;
  10. }

In this way, the code will be parsed according to the customized DnsResolver when running. For example, in the http request, the destination machine will be parsed into 2.2.2.2.