Java – why should I use URL Openstream instead of URL getContent?

I want to search the content of a website

html_content = urllib.urlopen("http://www.test.com/test.html").read()

In the example (java2s. Com), you often see the following code:

URL url = new URL("http://www.test.com/test.html");
String foo = (String) url.getContent();

Getcontent is described 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, it should be perfect Buuut obviously this code doesn't 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 asked myself: what is the purpose of this function? Does it not do what it seems to do? Why doesn't it imply its quirks in the document? Why do I see it in a few examples?

Or am I wrong?

The recommended solution (stack overflow) is to use URL Openstream() and read the stream

Solution

As you said, the document says URL Getcontent () is openconnection () Getcontent (), so we need to check the document for urlconnection getContent().

We can see that this returns an object whose type is determined by the content - type header field of the response This type determines the contenthandler. ID that will be used Therefore, contenthandler converts the data based on its MIME type to the appropriate Java object class

In other words, the type of object you get will depend on what you provide For example, if the MIME type is image / PNG, it is meaningless to return string

That's why you're linking to java2s In the sample code of COM, they will check the class of the returned object:

try {
  URL u = new URL("http://www.java2s.com");
  Object o = u.getContent();
  System.out.println("I got a " + o.getClass().getName());
} catch (Exception ex) {
  System.err.println(ex);
}

So you can say string foo = (string) URL getContent(); If you know, your contenthandler will return a string

sun. net. Default content handlers are defined in the www.content package, but as you can see, they are returning streams for you

You can create your own contenthandler, which does return a string, but it may be easier to read the stream as you suggest

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>