The URI throws a URISyntaxException when you select the appropriate constructor:
URI someUri=new URI("http","www.christlicheparteiösterreichs.at","/steiermark",null);
java.net.URISyntaxException: Invalid character in host name at index 28: http: // www .christlicheparteiösterreichs.at / steiermark
You can use IDN to fix:
URI someUri=new URI("http",IDN.toASCII("www.christlicheparteiösterreichs.at"),"/steiermark",null); System.out.println(someUri); System.out.println("host: "+someUri.getHost()));
Conclusion:
http://www.xn--christlicheparteisterreichs-5yc.at/steiermark
host: www.xn--christlicheparteisterreichs-5yc.at
UPDATE regarding chicken egg problem:
You can let the url do the job:
public static URI createSafeURI(final URL someURL) throws URISyntaxException { return new URI(someURL.getProtocol(),someURL.getUserInfo(),IDN.toASCII(someURL.getHost()),someURL.getPort(),someURL.getPath(),someURL.getQuery(),someURL.getRef()); } URI raoul=createSafeURI(new URL("http://www.christlicheparteiösterreichs.at/steiermark/readme.html#important"));
This is just a quick snapshot, it does not check all the problems associated with converting a URL to a URI. Use it as a starting point.
source share