import java.net.*;
public class SimplePing
{
public static void main( String[] args )
{
System.out.println( "\nUsage: java SimplePing host [port] \n" +
" e.g.: java SimplePing www.torsten-horn.de \n" +
" or: java SimplePing 1.2.3.4 80 \n" );
try {
if( args.length <= 0 )
throw new Exception( "Parameter missing!" );
InetAddress host = InetAddress.getByName( args[0] );
int port = ( args.length > 1 ) ? Integer.parseInt( args[1] ) : 80;
long tm = System.nanoTime();
Socket so = new Socket( host, port );
so.close();
tm = (System.nanoTime() - tm) / 1000000L;
System.out.println( "Connection ok (port " + port + ", time = " + tm + " ms). \n" +
"Host Address = " + host.getHostAddress() + "\n" +
"Host Name = " + host.getHostName() );
System.exit( 0 );
} catch( Exception ex ) {
System.out.println( "Error: " + ex.getMessage() );
System.exit( 1 );
}
}
}
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TimeFromInternet
{
public static void main( String[] args )
{
final String DEFAULT_TIME_SERVER = "ptbtime1.ptb.de";
final SimpleDateFormat DATUMFORMAT = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
final long SEKUNDEN_1900_1970 = 2208988800L;
Socket so = null;
InputStream in = null;
long time = 0;
try {
so = new Socket( ( args.length > 0 ) ? args[0] : DEFAULT_TIME_SERVER, 37 );
in = so.getInputStream();
for( int i = 3; i >= 0; i-- ) {
time ^= (long) in.read() << i * 8;
}
// Der Time Server gibt die Sekunden seit 1900 aus, Java erwartet Millisekunden seit 1970:
System.out.println( DATUMFORMAT.format( new Date( (time - SEKUNDEN_1900_1970) * 1000 ) ) );
} catch( Exception ex ) {
System.out.println( ex );
} finally {
if( in != null ) try { in.close(); } catch( IOException ex ) {/*ok*/}
if( so != null ) try { so.close(); } catch( IOException ex ) {/*ok*/}
}
}
}
import java.io.*;
import java.net.URL;
public class FileFromUrl
{
public static void main( String[] args )
{
if( 2 != args.length ) {
System.out.println( "\nLoad file from Internet (any file, text as well as binary).\n" +
"Usage: java FileFromUrl url file \n" +
" e.g.: java FileFromUrl http://www.torsten-horn.de x.htm \n" +
" or: java FileFromUrl ftp://ftp.gnu.org/README x.txt \n" );
System.exit( 1 );
}
System.out.println( "\nURL:\n" + args[0] );
long tm = 0;
int len1, len2 = 0;
InputStream is = null;
OutputStream os = null;
try {
URL url = new URL( args[0] );
is = url.openStream();
os = new FileOutputStream( args[1] );
byte[] buff = new byte[4096];
tm = System.nanoTime();
while( -1 != (len1 = is.read( buff )) ) {
os.write( buff, 0, len1 );
len2 += len1;
}
tm = (System.nanoTime() - tm) / 1000000L;
os.flush();
} catch( Exception ex ) {
System.out.println( "\nError: " + ex );
} finally {
if( os != null ) try { os.close(); } catch( IOException ex ) {/*ok*/}
if( is != null ) try { is.close(); } catch( IOException ex ) {/*ok*/}
}
System.out.print( "written to file:\n" + args[1] + "\n(" + len2 + " Bytes in " + tm + " ms" );
System.out.println( (( 1000 < tm ) ? (" = " + ((10*len2+tm/2)/tm)/10. + " KByte/sec") : "") + ")." );
}
}
HTTP-GET-Requests sind der Default (wie obiges Programmierbeispiel 'FileFromUrl' zeigt). Dazu genügt der einfache Aufruf einer URL. Parameter können durch '?' von der URL abgetrennt und untereinander durch '&' getrennt angehängt werden (z.B. "http://www.domain.tld?parm1=val1&parm2=val2").
HTTP-POST-Requests sind etwas aufwändiger zu programmieren, wie dieses Programmierbeispiel 'PostToUrl' zeigt. Eventuell müssen Sie zusätzlich auch Encodings berücksichtigen.
Wenn Sie es wesentlich einfacher und komfortabler haben wollen: Sehen Sie sich
RESTful Web Services mit JAX-RS und Jersey an.
Falls Sie das dort beschriebene
Bücherverwaltung-Programmierbeispiel
starten, können Sie 'PostToUrl' zum Beispiel testen über:
java PostToUrl http://localhost:8080/REST/ws/Artikel/Buecher "isbn=9876543213&titel=PostToUrl-Titel&preis=333"
start http://localhost:8080/REST/ws/Artikel/Buecher
import java.io.*;
import java.net.*;
public class PostToUrl
{
public static void main( String[] args )
{
if( 2 != args.length ) {
System.out.println( "\nSend data per HTTP POST Request.\n" +
"Usage: java PostToUrl url \"parm1=val1&parm2=val2\" \n" );
System.exit( 1 );
}
System.out.println( "\nURL: " + args[0] );
System.out.println( "Data: " + args[1] );
System.out.println( "Response:" );
OutputStream os = null;
InputStream is = null;
try {
// Connection:
URL url = new URL( args[0] );
URLConnection con = url.openConnection();
if( !(con instanceof HttpURLConnection) )
throw new Exception( "Error: Only HTTP allowed." );
((HttpURLConnection) con).setRequestMethod( "POST" );
con.setDoOutput( true );
// Send data:
os = con.getOutputStream();
os.write( args[1].getBytes() );
os.flush();
// Read response:
is = con.getInputStream();
int len;
byte[] buff = new byte[4096];
while( -1 != (len = is.read( buff )) ) {
System.out.print( new String( buff, 0, len ) );
}
System.out.println();
} catch( Exception ex ) {
System.out.println( "\nError: " + ex );
} finally {
if( os != null ) try { os.close(); } catch( IOException ex ) {/*ok*/}
if( is != null ) try { is.close(); } catch( IOException ex ) {/*ok*/}
}
}
}
Unter "jsp-taglibs.htm#ErsteTestanwendungApacheJakartaTaglibs" finden Sie eine einfache JSP-Anwendung, die Dateien vom Internet per HTTP, FTP oder SOAP per JSP Apache Jakarta Taglib "taglibs-io" lädt.
Unter "http://checkip.dyndns.org" können Sie Ihre externe im Internet sichtbare IP-Adresse ermitteln. Diese ist anders als die lokale IP-Adresse Ihres Rechners, wenn Sie über einen NAT-Router ans Internet gekoppelt sind.
Ihre lokale IP-Adresse können Sie unter Windows XP mit 'ipconfig /all' und unter Linux mit 'ifconfig' ermitteln.