DNS resolution does not work on Android

There is a limit on the size of the UDP response for the DNS protocol. It can contain ~ 500 bytes. When the data exceeds the limit, all DNS servers set the flag "truncated" in the response, but some (for example, google 8.8.8.8) do not put any IP addresses, others simply put a clipped list in the answer. Utilities such as nslookup and dig try to ask the DNS server over TCP to get the full answer, but Android does not. Instead, it fails. Example failed code below.

var host = "prdimg.affili.net";
var addressList = Dns.GetHostEntry(host).AddressList;

ModernHttpclient uses IP addresses the same way, so I cannot get files from prdimg.affili.net. To fix this, I applied a workaround. I use GooglePublicDnsClient to resolve DNS, and then change the hostname to resolved ip using UriBuilder.

var builder = new UriBuilder(originalUrl); builder.Host = ip;

But the solution has two drawbacks

  • it does not work for https due to certificate verification.
  • it does not work if the server uses Vhosts

Can anyone suggest a better solution?

+6
source share

All Articles