• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
You may have just won ten million dollars! Or, maybe a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic