I am trying to get the HTML source for say...google 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.
import java.net.*;
import java.io.*;
public class URLReader {
public static void main(
String[] args) throws Exception {
try {
URL google = new URL("http://www.google.com/");
URLConnection gl = google.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
gl.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
// yahooConnection.connect();
System.out.println("OK");
} catch (MalformedURLException e) { // new URL() failed
System.out.println("new URL() failed");
} catch (IOException e) { // openConnection() failed
System.out.println("openConnection() failed");
}
}
}
Thanks.