HTML mit Java anzeigen



  • Hi,

    mit dem nachstehenden Java Programm möchte ich den HTML Quellcode einer Webseite
    anzeigen lassen, aber es geht schief. Warum?

    Danke

    public class Webseite
    {
    public static void main(String[] args)
    {
    Socket WebseiteSocket = null;
    DataInputStream is = null;
    String hostname = "www.web.de";

    try {WebseiteSocket = new Socket(hostname, 8080);}
    catch (UnknownHostException e)
    {
    System.err.println("Don't know about host: " + hostname);
    }
    catch (IOException e)
    {
    System.err.println("Couldn't get I/O for the connection to: " + hostname);
    }

    try{is = new DataInputStream(WebseiteSocket.getInputStream());}
    catch (IOException e)
    {
    System.err.println("DataInputStream error");
    }
    catch (NullPointerException e)
    {
    System.err.println("DataInputStream error");
    }

    if (WebseiteSocket != null && is != null)
    {
    try
    {
    System.out.println( "Webseite: " + is.readLine() );
    }
    catch (UnknownHostException e)
    {
    System.err.println("Trying to connect to unknown host: " + e);
    }
    catch (IOException e)
    {
    System.err.println("IOException: " + e);
    }
    }

    try
    {
    is.close();
    WebseiteSocket.close();
    }
    catch (IOException e)
    {
    System.err.println("IOException: " + e);
    }
    catch (NullPointerException e)
    {
    System.err.println("NullPointerException:" + e);
    }
    }
    }
    [java]



  • Hallo,
    aus dem "Handbuch der Javaprogrammierung":

    import java.net.*;
    import java.io.*;
    
    public class Listing4504
    {
      public static void main(String[] args)
      {
        if (args.length != 2) {
          System.err.println(
            "Usage: java Listing4504 <host> <file>"
          );
          System.exit(1);
        }
        try {
          Socket sock = new Socket(args[0], 80);
          OutputStream out = sock.getOutputStream();
          InputStream in = sock.getInputStream();
          //GET-Kommando senden
          String s = "GET " + args[1] + " HTTP/1.0" + "\r\n\r\n";
          out.write(s.getBytes());
          //Ausgabe lesen und anzeigen
          int len;
          byte[] b = new byte[100];
          while ((len = in.read(b)) != -1) {
            System.out.write(b, 0, len);
          }
          //Programm beenden
          in.close();
          out.close();
          sock.close();
        } catch (IOException e) {
          System.err.println(e.toString());
          System.exit(1);
        }
      }
    }
    

    MfG



  • Alternative:

    import java.net.*;
    import java.io.*;
    
    public class DisplayWebsite
    {
      public static void main(String[] args)
      {
        if (args.length != 1) {
          System.err.println(
            "Usage: java DisplayWebsite <url>"
          );
          System.exit(1);
        }
        try {
          URL url = new URL(args[0]);
          InputStream in = url.openStream();
          int len;
          byte[] b = new byte[100];
          while ((len = in.read(b)) != -1) {
            System.out.write(b, 0, len);
          }
          in.close();
        } catch (MalformedURLException e) {
          System.err.println(e.toString());
          System.exit(1);
        } catch (IOException e) {
          System.err.println(e.toString());
          System.exit(1);
        }
      }
    }
    

    Aufruf z.B.: java DisplayWebsite http://www.web.de

    MfG


Anmelden zum Antworten