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?的更多相关文章

  1. SharePoint 2010 Url Shortener --SharePoint 2010 短URL生成器

    SharePoint 2010 Url Shortener --SharePoint 2010 短URL生成器 项目描写叙述 本项目加入了这种功能.在SP站点中能够生成短URLs. 这些URLs指向列 ...

  2. js 获取url中的参数 修改url 参数 移除url参数

    js 获取url中的参数 修改url 参数 移除url参数 var jsUrlHelper = { getUrlParam : function(url, ref) { var str = " ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. UrlUtils工具类,Java URL工具类,Java URL链接工具类

    UrlUtils工具类,Java URL工具类,Java URL链接工具类 >>>>>>>>>>>>>>>&g ...

  7. 获取URL的name值 getUrl(url,name) 传入url和key 得到key对应的value

    <body> <script type="text/javascript"> var url = "http://192.168.1.82:802 ...

  8. URL Handle in Swift (一) -- URL 分解

    更新时间: 2018-6-6 在程序开发过程之中, 我们总是希望模块化处理某一类相似的事情. 在 ezbuy 开发中, 我接触到了对于 URL 处理的优秀的代码, 学习.改进.记录下来.希望对你有所帮 ...

  9. 加密解密Url字符串,C#对Url进行处理,传递Url

    string _QueryStringKey = "abcdefgh"; //URL传输参数加密Key /// 加密URL传输的字符串        public string E ...

随机推荐

  1. [Computer Vision] SIFT特征学习笔记

    SIFT(Scale Invariant Feature Transform),尺度空间不变特征,目前手工设计的最好vision特征. 以下是学习http://blog.csdn.net/zddblo ...

  2. svn-主副分支使用

    主改bug 副加功能, :主合并到副(在副中切换主分支),副调试成功,合并回主(在主切换回副分支) 奇葩的实现了需求 主改bug 副加功能, :主合并到副(在副中切换主分支),副调试成功,合并回主(在 ...

  3. rpc远程调用开发

    RPC即远程过程调用,适用于集群管理,集群节点就是RPCServer,而我们发起远程调用的web服务器就是RPCClient.所以是少数rpcClient(可能一个)对多个RPCServer(集群节点 ...

  4. Lighttpd日志打印格式

    检查lighttpd打印的日志时,有些字段意义模糊,就找了个对照说明,备份一下 原文地址:http://redmine.lighttpd.net/projects/1/wiki/Docs_ModAcc ...

  5. Android 自定义View实现单击和双击事件

    自定义View, 1. 自定义一个Runnable线程TouchEventCountThread ,  用来统计500ms内的点击次数 2. 在MyView中的 onTouchEvent 中调用 上面 ...

  6. Vim粘贴代码时缩进混乱

    Vim粘贴代码时缩进混乱 via 背景 在终端Vim中粘贴代码时,发现插入的代码会有多余的缩进,而且会逐行累加.原因是终端把粘贴的文本存入键盘缓存(Keyboard Buffer)中,Vim则把这些内 ...

  7. SQLSERVER与C#中数据类型的对应关系

    SQLSERVER与C#中数据类型的对应关系 ///<summary> ///数据库中与C#中的数据类型对照 ///</summary> ///<paramname=&q ...

  8. POJ 3264 Balanced Lineup 简单RMQ

    题目:http://poj.org/problem?id=3264 给定一段区间,求其中最大值与最小值的差. #include <stdio.h> #include <algorit ...

  9. Install SharePoint 2013 on Windows Server 2012 without a domain

    Any setup of Team Foundation Server is not complete until you have at least tried t work with ShareP ...

  10. jQuery滚动条插件 – jquery.slimscroll.js

    jquery.slimscroll.js插件是一个支持把内容放在一个盒子里面,固定一个高度,超出的则使用滚动.jquery.slimscroll.js不仅可以定义高度.宽度,还可以定义位置.滚动条大小 ...