JAVA notes: Java Network Programming (2) URL and URLConnection,

Source: Internet
Author: User

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 ());}}};


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.