Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the knowledge points of java URL?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what are the java URL knowledge points", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "java URL knowledge points what" bar!

01 What is URL?

To figure out what a URL is, you need to introduce two other concepts: URI and URN.

What the hell? I didn't even get the URL clear, and there were two more who couldn't figure it out? don't worry, I can make you figure all three out like magic.

URI = Universal Resource Identifier, Chinese interpretation is Uniform Resource Identifier

URL = Universal Resource Locator, Chinese interpretation is Uniform Resource Locator

URN = Universal Resource Name, Chinese interpretation is Uniform Resource Name

The relationship between them is shown below:

What does this picture mean? What should I do? If Zhang Xiaojing has any questions, ask Ge Lao. We won't just ask Wikipedia.

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

Is the concept clear? A URI is a purely syntactic structure that specifies different parts of a string that identifies a Web resource. URLs are a special case of URIs that contain enough information to locate Web resources. URI is a Uniform Resource Identifier and URL is a Uniform Resource Locator. URL is a URI, such as http://www.itmind.net/. Not all URIs are URLs, however, because URIs may include a subset of Uniform Resource Names (URNs), which name resources but do not specify how they are located, such as mailto: qing_gee@163.com.

It's tiring to say so much, let's make an example of it, for obtaining 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/

1) The way to create java. net.URL objects is very simple, requiring only one line of code.

URL url = new URL(URL address);

The URL object is immutable because the URL class is of type final, which has the advantage of ensuring that it is "thread-safe."

2) With java. net.URL object, you can get URL related host name, port, path, etc.

url.getHost()

url.getPort()

url.getPath()

02 What is URLConnection?

URLConnection is an abstract class that represents a communication link between an application and a URL. An instance of it can be used to read and write to the resource referenced by this URL. This class provides a more user-friendly, higher-level abstraction of network connections than the Socket class.

How do I get URLConnection objects? Through the openConnection() method of the URL object, an example is as follows.

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

URLConnection connection = url.openConnection();

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

With the URLConnection object, you can return an InputStream via getInputStream(), which reads the resource data referenced by the URL (ASCII if reading ASCII text, raw HTML if reading HTML files, binary image data if reading image files, etc.).

Let's try to read the contents of the home page of Xiaobai 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();

}

You can use try-with-resource to get the InputStream, which implements the AutoCloseable interface and automatically closes the input stream after the content is read.

The printed contents are shown in the following figure (partial):

If you want to read the content of a URL, the above method is a good solution, try it quickly!

Difference between URL and URLConnection

The biggest difference between URL and URLConnection is:

URLConnection provides access to HTTP headers;

URLConnection can configure the request parameters sent to a URL;

URLConnection can not only read resources located by URLs, but also write data to them.

There are some ways to get HTTP headers:

getContentType, returns the value of the Content-type header field, i.e. the MIME content type of the data. If the type is unavailable, null is returned. If the content type is text, the Content-type header may contain a character set identifying how the content is encoded, for example: Content-type:text/html; charset= UTF-8

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

getContentEncoding(), returns the value of the Content-encoding header field, i.e. the encoding method of the content (different from the character encoding method), for example: x-gzip.

getDate(), returns the value of the date header field, i.e. the sending time of the request.

getExpiration() returns the value of the expires header field. If it returns 0, it means it will not expire and will be cached forever.

getLastModified() returns the value of the last-modified header field.

The code example is as follows.

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 At this point, I believe that everyone has a deeper understanding of "what are the java URL knowledge points". Let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report