import java.io.*;
import java.net.*;
import java.util.Scanner;
/** Fetches the HTML content of a web page (or HTTP header) as a String. */
public final class WebPageFetcher {
/**
* Demo harness.
*
* <ul>
* <li>aArgs[0] : an HTTP URL
* <li>aArgs[1] : (header | content)
* </ul>
*/
public static void main(String... aArgs) throws MalformedURLException {
String url = aArgs[0];
String option = aArgs[1];
WebPageFetcher fetcher = new WebPageFetcher(url);
if ( HEADER.equalsIgnoreCase(option) ) {
log( fetcher.getPageHeader() );
}
else if ( CONTENT.equalsIgnoreCase(option) ) {
log( fetcher.getPageContent() );
}
else {
log("Unknown option.");
}
}
public WebPageFetcher( URL aURL ){
if ( ! HTTP.equals(aURL.getProtocol()) ) {
throw new IllegalArgumentException("URL is not for HTTP Protocol: " + aURL);
}
fURL = aURL;
}
public WebPageFetcher( String aUrlName ) throws MalformedURLException {
this ( new URL(aUrlName) );
}
/** Fetch the HTML content of the page as simple text. */
public String getPageContent() {
String result = null;
URLConnection connection = null;
try {
connection = fURL.openConnection();
Scanner scanner = new Scanner(connection.getInputStream());
scanner.useDelimiter(END_OF_INPUT);
result = scanner.next();
}
catch ( IOException ex ) {
log("Cannot open connection to " + fURL.toString());
}
return result;
}
/** Fetch HTML headers as simple text. */
public String getPageHeader(){
StringBuilder result = new StringBuilder();
URLConnection connection = null;
try {
connection = fURL.openConnection();
}
catch (IOException ex) {
log("Cannot open connection to URL: " + fURL);
}
//not all headers come in key-value pairs - sometimes the key is
//null or an empty String
int headerIdx = 0;
String headerKey = null;
String headerValue = null;
while ( (headerValue = connection.getHeaderField(headerIdx)) != null ) {
headerKey = connection.getHeaderFieldKey(headerIdx);
if ( headerKey != null && headerKey.length()>0 ) {
result.append( headerKey );
result.append(" : ");
}
result.append( headerValue );
result.append(NEWLINE);
headerIdx++;
}
return result.toString();
}
// PRIVATE //
private URL fURL;
private static final String HTTP = "http";
private static final String HEADER = "header";
private static final String CONTENT = "content";
private static final String END_OF_INPUT = "\\Z";
private static final String NEWLINE = System.getProperty("line.separator");
private static void log(Object aObject){
System.out.println(aObject);
}
}
Written by Naresh in
http header
Popular Posts
-
import java.io.*; import java.net.*; import java.util.Scanner; /** Fetches the HTML content of a web page (or HTTP header) as a String....
-
import java.util.*; import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; class common extends Panel im...
-
Calculator.java public interface Calculator extends java.rmi.Remote { public long add(long a,long b) throws java.rmi.RemoteException; ...
-
Client import java.io.*; import java.net.*; class client { public static void main(String args[])throws Exception { String str;...
-
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Bully { static int n,pc,in...
-
import java.net.*; import java.io.IOException; import javax.swing.*; public class Trial { public static void main(String args[]) { ...
-
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Ring { static int n,pc,i...