Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Afroz Ahmed
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Afroz Ahmed
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic