2016-08-04 62 views
-1

我试图通过使用我的java代码来获取某些url的内容。该代码返回一些网址的内容,例如: “http://www.nytimes.com/video/world/europe/100000004503705/memorials-for-victims-of-istanbul-attack.html” ,并且它对于某些其他网站不会返回任何内容。例如这一个: “http://www.nytimes.com/2016/07/24/travel/mozart-vienna.html?_r=0” 当我手动检查网址时,我看到了内容,即使我查看源代码,我也没有注意到页面结构之间有任何特别的区别。但我仍然没有得到这个网址。为什么我的Java代码可以获取某些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; 
} 
} 

回答

0

这是因为第二个重定向,你不要试图跟随重定向。

尝试用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 

你可以看到有没有内容,这是一个3XX返回代码,并具有Location:头。

+0

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

0

你好, 问题是在您的网址,我想你的代码在我的机器,它也返回null,但我阅读Oracle文档一下,发现问题是主人,所以如果你改变网址(例如这篇文章链接)它会正常工作。我的代码在这里

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; 
     } 
} 
相关问题