wrote:
> What is the simplest way to retrieve a web page in Java?
> Ideally, I want to pass in a URL and get back a String.
Since a URL isn't guaranteed to return text data and you don't know the
encoding head of time, it's not that simple. You can certainly create a
URL and get an InputStream, but to convert to characters, you'll need to
use the Content-Type response header to deduce the encoding.
Unfortunately, that's rather complex so I'll wait for you to say you
need it, and then maybe I or someone else will have time to write it for
you. The basics are:
URL url = new URL("http://something");
InputStream stream = url.openStream();
try
{
// read the data from the stream
}
finally
{
stream.close();
}
or, if you want to convert to text with a given character encoding:
URL url = new URL("http://something");
InputStream stream = url.openStream();
try
{
InputStreamReader reader = new InputStreamReader(
stream, "encoding-name");
// read the data from the reader
}
finally
{
stream.close();
}
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation