Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.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_Html_Url_Bufferedreader - Fatal编程技术网

为什么我的Java代码可以获取某些URL(网页)的内容?

为什么我的Java代码可以获取某些URL(网页)的内容?,java,html,url,bufferedreader,Java,Html,Url,Bufferedreader,我尝试使用java代码获取一些URL的内容。代码返回某些URL的内容,例如: "" 对其他一些人来说,它什么也没有回报。例如,这个: "" 当我手动检查url时,我会看到内容,即使我查看了源代码,我也不会注意到页面结构之间的任何特殊差异。但我仍然没有得到这个网址 它是否与权限问题、网页结构或我的java代码有关 这是我的密码: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStrea

我尝试使用java代码获取一些URL的内容。代码返回某些URL的内容,例如: "" 对其他一些人来说,它什么也没有回报。例如,这个: "" 当我手动检查url时,我会看到内容,即使我查看了源代码,我也不会注意到页面结构之间的任何特殊差异。但我仍然没有得到这个网址

它是否与权限问题、网页结构或我的java代码有关

这是我的密码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

public class TestJsoup {
  public static void main(String[] args) {
  System.out.println(getUrlParagraphs("http://www.nytimes.com/2016/07/24/travel/mozart-vienna.html?_r=0"));
}

public static String getUrlParagraphs (String url) {
try {
  URL urlContent = new URL(url);
  BufferedReader in = new BufferedReader(new InputStreamReader(urlContent.openStream()));
  String line;
  StringBuffer html = new StringBuffer();
  while ((line = in.readLine()) != null) {
    html.append(line);
    System.out.println("Test");
  }
  in.close();
  System.out.println(html.toString());
  return html.toString();
} catch (IOException e) {
    e.printStackTrace();
}
return null;
}
}

这是因为第二个重定向,而您不尝试遵循重定向

尝试使用
curl-v
访问它:

$ curl -v 'http://www.nytimes.com/2016/07/24/travel/mozart-vienna.html?_r=0'
* Hostname was NOT found in DNS cache
*   Trying 170.149.161.130...
* Connected to www.nytimes.com (170.149.161.130) port 80 (#0)
> GET /2016/07/24/travel/mozart-vienna.html?_r=0 HTTP/1.1
> User-Agent: curl/7.35.0
> Host: www.nytimes.com
> Accept: */*
> 
< HTTP/1.1 303 See Other
* Server Varnish is not blacklisted
< Server: Varnish
< Location: http://www.nytimes.com/glogin?URI=http%3A%2F%2Fwww.nytimes.com%2F2016%2F07%2F24%2Ftravel%2Fmozart-vienna.html%3F_r%3D1
< Accept-Ranges: bytes
< Date: Thu, 04 Aug 2016 08:45:53 GMT
< Age: 0
< X-API-Version: 5-0
< X-PageType: article
< Connection: close
< X-Frame-Options: DENY
< Set-Cookie: RMID=007f0101714857a300c1000d;Path=/; Domain=.nytimes.com;Expires=Fri, 04 Aug 2017 08:45:53 UTC
< 
* Closing connection 0
$curl-v'http://www.nytimes.com/2016/07/24/travel/mozart-vienna.html?_r=0'
*在DNS缓存中找不到主机名
*正在尝试170.149.161.130。。。
*已连接到www.nytimes.com(170.149.161.130)端口80(#0)
>GET/2016/07/24/travel/mozart vienna.html?\u r=0 HTTP/1.1
>用户代理:curl/7.35.0
>主持人:www.nytimes.com
>接受:*/*
> 
您可以看到没有内容,它是一个3XX返回码,并且有一个
位置:
标题。

您好, 问题出在你的url上,我在我的机器上试过你的代码,它也返回null,但我阅读了oracle文档,发现问题出在主机上,所以如果你更改url(例如,这个帖子链接),它会正常工作。我的代码在这里

package sd.nctr.majid;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

public class Program {

    public static void main(String[] args) {
        System.out.println(getUrlParagraphs("http://stackoverflow.com/questions/4328711/read-url-to-string-in-few-lines-of-java-code"));

    }

    public static String getUrlParagraphs (String url) {
        try {
          URL urlContent = new URL(url);
          BufferedReader in = new BufferedReader(new InputStreamReader(urlContent.openStream()));
          String line;
          StringBuffer html = new StringBuffer();
          while ((line = in.readLine()) != null) {
            html.append(line);
            System.out.println("Test");
          }
          in.close();
          System.out.println(html.toString());
          return html.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
        }
}

谢谢你,安迪!你是对的!这是一个重定向的url,当我想在浏览器中打开重定向的url时,我必须输入用户名和密码,然后才能看到页面。我知道如何在java代码中获得重定向代码,但我不知道如何通过“用户,密码”步骤获得内容。你知道吗?我可以简单地添加我的用户并传递到重定向链接吗?!