2017-02-19 72 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

相關問題