2014-02-10 49 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