在java中读取网页源代码与原始网页源代码不同

reading web page source code in java Differs from the orginal webpage source code

我正在尝试实现读取网页源代码并将其保存在文本文件中然后在其中进行一些操作的程序但是当我读取网页源代码时出现问题,原始网页源代码与原始网页源代码之间存在差异java程序网页源代码的输出。

我的程序:

        String inputLine;
        URL link = new URL("http://www.ammanu.edu.jo/English/Articles/newsArticle.aspx?id=2935");
        BufferedReader in = new BufferedReader( new InputStreamReader(link.openStream(),"UTF-8"));

        while ((inputLine = in.readLine()) != null){
                System.out.println(inputLine);
        }
        in.close();

还有我这行源码的问题

原始网页源代码第1156行:

<img id="img" src="http://www.ammanu.edu.jo/AdminImages/20652935/_DSC0246.jpg" style="height:310px;width:400px;border-width:0px;display:block;float:left; padding-right:5px;" />

java程序的实际输出:

<img id="img" src="http://www.ammanu.edu.jo/AdminImages/20652935/_DSC0246.jpg" height="310" width="400" border="0" style="display:block;float:left; padding-right:5px;" />

对此问题有什么建议的解决方案吗?

您必须将用户设置为 User-Agent:

con.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");

完整代码:

    String inputLine;
    URL link = new URL("http://www.ammanu.edu.jo/English/Articles/newsArticle.aspx?id=2935");
    URLConnection con = link.openConnection();
    con.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");

    BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream(),"UTF-8"));

    while ((inputLine = in.readLine()) != null){
        System.out.println(inputLine);
    }
    in.close();

结果

<img id="img" src="http://www.ammanu.edu.jo/AdminImages/20652935/_DSC0246.jpg" style="height:310px;width:400px;border-width:0px;display:block;float:left; padding-right:5px;" />