Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/
Import java.net. url;
Import java. Io. inputstream;
Import java. Io. inputstreamreader;
Import java. Io. bufferedreader;
Import java. Io. ioexception;
Public class readurl1 {
Public static void main (string Arg []) {
String STR;
Try {
URL url = new URL ("http://www.sohu.com /");
Inputstream is = URL. openstream ();
Inputstreamreader ISR = new inputstreamreader (is );
Bufferedreader BR = new bufferedreader (ISR );
While (STR = Br. Readline ())! = NULL)
System. Out. println (STR );
BR. Close ();
} Catch (ioexception e ){
System. Out. println (E );
}
}
}
Another method:
Import java.net. url;
Import java.net. urlconnection;
Import java. Io. inputstream;
Import java. Io. inputstreamreader;
Import java. Io. bufferedreader;
Import java. Io. ioexception;
Public class readurl2 {
Public static void main (string Arg []) {
String STR;
Try {
URL url = new URL ("http://www.sohu.com /");
Urlconnection UC = URL. openconnection ();
Inputstream is = UC. getinputstream ();
Inputstreamreader ISR = new inputstreamreader (is );
Bufferedreader BR = new bufferedreader (ISR );
While (STR = Br. Readline ())! = NULL)
System. Out. println (STR );
BR. Close ();
} Catch (ioexception e ){
System. Out. println (E );
}
}
}
You can also set a proxy:
Import java.net. url;
Import java.net. urlconnection;
Import java. Io. inputstream;
Import java. Io. inputstreamreader;
Import java. Io. bufferedreader;
Import java. Io. ioexception;
Import java. util. properties;
Public class readurl3 {
Public static void main (string Arg []) {
String STR;
Properties props = system. getproperties ();
Props. setproperty ("proxyset", "true ");
Props. setproperty ("proxyport", "8080 ");
Props. setproperty ("proxyhost", "proxy ");
Try {
URL url = new URL ("http://www.sohu.com /");
Urlconnection UC = URL. openconnection ();
Inputstream is = UC. getinputstream ();
Inputstreamreader ISR = new inputstreamreader (is );
Bufferedreader BR = new bufferedreader (ISR );
While (STR = Br. Readline ())! = NULL)
System. Out. println (STR );
BR. Close ();
} Catch (ioexception e ){
System. Out. println (E );
}
}
}
Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/