2014-02-10 48 views
0

我想從網址連接加載網站http://www.povarenok.ru/,但內容爲空。我嘗試過與其他網站 - 所有作品。請幫忙,這個網站有什麼問題?無法通過URL連接從網站加載內容

URL url; 

    try { 

     url = new URL("http://www.povarenok.ru/"); 
     URLConnection conn = url.openConnection(); 

     BufferedReader br = new BufferedReader(new InputStreamReader(
       conn.getInputStream())); 

     String inputLine; 

     String fileName = "c:\\test.html"; 
     File file = new File(fileName); 

     if (!file.exists()) { 
      file.createNewFile(); 
     } 

     FileWriter fw = new FileWriter(file.getAbsoluteFile()); 
     BufferedWriter bw = new BufferedWriter(fw); 

     //inputLine is empty!!! All works with other sites 
     while ((inputLine = br.readLine()) != null) { 
      bw.write(inputLine); 
     } 

     bw.close(); 
     br.close(); 

     System.out.println("Done"); 

    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

回答

1

更改爲:

url = new URL("http://www.povarenok.ru"); 
            ^- no slash here 

看起來是這樣的站點都配置了/到一些其它目的

[編輯]

檢查了一遍,這個斜槓是實際情況並非如此,從我看到它在更改urser-agent之後開始工作(在BufferedReader創建之前放置它):

((HttpURLConnection)conn).setRequestProperty("User-Agent", "SO"); 

提示如何調試在Windows with Fiddler這樣的問題:

你應該先安裝fiddler2 - 它可以讓你查看你的請求。現在

System.setProperty("http.proxyHost", "127.0.0.1"); 
    System.setProperty("https.proxyHost", "127.0.0.1"); 
    System.setProperty("http.proxyPort", "8888"); 
    System.setProperty("https.proxyPort", "8888"); 

,假設你有一個加載網頁瀏覽器中的一個站點,但在你的Java應用程序不加載:在Java應用程序中,添加以下行對應用程序啓動。您必須比較請求標題並找出差異。所以你在webbrowser中加載你的頁面,然後在你的app中加載你的頁面,並使用fiddler比較結果。

+0

我試過沒有斜槓 - 相同的結果=( – zella

+0

更新,希望它會工作 – marcinj

+0

謝謝,現在它的工作! – zella