Why should i use url.openStream instead of of url.getContent?
2014-10-18 14:09
190 查看
I would like to retrieve the content of a url. Similar to pythons:
In examples( java2s.com ) you see very often the following code:
The Description of getContent is the following:
In my opinion that should work perfectly fine. Buuut obviously this code doesnt work, because it raises an error:
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.
As you said, documentation says that
We can see that this returns an
In other words the type of Object you get will depend on the content served. For example, it wouldn't make sense to return a
This is why in the example code you link to at java2s.com they check the class of the returned Object:
So you can say
There are default content handlers defined in the
You could create your own
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.
As you said, documentation says that
URL.getContent()is a shortcut for
openConnection().getContent()so we need to look at the documentation for
URLConnection.getContent().
We can see that this returns an
Objectthe type of which is determined by the the
content-typeheader field of the response. This type determines the
ContentHandlerthat will be used. So a
ContentHandlerconverts data based on its MIME type to the appropriate class of Java Object.
In other words the type of Object you get will depend on the content served. For example, it wouldn't make sense to return a
Stringif the MIME type was
image/png.
This is why in the example code you link to at java2s.com they 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
ContentHandlerwill return a
String.
There are default content handlers defined in the
sun.net.www.contentpackage but as you can see they are returning streams for you.
You could create your own
ContentHandlerthat does return a
Stringbut it will probably be easier just to read the Stream as you suggest.
URL url = new URL("http://www.so.com");
URLConnection.setContentHandlerFactory(new ContentHandlerFactory() {
@Override
public ContentHandlercreateContentHandler(String mimetype) {
return new ContentHandler() {
@Override
public Object getContent(URLConnection urlc) throws IOException {
InputStream input = urlc.getInputStream();
StringBuffer stringBuffer = new StringBuffer();
byte[] bytes = new byte[1024];
while(input.read() != -1){
input.read(bytes);
stringBuffer.append(new String(bytes));
}
return stringBuffer.toString();
}
};
}
});
String str = (String)url.getContent();
System.out.println(str);
/*
byte[] bytes = new byte[1024];
InputStream input = (InputStream)url.getContent();
StringBuffer stringBuffer = new StringBuffer();
while(input.read() != -1){
input.read(bytes);
stringBuffer.append(new String(bytes));
}
System.out.println(stringBuffer.toString());
*/
相关文章推荐
- Why should I use XML instead of HTML?
- Why we use HTTP POST instead of HTTP GET
- [ES6] When should use Map instead of Object
- Why Use Git Instead of a Legacy Version Control System
- Why does Quora use MySQL as the data store instead of NoSQLs such as Cassandra, MongoDB, or CouchDB?
- Why does MariaDB 10.2 use InnoDB instead of XtraDB?
- sometimes we should use "disable fork" instead of "disable block_name"
- storm杂谈之Why use netty as transport instead of zeromq
- Why does Quora use MySQL as the data store instead of NoSQLs such as Cassandra, MongoDB, CouchDB etc?
- storm杂谈之Why use netty as transport instead of zeromq
- Why use std::type_index instead of std::type_info*
- Why use MTP instead of USB Mass Storage
- [RxJS] Use takeUntil instead of manually unsubscribing from Observables
- Item 4:Use Conditional Attributes Instead of #if
- HttpURLConnection getContentLength();返回时-1或者是0
- Get the data of today only use c language
- HttpURLConnection getContentLength();返回时-1或者是0
- HttpURLConnection getContentLength();返回时-1或者是0
- Effective C# Item 1: Always Use Properties Instead of Accessible Data Members
- Get the content of an Iframe in Javascript – crossbrowser solution for both IE and Firefox