web page retrieval

A

anonymous

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.
 
C

Chris Smith

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
 
A

Andrew Thompson

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.

Isn't it about time you Bob, that you
found the JavaDocs and tutorials, and
headed over to c.l.j.help?
 
K

kaeli

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.

A snippet from code I have that does this very thing...


URL url=null;
String line=null;
InputStream in=null;
BufferedReader input=null;
URLConnection connection=null;
String pageContent="";


url = new URL("http://www.google.com");
connection = url.openConnection();
connection.setDoInput(true);
in = connection.getInputStream();
input = new BufferedReader(new InputStreamReader(in));

while ((line = input.readLine()) != null)
{
// put the whole thing into a string
pageContent += line;
}


--
--
~kaeli~
The man who fell into an upholstery machine is fully
recovered.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top