Thursday, March 5, 2009

Encoding URL's to access a resource over HTTP in Java

In java the URI class is more flexible to construct much more flexible URL's and also supports encoding.

URI uri = new URI("http","localhost","/temp/XYZ.xsd",null);
URL url = uri.toURL();
URLConnection urlConnection = url.openConnection();
urlConnection.setDoOutput(true);    

Quote from the sun documentation "The recommended way to manage the encoding and decoding of URLs is to use URI, and to convert between these two classes using toURI() and URI.toURL()."  I did see this only on the 1.5 version not on 1.4 version of the doc. 

No comments:

Post a Comment