2017-02-16 32 views
-1

我試圖訪問以下網站的內容:http://paraphrase.org/以便通過查詢,不幸的是我沒有得到內容(檢索答案)。我將非常感謝任何幫助,這裏是我的代碼;我試圖訪問該網站的內容:http://paraphrase.org/#/search?q=tree&filter=&lang=en

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

public class PPdbURL { 
    public static void main(String args[]) throws URISyntaxException{ 
     String nextLine;    
     URL url = null;  
     URLConnection urlConn = null;   
     InputStreamReader inStream = null;  
     BufferedReader buff = null;  
     try{ // Create the URL obect that points at the default file index.html        
      url = new URL("http://paraphrase.org/#/search?q=tree&filter=&lang=en");      
      urlConn = url.openConnection();   
      inStream = new InputStreamReader(urlConn.getInputStream());         
      buff= new BufferedReader(inStream); // Read and print the lines from 
      //index.html 

      while (true){    
       nextLine =buff.readLine();    
       if (nextLine !=null){     
        System.out.println(nextLine);    
       }    
       else{    
        break;    
       }   
      }  
     } 
     catch(MalformedURLException e) 
     {   
      System.out.println("Please check the URL:" + e.toString());              
     } 
     catch(IOException e1){  
      System.out.println("Can't read from the Internet: "+ e1.toString());            
     } 
    } 
} 
+0

你想說什麼?例外?無內容? – JustinKSU

+0

我試圖發佈輸出,但它太長了。基本上它只是html,但最後指出的元素與Java腳本有關: Soxell

回答

0

當你說你沒有得到內容時,你的意思是你沒有得到正確的內容?我已經試過你的代碼,它工作正常,即我得到一個HTTP響應。

上面的代碼片段可以識別它的一點是,您不能使用上面的URL向服務器發出HTTP請求,並期望它的行爲類似於釋義網頁。這是因爲URL中的片段標識符('#')。

RFC 3986和RFC 1738都解釋說,片段標識符在derefencing時不是URL或URI的一部分,應該刪除片段標識符後面的所有內容。

所以在上面的例子中,URL連接只被以http://paraphrase.org/

提出在檢查意譯Web文檔,查詢和搜索實際上是被使用JavaScript啓動,在客戶端上,在不同的URL操作。向正在返回JSON資源的端點發出請求。客戶端上的JavaScript然後解析並將結果呈現給瀏覽器。

此HTTP請求的URL低於:

http://paraphrase.org/api/en/search/?batchNumber=0&needsPOSList=true&q=tree

正如你所看到的,這將產生搜索結果數據。

因此,在您的示例中,您應該考慮使用相同的URL,而是將響應主體表示爲JSON。有一些庫來幫助你做到這一點:

http://www.oracle.com/technetwork/articles/java/json-1973242.html https://github.com/FasterXML/jackson

+0

您的回答太棒了,我非常滿意,我非常感謝,但我想知道如何獲取查詢的URL。 – Soxell