I would like to get the contents of the url. Like pythons:
html_content = urllib.urlopen("http://www.test.com/test.html").read()
In the examples ( java2s.com ), you often see the following code:
URL url = new URL("http://www.test.com/test.html"); String foo = (String) url.getContent();
The getContent description is as follows:
Gets the contents of this URL. This method is a shorthand for: openConnection().getContent() Returns: the contents of this URL.
In my opinion, this should work just fine. Buuut, obviously, this code does not work because it causes 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 is the purpose of this function that does not do what it does? And why is there no hint of quirks in the documentation? And why did I see this in a few examples?
Or I'm wrong?
The proposed solution ( https://stackoverflow.com/a/123908/ ) should use url.openStream () and then read Stream.
source share