Java-Einsatz im Netzwerk

Beispiel: URL-Informationen

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

public class ExamineURL {
static int keyWidth = 20;

public static void main( String args[]) {
// Kopieren des Argumentes beim Aufruf
String link = args[0];

try {
System.out.println("Trying URL " + link );
URL url = new URL( link );
URLConnection conn = url.openConnection();
int n = 1;

// Ausgabe der Elemente des http-Headers
String key;
System.out.println("Header fields:");
while((key=conn.getHeaderFieldKey(n) ) != null ) { key += ": ";
while( key.length() < keyWidth ) key += " ";
System.out.println( n + " " + key + conn.getHeaderField(n) );
++n;
}

// Objekt für Zugriff auf Inhalt
Object o = conn.getContent();
System.out.println( "Content: " + o );
System.out.println( "ContentType: " + conn.getContentType() );
System.out.println( "Permission: " + conn.getPermission() );

} catch( Exception ex ) {
System.out.println("Cannot create URL " + link + " Excption:" + ex);
}
}
}

Ruft man diese Anwendung mit der Homepage der FH Friedberg als Parameter auf, erhält man folgende Ausgabe:

java ExamineURL http://www.fh-friedberg.de
Trying URL http://www.fh-friedberg.de
Header fields:
1 Date: Mon, 16 Dec 2002 08:56:52 GMT
2 MIME-Version: 1.0
3 Content-Type: text/html
Content: java.io.PushbackInputStream@3169f8
ContentType: text/html
Permission: (java.net.SocketPermission
www.fh-friedberg.de:80 connect,resolve)