2017-02-19 81 views
2

我试图打开我的大学网站来阅读他们的菜单。我已经写了一个版本,可以直接读取菜单链接到菜单链接的菜单,但是我想将它拉回一点,以便从网站上读取菜单,而不是直接链接(如果链接发生更改) 。网站不喜欢Java吗?

这里是我打开的网址: https://nccudining.sodexomyway.com/dining-choices/index.html

每当我打开链接的网站,这是我得到的输出:

302 
<html><head><title>Object moved</title></head><body> 
<h2>Object moved to <a href="http://m-nccudining.sodexomyway.com/dining-choices/index.html">here</a>.</h2> 
</body></html> 

它输出的网址是移动版的网站,但当我尝试使用该网址时,它不输出任何内容。

这是我的代码:

import java.io.*; 
import java.net.*; 

public class test 
{ 
    public static void main(String[] args) 
    { 
     URL url = null; 

     try 
     { 
      url = new URL("https://nccudining.sodexomyway.com/dining-choices/index.html"); 
      HttpURLConnection test = (HttpURLConnection) url.openConnection(); 
      test.setInstanceFollowRedirects(true); 
      test.connect(); 
      System.out.println(test.getResponseCode()); 
     } catch (MalformedURLException e1) 
     { 
      System.out.println("URL cannot be opened."); 
      return; 
     } 

     BufferedReader in = null; 
     try 
     { 
      in = new BufferedReader(new InputStreamReader(url.openStream())); 
     } catch (IOException e) 
     { 
      System.out.println("Error");    
     } 
     String inputLine; 

     try 
     { 
      while ((inputLine = in.readLine()) != null) 
      { 
       System.out.println(inputLine); 
      } 
     } catch (IOException e) 
     { 
      System.out.println("Error"); 
     }  
    } 
} 

我所有的try/catch循环道歉。我不想仅仅从一开始就抛出一个IOException异常,因为我听说这是不好的做法。无论如何,这段代码只是打开URL,建立一个连接,所以我可以确保URL实际存在,并尝试阅读它的HTML。它适用于我尝试过的任何其他网站,包括谷歌。

我的问题是为什么我的代码不能读取网站的正确源代码?我的代码有问题吗(我想在HttpsURLConnection中添加并允许重定向会起作用),还是仅仅是网站,我有什么可以绕开每周菜单的页面来绕过这些?

找到解决方案!感谢@ShayHaned的修复。我添加下列行到HttpURLConnection类所以我得到一个200响应代码,而不是302:

 test = (HttpURLConnection) url.openConnection(); 
     test.setRequestMethod("GET"); 
     test.setRequestProperty("User-Agent", "Mozilla/5.0"); 
     test.setInstanceFollowRedirects(true); 

然后我从URL打开流从HttpURLConnection类获取输入流改变了的InputStream,如图所示:

BufferedReader in = new BufferedReader(new InputStreamReader(test.getInputStream())); 

这给了我正在寻找的HTML。

+0

什么是响应代码?如果它不是301或类似的,问题出现在服务器端:它们不发布重定向,所以Java没有遵循。 – EJP

+0

@EJP这就是他的第一个代码块...... –

+0

它读取网页的HTML。现在,它没有做任何事情,因为我只是想解决这个问题。 – ds777fighter

回答

0

你只是错过了适当的标题为http通信安全和安全地工作。您可以添加几个头,以确保您获得所需的响应

HttpURLConnection test = (HttpURLConnection) url.openConnection(); 
    test.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko"); 
    test.addRequestProperty("Accept" , "text/html,application/xhtml+xml,application/xml,image/png, image/svg+xml,;q=0.9,*/*;q=0.8"); 
    test.addRequestProperty("Accept-Charset" , "ISO-8859-1,utf-8;q=0.7,*;q=0.3"); 
    test.addRequestProperty("Accept-Language" , "en-US,en;q=0.8"); 
    test.addRequestProperty("Connection" , "close"); 
    test.setRequestMethod("GET"); 


    test.setInstanceFollowRedirects(true); 
    test.connect(); 

    // Nopes DONT TRY THIS 
    //in = new BufferedReader(new InputStreamReader(url.openStream())); 

    in = new BufferedReader(new InputStreamReader(test.getInputStream()));  
    String htmlContent = ""; 
    for(String inputLine = ""; (inputLine = in.readLine()) != null;) 
     htmlContent += inputLine; 
    System.out.println(htmlContent); 

而不是在=新的BufferedReader (新的InputStreamReader(url.openStream()));,请尝试in = new BufferedReader(new InputStreamReader(test.getInputStream()));,因为它听起来非常符合从实际的HttpURLConnection对象中打开您的InputStream。请让我知道你是否仍然空白页。标题调整可能会让你的HTTP 200代码,而不是302 :),并试图编辑时,你也会得到的HTML文件。我仍然对这个反对票感到惊讶:D和网站是否不喜欢Java并不重要,因为Java喜欢网站。如果您真的想了解http头部分,请尝试https://en.wikipedia.org/wiki/List_of_HTTP_header_fields以获取http头文件和用法的详细说明。

+0

为什么?这里的Connection:close的目的是什么?和“接受”?和“Accept-Charset”?和“接受语言”。一个纯魔法药水是不够的。你必须*解释。* – EJP

+0

**仅仅是一个魔法药水是不够的**,完全同意,但至少他会确保代码正在运行并给出准确的结果? **你必须解释**,我当然会,一旦他运行代码并返回解释??因为在我甚至想到将它上传为答案之前,刚刚得到降级的内容实际上已经经过了准确测试。 – ShayHaned

+0

@ShayHaned我添加了代码,正如问题中所示,但它仍然没有给出完整页面。 – ds777fighter