Java Network Programming: you must know the URL and URLConnection will be

java.net.URLThe URL address-based package, and provides a basic method for parsing the URL address, such as access to the host name and port number of the URL. java.net.URLConnectionIt represents the communications link between the application and URL, can be used to read and write to the resource URL reference.

URLConnection looks like just one more than the Connection URL, the relationship between them only so far right?

01, what is the URL

To understand what is a URL, the need to introduce two new concepts URI and URN.

What the hell, URL no clear, again confused the two? Do not worry, I can change like magic, like the three so that we are clear.

  • URI = Universal Resource Identifier, Chinese interpretation for the uniform resource identifier
  • URL = Universal Resource Locator, Chinese interpretation for the uniform resource locator
  • URN = Universal Resource Name, Chinese interpretation as Uniform Resource Name

The relationship between them is shown below:

 

 

This chart What do you mean, ah, how to do it? Zhang Xiaojing there are problems Ge guy to ask, we will not just ask "Wikipedia" ah.

URI URL and can be divided into URN, URL or URN and the combination (along with Locator and Name). URN is like a person's name, URL is like a person's address. In other words: URN to determine the identity, URL provides a way to find it.

The concept clearly, right? URI is a purely syntactic structure, the various parts of the string used to specify the identity of a Web resource. A URL is a special case of URI, including the positioning of Web resources sufficient information. URI is a uniform resource identifier, and the URL is a Uniform Resource Locator. A URL is a URI, for example: http: //www.itmind.net/. But not all URI are URL, because URI might include a subset, or Uniform Resource Name (URN, named resource but does not specify how to locate resources), for example: mailto: [email protected].

It it it it so much tiring to examples out of it, get the URL for the host name and port number.

URL url = new URL("http://www.itmind.net/category/payment-selection/zhishixingqiu-jingxuan/");

System.out.println("host: " + url.getHost());
System.out.println("port: " + url.getPort());
System.out.println("uri_path: " + url.getPath());

// 输出
// host: www.itmind.net
// port: -1
// uri_path: /category/payment-selection/zhishixingqiu-jingxuan/

1) Create a java.net.URLmethod of the object is very simple and requires only a single line of code.

URL url = new URL(URL地址);

URL object is immutable, because the URL class is final, and this advantage is to ensure that it is "thread-safe".

2) With java.net.URLthe object, you can obtain the URL associated with the host name, port, path, and so on.

url.getHost()
url.getPort()
url.getPath()

02, what is the URLConnection

URLConnection is the communication link between an abstract class that represents the application and URL. It's an example that can be used to read and write to the resource URL reference. This class provides an easier to use than the Socket class, more advanced network connections abstract.

How to obtain URLConnection object? URL object by openConnection()methods exemplified below.

URL url = new URL("http://www.itmind.net");
URLConnection connection = url.openConnection();

If the URL protocol is HTTP, then returned as a subclass of URLConnection connection HttpURLConnection.

With the URLConnectionobject, you can getInputStream()return an InputStream, thereby reading the resource data (if read ASCII text ASCII URL was cited; if the HTML file is read, compared with the original HTML, if the image file was read binary picture data, etc.).

Let's try to read about the contents of a small Bai Xuetang home page, the code sample below.

URL url = new URL("http://www.itmind.net");
URLConnection connection = url.openConnection();

try (InputStream in = connection.getInputStream();) {

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = -1;
    while ((len = in.read(buffer)) != -1) {
        output.write(buffer, 0, len);
    }

    System.out.println(new String(output.toByteArray()));

catch (IOException e) {
    e.printStackTrace();
}

You can try-with-resourceobtain InputStream, which implements the AutoCloseableinterface can be automatically closed after the contents of the input stream have been read.

Print content as shown below (section):

 

 

If you want to read the contents of a URL, the above method is a good program, hurry to try it!

03, URL and URLConnection different

URL and URLConnection biggest difference is that:

  • URLConnection provides access to the HTTP header;
  • URLConnection may be configured to send a URL request parameters;
  • Not only can read the resource URLConnection URL targeting, you can also write data.

Get the HTTP header of some of the following methods:

  • the getContentType, returns the value of the Content-type header field, i.e. the MIME content type of the data. If the type is not available, null is returned. If the content type is text, the Content-type header may contain an identification of the contents of the character set encoding, such as:Content-type:text/html; charset=UTF-8

  • getContentLength (), returns the Content-length header field value, i.e., the number of bytes of content.

  • getContentEncoding (), returns Content-encoding header field value, i.e., the content encoding (encoding in a different character), for example: x-gzip.

  • getDate (), returns the date header field, i.e., the transmission time of the request.

  • (), The value returned Expires (expiration) header field getExpiration. If it returns 0, that does not expire, cached forever.

  • (), The value returned last-modified (last modified date) header field getLastModified.

Code Example below.

URL url = new URL("http://www.itmind.net");
URLConnection connection = url.openConnection();
System.out.println(connection.getContentType());
System.out.println(connection.getContentLength());
System.out.println(connection.getContentEncoding());
System.out.println(connection.getDate());
System.out.println(connection.getExpiration());
System.out.println(connection.getLastModified());

// 输出
// text/html; charset=UTF-8
// -1
// null
// 1566886980000
// 0
// 0

 

Guess you like

Origin www.cnblogs.com/qing-gee/p/11489556.html