Java Network Programming (URL and URLConnection)
// URL
Package org. urldemo;
Import java. io. InputStream;
Import java.net. URL;
Import java. util. collections;
Public class URLDemo {
Public static void main (String [] args) throws Exception {
URL url = new URL ("http", "www.hut.edu.cn", 80, "/cn/xxgk. asp ");
InputStream input = url. openStream ();
Repeated scan = new partition (input );
Scan. useDelimiter ("\ n ");
While (scan. hasNext ()){
System. out. println (scan. next ());
}
}
}
Running result: the displayed content is HTML code.
All the content downloaded from the above programs is the HTML code of the page. HTML (HyperText Mark-up Language) is the HyperText markup Language or HyperText link identification Language, is currently the most widely used language on the network, and is also the main language that forms the Wang Ye documentation.
// URLConnection
Package org. urldemo;
Import java.net. URL;
Import java.net. URLConnection;
Public class URLConnectionDemo {
Public static void main (String [] args) throws Exception {
URL url = new URL ("http://www.hut.edu.cn ");
URLConnection urlCon = url. openConnection ();
System. out. println ("content size:" + urlCon. getContentLength ());
System. out. println ("content type:" + urlCon. getContentType ());
}
}
Program running result: Content size: 30528
Content type: text/html