Home Java Example Java Io Downloading and Viewing html source of a page i.e. running on the server
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Downloading and Viewing html source of a page i.e. running on the server

Advertisement
This section illustrates you the procedure of viewing complete html code of a page i.e. running on the server.

Downloading and Viewing html source of a page i.e. running on the server

     

This section illustrates you the procedure of viewing complete html code of a page i.e. running on the server. This section provides you an example with the complete code of the program which views the html source code of your given page running on server. Following program strictly takes a url (Uniform Resource Locator) started with the "http://" otherwise it will generate an error which is caught by the MalformedURLException in catch block.

 

 

Here is the code of the program:

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

public class SourceViewer{
  public static void main (String[] argsthrows IOException{
  System.out.print("Enter url of local for viewing html source code: ");
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  String url = br.readLine();
  try{
  URL u = new URL(url);
  HttpURLConnection uc = (HttpURLConnectionu.openConnection();
  int code = uc.getResponseCode();
  String response = uc.getResponseMessage();
  System.out.println("HTTP/1.x " + code + " " + response);
  for(int j = 1; ; j++){
  String header = uc.getHeaderField(j);
  String key = uc.getHeaderFieldKey(j);
  if(header == null || key == null)
  break;
  System.out.println(uc.getHeaderFieldKey(j": " + header);
  }
  InputStream in = new BufferedInputStream(uc.getInputStream());
  Reader r = new InputStreamReader(in);
  int c;
  while((c = r.read()) != -1){
  System.out.print((char)c);
  }
  }
  catch(MalformedURLException ex){
  System.err.println(url + " is not a valid URL.");
  }
  catch(IOException ie){
  System.out.println("Input/Output Error: " + ie.getMessage());
  }
  }
}

Program Description:

Following program takes an url name which should be started with "http://" otherwise it will generate an error like this:

C:\work\chandan>java SourceViewer
Enter url of local for viewing html source code: www.google.com
www.google.com is not a valid URL.

And if the file name (given through the url) exists then the program gives you the complete html source code of the file.

Code Description:

Various methods and APIs have been used in the above program. These are illustrated as follows:

HttpURLConnection:
This is the abstract class of the java.net package. This class extends the URLConnection class of the java.net package and facilitates all same facilities of the URLConnection class with the specific HTTP features specially.

getResponseCode():
This is the method of the HttpURLConnection class that returns a integer value which is the code for the status from the http response message.

getResponseMethod():
This is also the method of the HttpURLConnection class that show the message with the http response code from the server.

getHeaderFieldKey(int j):
Above method returns the key for the given jth header field. Given j is the argument of the method. And this method returns the String type value that will be the key value.

Output of the program:

C:\work\chandan>javac SourceViewer.java

C:\work\chandan>java SourceViewer
Enter url of local for viewing html source code: http://192.168.10.3/
HTTP/1.x 200 OK
Date: Sat, 23 Dec 2006 12:15:34 GMT
Server: Apache/2.0.55 (Win32) PHP/5.0.0
Content-Location: index.html.en
Vary: negotiate,accept-language,accept-charset
TCN: choice
Last-Modified: Sun, 21 Nov 2004 14:35:22 GMT
ETag: "13745-5d6-a659be80;1375b-9dc-a659be80"
Accept-Ranges: bytes
Content-Length: 1494
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/html
Content-Language: en
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test Page for Apache Installation</title>
</head>
<!-- Background white, links blue (unvisited), navy (visited), red
(active) -->
<body bgcolor="#FFFFFF" text="#000000" link="#0000FF"
vlink="#000080" alink="#FF0000">
<p>If you can see this, it means that the installation of the <a
href="http://www.apache.org/foundation/preFAQ.html">Apache web
server</a> software on this system was successful. You may now add
content to this directory and replace this page.</p>

<hr width="50%" size="8" />
<h2 align="center">Seeing this instead of the website you
expected?</h2>

<p>This page is here because the site administrator has changed the
configuration of this web server. Please <strong>contact the person
responsible for maintaining this server with questions.</strong>
The Apache Software Foundation, which wrote the web server software
this site administrator is using, has nothing to do with
maintaining this site and cannot help resolve configuration
issues.</p>

<hr width="50%" size="8" />
<p>The Apache <a href="manual/">documentation</a> has been included
with this distribution.</p>

<p>You are free to use the image below on an Apache-powered web
server. Thanks for using Apache!</p>

<div align="center"><img src="apache_pb.gif" alt="" /></div>
</body>
</html>


C:\work\chandan>_

Download this example.

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 17, 2007

Ask Questions?    Discuss: Downloading and Viewing html source of a page i.e. running on the server   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Lalit Mali
December 8, 2011
Thank YOu

Nice Nice. You helped with a problem with my project. :)
kanth kumar
April 12, 2012
some problem with your code!!!!

your code works alright without any error... but if I try to extract the source code of the webpage www.simran.in i'm not able to extract the complete source code. what I mean is,the extracted source code of this webpage using the above prog doesn't match with code present in view->pagesource of the same web page.. if you are able to fix it kindly mail the updated prog to my id kanthkumar46@gmail.com.. thanks in advance regards kanthkumar
Suraj Tomar
September 7, 2012
to save a file

try { // Create file FileWriter fstream = new FileWriter(OUTPUT_FILE_NAME, true); BufferedWriter out = new BufferedWriter(fstream); out.write(( char) c); // Close the output stream System.out.print((char)c); out.close(); } catch (Exception e) {// Catch exception if any System. err.println("Error: " + e.getMessage()); }
amit kumar
September 10, 2012
file proyection system

my project give me help.
DMCA.com