File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Given a URL, how could I read the html Source code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Given a URL, how could I read the html Source code?" Watch "Given a URL, how could I read the html Source code?" New topic
Author

Given a URL, how could I read the html Source code?

lili la
Greenhorn

Joined: Nov 05, 2001
Posts: 3
Given a URL, how could I read the html Source code?
Thanks.
Kris Nelson
Ranch Hand

Joined: Nov 04, 2001
Posts: 35
I had started working on something for a JavaServer Page that would read the contents between the html <title></title> tags to find the title of the page. What I was looking at was the java.net.URL class and the related classes in the Javadoc. There is a method:
object URL.getContent()
in the URL class. The object returned has to do with the Content Type and the MIME, etc. That's where I realized it was more trouble than it was worth for my purposes. Since all the URLs I were interested in where on my server, I just used the Servlet methods to obtain the real path of the files and accessed them with the java.io file classes. I doubt that would work with files that don't belong to you, however
I'm sure it can be done, it's just gonna take some research either on the web or throught the javadoc.
Good Luck.

------------------
---
WebNelly.com
Java/XML Web Development
Check it out!
http://www.webnelly.com


WebNelly.com<br />Java/XML Web Development<br />Check it out!<br /><a href="http://www.webnelly.com" target="_blank" rel="nofollow">http://www.webnelly.com</a>
lili la
Greenhorn

Joined: Nov 05, 2001
Posts: 3
Thank you
Colin Kenworthy
Ranch Hand

Joined: Aug 06, 2001
Posts: 88
You might find something of interest here.
http://java.sun.com/docs/books/tutorial/networking/urls/readingWriting.html
Kris Nelson
Ranch Hand

Joined: Nov 04, 2001
Posts: 35
Somebody in a different part of the forum was having a similar problem, and I think he figured it out. Here's the URL:
http://www.javaranch.com/ubb/Forum7/HTML/006735.html
He might be just the right person to help you out.

------------------
WebNelly.com
Java/XML Web Development
Check it out!
http://www.webnelly.com
lili la
Greenhorn

Joined: Nov 05, 2001
Posts: 3
Thank you All.
I'm now able to solve the problem.
Kris Nelson
Ranch Hand

Joined: Nov 04, 2001
Posts: 35
I was curious on how it worked out. Got any details of what you did?

------------------
WebNelly.com
Java/XML Web Development
Check it out!
http://www.webnelly.com
BJ Grau
Ranch Hand

Joined: Jul 10, 2001
Posts: 234
Just do something like this:
<code>
URL url = new URL("http://www.javaranch.com");
HttpUrlConnection conn = new HttpUrlConnection(url);
url.connect();
InputStream in = url.getInputStream();
BufferedReader reader =
new BufferedReader(new InputStreamReader(in));
</code>
then you can iterate over reader, calling readLine() which will return each line of the html returned.
Kris Nelson
Ranch Hand

Joined: Nov 04, 2001
Posts: 35
Thanks BJ,
It'll be put to good use, I promise

------------------
WebNelly.com
Java/XML Web Development
Check it out!
http://www.webnelly.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Given a URL, how could I read the html Source code?