HttpURLConnection Fails to Connect When First DNS-Resolved Address Is Unreachable

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      openjdk 25.0.1 2025-10-21
      OpenJDK Runtime Environment (build 25.0.1)
      OpenJDK 64-Bit Server VM (build 25.0.1, mixed mode, sharing)

      A DESCRIPTION OF THE PROBLEM :
      When trying to connect to a hostname, Java tries to connect to the first address returned by the DNS resolver, disregarding all other ones. This causes many hosts to become unreachable in some scenarios, eg. single hosts which are down in a load balanced CDN or the IPv4 address is unreachable while IPv6 remains reachable for any reason.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Try to connect to a host, for example through a web request.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The connection to the client success, even under non-standard network conditions.
      ACTUAL -
      The connection fails under non-standard network connection because further addresses are not being tried.

      ---------- BEGIN SOURCE ----------
      import java.net.HttpURLConnection;
      import java.net.URI;
      import java.net.URL;

      class Main {
          public static void main(String args[]) {
              try {
                  System.out.println("Url: " + args[0]);
                  URL uri = new URI(args[0]).toURL();

                  HttpURLConnection conn = (HttpURLConnection)uri.openConnection();
                  int responseCode = conn.getResponseCode();

                  System.out.println("Got response: " + responseCode);
              } catch(Exception e) {
                  System.out.println(e);
              }
          }
      }
      ---------- END SOURCE ----------

            Assignee:
            Daniel Fuchs
            Reporter:
            Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: