I would like to retrieve the content of a url. Similar to pythons:
html_content = urllib.urlopen("http://www.test.com/test.html").read()
In examples( java2s.com ) you see very often the following code:
URL url = new URL("http://www.test.com/test.html");
String foo = (String) url.getContent();
The Description of getContent is the following:
Gets the contents of this URL. This method is a shorthand for: openConnection().getContent()
Returns: the contents of this URL.
In my opinion that should work perfectly fine. Buuut obviously this code doesnt work, because it raises an error:
Exception in thread "main" java.lang.ClassCastException: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream cannot be cast to java.lang.String
Obviously it returns an inputStream.
So i ask myself: what's the purpose of this function which isn't doing what it is seems to do? And why is no hint for quirks it in the documentation? And why did i saw it in several examples?
Or am i getting this wrong?
The suggested solution (stackoverflow) is to use url.openStream() and then read the Stream.