Skip to content Skip to sidebar Skip to footer

Reading Web Page Source Code In Java Differs From The Orginal Webpage Source Code

I am trying to implement program to read webpage source code and save it in text file then do some operations in it but the problem when I read web page source code , there is diff

Solution 1:

You have to set the user the 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");

Full code:

    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();

Result

<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;" />

Post a Comment for "Reading Web Page Source Code In Java Differs From The Orginal Webpage Source Code"