Comparison: HTTP with Java, Perl or Python

J Swartz js_nntp at nospam_blackrocksoftware.com
Sat Jun 8 07:23:20 EDT 2002


On 6/7/02 2:38 PM, in article 3D0127D2.2060604 at gmx.de, "Ingo Linkweiler"
<i.linkweiler at gmx.de> wrote:
> thanks a lot for your help.
> 
> Do you know SHORTER solutions in Java?

Here's one:

public class URLTest {
    public static void main(String[] arguments) {
        String myurl = "http://www.uni-dortmund.de";
        try {
            java.io.BufferedReader reader = new java.io.BufferedReader
( new java.io.InputStreamReader((new java.net.URL(myurl)).openStream()) );
            for (String curLine = reader.readLine(); curLine != null;
curLine = reader.readLine())
                System.out.println( curLine );
            reader.close();
        } catch (java.io.IOException ex) { System.out.println(ex); }
    }
}


Instead of reading the url completely before printing, this version prints
line by line. Still, it's pretty close. The java.io package could definitely
use a simple method to download the complete contents of a stream at once.

Below is a similar version:

public class URLTest {
    public static void main(String[] arguments) {
        String myurl = ( "http://www.uni-dortmund.de" );
        try {
            java.io.BufferedInputStream input = new
java.io.BufferedInputStream( (new java.net.URL(myurl)).openStream() );
            java.io.ByteArrayOutputStream output = new
java.io.ByteArrayOutputStream();
            for (int val = input.read(); val != -1; val = input.read())
                output.write( val );
            input.close();
            System.out.println( output );
        } catch (java.io.IOException ex) { System.out.println(ex); }
    }
}

This one is 2 lines longer but it is closer in function to your sample
program.



- JS










More information about the Python-list mailing list