Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从URL获取页面内容?_Java_Url - Fatal编程技术网

Java 从URL获取页面内容?

Java 从URL获取页面内容?,java,url,Java,Url,我想通过以下代码从URL获取页面内容: public static String getContentResult(URL url) throws IOException{ InputStream in = url.openStream(); StringBuffer sb = new StringBuffer(); byte [] buffer = new byte[256]; while(true){ int byteRead = in.

我想通过以下代码从URL获取页面内容:

public static String getContentResult(URL url) throws IOException{

    InputStream in = url.openStream();
    StringBuffer sb = new StringBuffer();

    byte [] buffer = new byte[256];

    while(true){
        int byteRead = in.read(buffer);
        if(byteRead == -1)
            break;
        for(int i = 0; i < byteRead; i++){
            sb.append((char)buffer[i]);
        }
    }
    return sb.toString();
}
公共静态字符串getContentResult(URL)引发IOException{
InputStream in=url.openStream();
StringBuffer sb=新的StringBuffer();
字节[]缓冲区=新字节[256];
while(true){
int byteRead=in.read(缓冲区);
if(byteRead==-1)
打破
for(int i=0;i
但使用此URL: 我无法获得Asbtract:数据库管理系统将继续管理

你能给我解决这个问题的办法吗?
提前感谢

给定url上没有“数据库管理…”。也许,它是由javascript动态加载的。您需要有更复杂的应用程序才能下载此类内容;)

此URL中不包含您要查找的内容。打开浏览器并查看源代码。而是加载了许多javascript文件。我认为内容是稍后通过AJAX调用获取的。您需要了解内容是如何加载的


Firfox插件Firebug可能有助于进行更详细的分析。

输出get请求的标题:

HTTP/1.1 302 Moved Temporarily
Connection: close
Date: Thu, 18 Nov 2010 15:35:24 GMT
Server: Microsoft-IIS/6.0
location: http://portal.acm.org/citation.cfm?id=152610.152611&coll=DL&dl=GUIDE
Content-Type: text/html; charset=UTF-8
这意味着服务器希望您下载新地址。因此,要么直接从UrlConnection获取标题并跟随该链接,要么使用自动跟随重定向的自动链接。以下代码基于:


您应该使用的url是:

http://portal.acm.org/citation.cfm?id=152610.152611&coll=DL&dl=GUIDE

因为您发布的原始url(如dacwe所述)发送重定向。

可能的dup:@Matt Ball这里的问题是OP需要执行JavaScript才能获得所需的内容,从这个意义上讲,问题是根本不同的。你能告诉我这段代码使用哪个库吗?因为我不能用apache的httpcore运行它!我可以运行你的代码!但结果和我的代码一样吗?你能给我一些建议吗suggestion@tiendv:我刚刚尝试了这段代码,我得到了预期的重定向页面,你想得到什么?
http://portal.acm.org/citation.cfm?id=152610.152611&coll=DL&dl=GUIDE