I´m searching for a opportunity to open a url in java.
URL url = new URL("http://maps.google.at/maps?saddr=4714&daddr=Marchtrenk&hl=de");
InputStream is = url.openConnection().getInputStream();
BufferedReader reader = new BufferedReader( new InputStreamReader( is ) );
String line = null;
while( ( line = reader.readLine() ) != null ) {
System.out.println(line);
}
reader.close();
I found that way.
I added it in my program and the following error occurred.
The method openConnection() is undefined for the type URL
(by url.openConnection())
What is my problem?
I use a tomcat-server with servlets, ...
Following code should work,
It may be more useful to use a http client library like such as this
There are more things like access denied , document moved etc to handle when dealing with http.
(though, it is unlikely in this case)
Are you sure using the
java.net.URL
class? Check your import statements.If you just want to open up the webpage, I think less is more in this case:
I found this question while Googling. Note that if you just want to make use of the URI's content via something like a string, consider using Apache's
IOUtils.toString()
method.For example, a sample line of code could be: