Wednesday, June 22, 2016

How to convert URL to punycode in Java

To convert a URL to punycode in Java, you can use the following code if you're using Spring framework:

  public static String getPunycodeUrl(String url) {
    UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(url);
    String host = uriComponentsBuilder.build().getHost();
    return uriComponentsBuilder.host(IDN.toASCII(host)).toUriString();
  }

No comments:

Post a Comment