Why should i use url.openStream instead of of url.getContent?
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.
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 Object the type of which is determined by the the content-type header field of the response. This type determines the ContentHandler that will be used. So a ContentHandler converts 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 String if 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 ContentHandler will return a String.
There are default content handlers defined in the sun.net.www.content package but as you can see they are returning streams for you.
You could create your own ContentHandler that does return a String but 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 ContentHandler createContentHandler(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 url.openStream instead of of url.getContent?的更多相关文章
- SharePoint 2010 Url Shortener --SharePoint 2010 短URL生成器
SharePoint 2010 Url Shortener --SharePoint 2010 短URL生成器 项目描写叙述 本项目加入了这种功能.在SP站点中能够生成短URLs. 这些URLs指向列 ...
- js 获取url中的参数 修改url 参数 移除url参数
js 获取url中的参数 修改url 参数 移除url参数 var jsUrlHelper = { getUrlParam : function(url, ref) { var str = " ...
- Django报错:提交表单报错---RuntimeError: You called this URL via POST, but the URL doesn’t end in a slash and you have APPEND_SLASH set.
Django报错:提交表单报错---RuntimeError: You called this URL via POST, but the URL doesn’t end in a slash and ...
- IDEA报错: Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.datasource.url' in value "${spring.datasource.url}"
运行审核流模块: 在ActivitiServiceApplication模块日志报错: Error starting ApplicationContext. To display the auto-c ...
- django ajax报错解决:You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set.
Django版本号:1.11.15 django中ajax请求报错:You called this URL via POST, but the URL doesn't end in a slash a ...
- UrlUtils工具类,Java URL工具类,Java URL链接工具类
UrlUtils工具类,Java URL工具类,Java URL链接工具类 >>>>>>>>>>>>>>>&g ...
- 获取URL的name值 getUrl(url,name) 传入url和key 得到key对应的value
<body> <script type="text/javascript"> var url = "http://192.168.1.82:802 ...
- URL Handle in Swift (一) -- URL 分解
更新时间: 2018-6-6 在程序开发过程之中, 我们总是希望模块化处理某一类相似的事情. 在 ezbuy 开发中, 我接触到了对于 URL 处理的优秀的代码, 学习.改进.记录下来.希望对你有所帮 ...
- 加密解密Url字符串,C#对Url进行处理,传递Url
string _QueryStringKey = "abcdefgh"; //URL传输参数加密Key /// 加密URL传输的字符串 public string E ...
随机推荐
- MyEclipse 搭建webservice (axis1.4)
0 引言 以前都是做javaweb的 最近因工作需要 接触了webservice 关于什么事webservice,与web的区别,soap,跟http的区别,asix1和asix2的区别,为什么不用 ...
- zTree的getChangeCheckedNodes()使用
zTree的getChangeCheckedNodes()方法用于获取输入框勾选状态被改变的节点集合.如果需要获取每次操作后全部被改变勾选状态的节点数据,请在每次勾选操作后,遍历所有被改变勾选状态的节 ...
- 系统重装后phpnow修复
最近在捣鼓wordpress,主题写了一半然后就重装了win8,在新系统里面访问127.0.0.1的时候出现无法访问的情况.主题写了一半,又不想重装wordpress导数据库这些繁琐的过程,于是,尝试 ...
- PHP — php精粹-编写高效的php代码 --- API
1.数据格式 (1)json 示例代码: $jsonData = '[{"title":"The Magic Flute","time":1 ...
- 【原创】Mvc学习笔记(1)
1.新建MVC4项目 在MVC4中有App_Data文件夹,这个文件夹里可以放一些重要的数据,比如说数据库的mdf文件等等,这个文件夹非常安全,因为这个文件夹不允许被别人下载,不允许被浏览器访问. A ...
- capitalize()在Python中含义
Python为string对象提供了转换大小写的方法:upper() 和 lower(). 还不止这些,Python还为我们提供了首字母大写,其余小写的capitalize()方法, 以及所有单词首字 ...
- Spring MVC注解冲突
SpringMVC+MyBatis - 7 Spring自动扫描注解类的冲突问题 http://www.blogjava.net/crazycy/archive/2014/07/12/415738.h ...
- Hibernate 注解 没有加@Column一样会在数据库创建这些字段
Hibernate 注解 没有加@Column一样会在数据库创建这些字段 如下一个注解类: package com.hyy.hibernate.one_to_many.domain; import j ...
- UVA 10716 Evil Straw Warts Live(贪心)
Problem D: Evil Straw Warts Live A palindrome is a string of symbols that is equal to itself when re ...
- 安卓高手之路之java层Binder
很多人一提到Binder就说代理模式,人云亦云的多,能理解精髓的少. 本篇文章就从设计角度分析一下java层BInder的设计目标,以及设计思路,设计缺陷,从而驾驭它. 对于[邦德儿]的理解, 从通信 ...