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. jsp 嵌套iframe 从iframe中表单提交并传值到外层

    今天因需求迭代 更改元来代码 遇到了这么个问题 就是想在 iframe中提交后进行整个页面的跳转 并把iframe中的值传到外层jsp 大概就是这个样子 外层 a.jsp <div id=&qu ...

  2. ubuntu sudo

    sudo(substitute user 或者 superuser do),是一种程序, 以允许用户通过安全的方式使用特殊的权限运行程序(通常为系统的超级 用户) 语法 sudo [-bhHpV][- ...

  3. 在Windows下不使用密码远程登陆Linux

    在登陆Linux进行管理的时候我们通常会使用用户名和密码进行登陆,这样一来是比较麻烦,二来是不安全,为了解决这个问题,我们可以使用公私钥 (public keys和private keys)进行认证. ...

  4. FastCgi与PHP-fpm关系[转] 读完本文瞬间明朗了很多

    刚开始对这个问题我也挺纠结的,看了<HTTP权威指南>后,感觉清晰了不少. 首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者. ...

  5. Quartz2D学习笔记(1)

    ********************************** 简介 *************************************** Quartz2D是⼀个二维绘图引擎,同时支持 ...

  6. c#开发Mongo笔记第一篇

    现在开发的这个项目要用mongo数据库开发,发现网上的这方面教程还是比较少的,只能边看官方说明边进行开发,再开发过程中写下笔记,也算上是一个总结吧. 我开发使用的是vs2013了,驱动用的是最新的1. ...

  7. URL参数加密解密

    /// <summary>        /// DES加密字符串        /// </summary>        /// <param name=" ...

  8. aircrack-ng on OSX 从零开始之探测

    继续上一篇内容,在安装好aircrack-ng之后,就要学习如何对目标进行探测了.找了篇教程跟着学习一下吧.其实网上关于使用aircrack-ng的教程还是很多的,我也参考了很多,不过最后还是以官方的 ...

  9. hdu 4452

    今天模拟赛的一个模拟题: 每次看到这种题就感觉很繁琐: 这次静下心来写写,感觉还不错!就是很多错误,浪费了一点时间: 代码: #include<cstdio> #include<cs ...

  10. POJ_3321_APPLE_TREE

    poj上面的一道求子树上苹果的题目,网上看了很多题解,下面我来回忆一下,基本来源于大神的微博,http://blog.csdn.net/zhang20072844,我来做个搬运工.先将树的n条边上节点 ...