File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes Copy the content(view source) of an html file running in browser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Copy the content(view source) of an html file running in browser" Watch "Copy the content(view source) of an html file running in browser" New topic
Author

Copy the content(view source) of an html file running in browser

Afroz Ahmed
Ranch Hand

Joined: Jan 18, 2004
Posts: 64
Hello,

I want to copy the content(which we will get by view source in IE browser) to my local disc file.This must be done automatically with Java code.Is it possible using java.net package?If yes,please tell me how to do that?


The value of an idea lies in the usage of it.
Afroz Ahmed
Ranch Hand

Joined: Jan 18, 2004
Posts: 64
Hello,

I tried the below program.

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

public class URLGet
{
public static void main(String[] args) {
BufferedReader in=null;
System.out.println("Starting... ");
if (args.length == 1) {
try {
URL url = new URL(args[0]);
in = new BufferedReader( new InputStreamReader(url.openStream()));
BufferedWriter writer=new BufferedWriter(new FileWriter(new File("myfile.txt")));
String line=null;
while ((line=in.readLine()) != null)
{
System.out.println(line);
writer.write(line,0,line.length());
}
}
catch (MalformedURLException ex) {
System.err.println(ex);
}
catch (FileNotFoundException ex) {
System.err.println("Failed to open stream to URL: "+ex);
}
catch (IOException ex) {
System.err.println("Error reading URL content: "+ex);
}
if (in != null)
try {in.close();} catch (IOException ex) {}
}
else
System.err.println ("Usage: URLGet URL");
}
}


Running: java URLGet http://google.com

But it is giving the error
Error reading URL content: java.net.ConnectException: Connection timed out: connect
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
I didn't get ConnectException when I tried to run the program. (I used different URL since I'm behind firewall/proxy). Try

It should work if you have direct internet connection.

Empty file is created but that's another problem (try closing the writer).
[ August 10, 2004: Message edited by: Vlado Zajac ]
Afroz Ahmed
Ranch Hand

Joined: Jan 18, 2004
Posts: 64
Hi,

I am getting the content for http://localhost:8080/examples/index.html
But why not for http://google.com.

Tell me the solution.
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
1.
It's http://www.google.com (with www). It seems that http://google.com is also ok but it may only send redirect to www.google.com.

2. You may be behind firewall/proxy server (I get NoRouteToHostException in this case)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Copy the content(view source) of an html file running in browser
 
Similar Threads
line breaks in text file
How to display a property in JSP?
Is it possible to get the generated HTML code at the end of JSP
problem in getInitParameterNames() method of ServletConfig
JSF Components does not appear on the browser