XDns.java 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package com.example.xiaoshiweixinback.business.config;
  2. import okhttp3.Dns;
  3. import java.net.InetAddress;
  4. import java.net.UnknownHostException;
  5. import java.util.ArrayList;
  6. import java.util.Arrays;
  7. import java.util.List;
  8. import java.util.concurrent.Callable;
  9. import java.util.concurrent.FutureTask;
  10. import java.util.concurrent.TimeUnit;
  11. public class XDns implements Dns {
  12. private long timeout;
  13. public XDns(long timeout) {
  14. this.timeout = timeout;
  15. }
  16. @Override
  17. public List<InetAddress> lookup(final String hostname) throws UnknownHostException {
  18. if (hostname == null) {
  19. throw new UnknownHostException("hostname == null");
  20. } else {
  21. List<InetAddress> inetAddresses =new ArrayList<>();
  22. Boolean falg =true;
  23. while (falg){
  24. try {
  25. FutureTask<List<InetAddress>> task = new FutureTask<>(
  26. new Callable<List<InetAddress>>() {
  27. @Override
  28. public List<InetAddress> call() throws Exception {
  29. return Arrays.asList(InetAddress.getAllByName(hostname));
  30. }
  31. });
  32. new Thread(task).start();
  33. inetAddresses=task.get(timeout, TimeUnit.MILLISECONDS);
  34. falg =false;
  35. } catch (Exception var4) {
  36. continue;
  37. }
  38. }
  39. return inetAddresses;
  40. }
  41. }
  42. }