This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Java in General and the fly likes Get HTML Source Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic

Get HTML Source

An Raj

Joined: Aug 10, 2006
Posts: 6
I am trying to get the HTML source for page.
The following is my code. But I get "openConnection() failed".
So, it is not getting connected. Any input will be appreciated.

Or if you can think of a better way of doing this. May be getting a handle using the DOM ( W3C etc) please let me know.


public class URLReader {
public static void main(String[] args) throws Exception {
try {
URL google = new URL("");

URLConnection gl = google.openConnection();

BufferedReader in = new BufferedReader(
new InputStreamReader(

String inputLine;

while ((inputLine = in.readLine()) != null)

// yahooConnection.connect();

} catch (MalformedURLException e) { // new URL() failed
System.out.println("new URL() failed");

} catch (IOException e) { // openConnection() failed
System.out.println("openConnection() failed");



Campbell Ritchie

Joined: Oct 13, 2005
Posts: 44616
Please don't start a 2nd thread on the same subject.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19758

(Actually the first post was closed, so starting a new post was necessary.)

An Raj, you would get better information if you printed the stack trace, like this:
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63354

Originally posted by Paul Clapham:
(Actually the first post was closed, so starting a new post was necessary.)

No, it was moved. Closing this dupe.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: Get HTML Source
It's not a secret anymore!