code: public static String modifyURIPort(String uri, int newPort) {
          try {
              URI uriObj = new URI(uri);
              return uriToString(URLUtils.modifyURIPort(uriObj, newPort));
          }
          catch (URISyntaxException e) {
              try {
                  return (new NetworkInterfaceURI(uri)).modifyURIPort(newPort);
              }
              catch (IllegalArgumentException ne) {
                  throw new IllegalArgumentException(ne.getMessage(), ne);
              }
          }
      }
docs: Helper method for modifying URI port
  @param uri
  @param newPort
  @return