The java.net.URL class encapsulates URL addresses and provides basic methods for resolving URL addresses, such as obtaining the URL host name and port number. Java.net.URLConnection represents a communication link between an application and a URL that can be used to read and write resources referenced by this URL.
URLConnection looks like just one more Connection than URL. Is that all there is to the relationship between them?
What is a URL
To figure out what a URL is, two other concepts need to be introduced: URI and URN.
What the hell? I don’t know the URL, and two more? Don’t worry, I can magically get everyone to figure out all three.
- URI = Universal Resource Identifier (URI
- URL = Universal Resource Locator (URL
- URN = Universal Resource Name
The relationship between them is shown below:
What does that mean? What can WE do? Zhang Xiaojing has a problem to ask Ge guy, ZA won’t ask “wikipedia”.
Uris can be urls and UrNs, or a combination of urls and UrNs (with Locator and Name). URN is like the name of the person, THE URL is like the address of the person. In other words: The URN establishes the identity, and the URL provides the means to find it.
Is that clear? A URI is a pure syntactic structure for specifying the various parts of a string that identify a Web resource. A URL is a special case of a URI that contains enough information to locate a Web resource. Uris are uniform resource identifiers and urls are uniform resource locators. URL is a type of URI, for example, www.itmind.net/. Not all URIs are urls, however, because URIs may include a subset called a universal resource name (URN, which names the resource but does not specify how to locate it), such as mailto: [email protected].
Let’s get the hostname and port number of the URL.
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());
/ / output
// host: www.itmind.net
// port: -1
// uri_path: /category/payment-selection/zhishixingqiu-jingxuan/
Copy the code
1) Creating a java.net.URL object is simple and requires only one line of code.
URL url = newThe URL (URL);Copy the code
The URL object is immutable because the URL class is of final type, which has the advantage of being “thread-safe”.
2) Once you have the java.net.URL object, you can get the urL-related host name, port, path, and so on.
url.getHost()
url.getPort()
url.getPath()
Copy the code
02, What is URLConnection
URLConnection is an abstract class that represents a communication link between an application and a URL. Instances of it can be used to read and write to the resources referenced by this URL. This class provides a more advanced network connection abstraction that is easier to use than the Socket class.
How do I get the URLConnection object? Through the openConnection() method of a URL object, as shown in the following example.
URL url = new URL("http://www.itmind.net");
URLConnection connection = url.openConnection();
Copy the code
If the URL protocol is HTTP, the connection returned is HttpURLConnection, a subclass of URLConnection.
Once you have an URLConnection object, getInputStream() returns an InputStream that reads the resource data referenced by the URL (ASCII if you read ASCII text; If an HTML file is read it is raw HTML, if an image file is read it is binary image data, etc.).
Let’s try to read the content of the home page of small white school, the code example is as follows.
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();
}
Copy the code
You can use try-with-resource to get the InputStream, which implements the AutoCloseable interface and automatically closes the InputStream when the content has been read.
The printed content is as follows (part) :
If you want to read the content of a URL, the above method is a good solution, go to try it!
03. The difference between URL and URLConnection
The biggest difference between URL and URLConnection is:
- URLConnection provides access to HTTP headers;
- URLConnection can configure request parameters to be sent to a URL.
- URLConnection can not only read the resource located by the URL, but also write data to it.
There are several ways to get HTTP headers:
-
GetContentType, which returns the value of the Content-Type header field, 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 a character set that identifies how the Content is encoded, for example: content-type :text/ HTML; charset=UTF-8
-
GetContentLength (), which returns the value of the Content-Length header field, the number of bytes of the Content.
-
GetContentEncoding (), which returns the value of the content-Encoding header field, i.e. the encoding of the Content (as opposed to the character encoding), for example, X-gzip.
-
GetDate () returns the value of the date header field, which is the time the request was sent.
-
GetExpiration (), returns the value of the Expires header field. If 0 is returned, it does not expire and is cached forever.
-
GetLastModified (), returns the value of the last-Modified header field.
An example of this code is shown 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());
/ / output
// text/html; charset=UTF-8
// -1
// null
/ / 1566886980000
/ / 0
/ / 0
Copy the code
04, finally
Get the hd bookmarked PDF – Java Network Programming (4th edition) carefully prepared for you.