JAVA notes: Java Network Programming (2) URL and URLConnection,
The full name of a URL is Uniform Resource Locator, which is a unified Resource Locator and can be used directly to find resources on the Internet.
URLConnection is a class that encapsulates methods used to access remote network resources. It can be used to establish connections with remote servers and check the attributes of remote resources.
Two instances:
URLConnection:
Import java.net. URL; import java.net. URLConnection; import java. io. inputStream; import java. util. failed; public class URLConnectionDemo {public static void main (String args []) throws Exception {// All exceptions throw URL url = new URL ("http://www.mldnjava.cn"); URLConnection urlCon = url. openConnection (); // establish the connection System. out. println ("content size:" + urlCon. getContentLength (); System. out. println ("content type:" + urlCon. getContentType ());}};
The URL reads the page's IO stream:
Import java.net. URL; import java. io. inputStream; import java. util. failed; public class URLDemo {public static void main (String args []) throws Exception {// All exceptions throw URL url = new URL ("http", "www.mldnjava.cn", 80, "/curriculum.htm"); InputStream input = url. openStream (); // open the input stream consumer scan = new consumer (input); // instantiate consumer class scan. useDelimiter ("\ n"); // you can specify the read separator while (scan. hasNext () {System. out. println (scan. next ());}}};