469,366 Members | 2,236 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

HOWTO open URL

I am trying to open a url and read the data into a string.

for example,
I want to see the raw html within www.sourceforge.com put into a String.
Then I'll parse out the data I need.

thanks for any help.

Jul 17 '05 #1
6 57663
Do you want all of it put
into one string or do you
want to read it a line at a time?

Phil..

"dave" <no****@nospam.net> wrote in message news:gk****************@fe04.atl2.webusenet.com...
I am trying to open a url and read the data into a string.

for example,
I want to see the raw html within www.sourceforge.com put into a String.
Then I'll parse out the data I need.

thanks for any help.


Jul 17 '05 #2
All is fine

"Phil" <ry***@ieee.org> wrote in message
news:1BR_a.105374$cF.31350@rwcrnsc53...
Do you want all of it put
into one string or do you
want to read it a line at a time?

Phil..

"dave" <no****@nospam.net> wrote in message
news:gk****************@fe04.atl2.webusenet.com...
I am trying to open a url and read the data into a string.

for example,
I want to see the raw html within www.sourceforge.com put into a String.
Then I'll parse out the data I need.

thanks for any help.


Jul 17 '05 #3
import java.net.*;
import java.io.*;

public class URLReader {
public static void main(String[] args) throws Exception {
URL yahoo = new URL("http://www.sourceforge.com");
BufferedReader in = new BufferedReader(
new InputStreamReader(
yahoo.openStream()));

String inputLine;

while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);

in.close();
}
}

from http://java.sun.com/docs/books/tutor...eadingURL.html

HTH
sandip

"dave" <no****@nospam.net> wrote in message news:<gk****************@fe04.atl2.webusenet.com>. ..
I am trying to open a url and read the data into a string.

for example,
I want to see the raw html within www.sourceforge.com put into a String.
Then I'll parse out the data I need.

thanks for any help.

Jul 17 '05 #4
You can do the following

URL url = new URL(urlString);
InputStream in = url.openStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while ( (aLine = br.readLine()) != null ) {
//do something with aLine
}

Hope this helps,
Daniel Goldman
http://d-goldman.org
Jul 17 '05 #5
Some example working code:
http://www.javapractices.com/Topic147.cjp

dave wrote:
I am trying to open a url and read the data into a string.

for example,
I want to see the raw html within www.sourceforge.com put into a String.
Then I'll parse out the data I need.

thanks for any help.



Jul 17 '05 #6

import java.io.*;
import java.net.*;

public class UrlVisit {

/** Creates a new instance of UrlVisit */
public UrlVisit() {
}

public boolean visitWebPage(String UrlToVisit) {

boolean urlVisited = false;
try {
URL url = new URL(UrlToVisit);
URLConnection urlConnection = url.openConnection();
BufferedReader htmlPage = new BufferedReader(new
InputStreamReader(url.openStream()));

String line = "";
while((line = htmlPage.readLine()) != null) {
//do something with the html line
System.out.println(line);
}
htmlPage.close();
urlConnection = null;
urlVisited = true;
} catch(Exception e) {
urlVisited = false;
}
return urlVisited;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
UrlVisit urlVisit = new UrlVisit();
String url = null;
String commstart = " <!-- *********************** ";
String commend = " *********************** -->";
if (args[0] == null) {
url = "http://www.cnn.com";
} else {
url = args[0];
System.out.println(commstart + "Visiting "+ url + commend);
}
if (urlVisit.visitWebPage(url)) {
System.out.println(commstart + url + " visited" + commend);
} else {
System.out.println(commstart + url + " Website not found" +
commend);
}
}
}
"John" <no****@nospam.com> wrote in message
news:RM********************@magma.ca...
Some example working code:
http://www.javapractices.com/Topic147.cjp

dave wrote:
I am trying to open a url and read the data into a string.

for example,
I want to see the raw html within www.sourceforge.com put into a String.
Then I'll parse out the data I need.

thanks for any help.



Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Logan | last post: by
4 posts views Thread by Josef Sachs | last post: by
4 posts views Thread by Alexander Eisenhuth | last post: by
2 posts views Thread by dmitrey | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.