Requirements: Given a URL address, for example: http://www.cncounter.com/tools/shorturl.php, parse the corresponding IP address and port number.
Note: This article does not involve the underlying DNS protocol, but directly uses the API provided by the Java platform to operate.
DNS is also Domain Name Service, which is the domain name service.
We know that the classes related to URLs in Java include java.net.URL and java.net.URI, etc., where URI is a resource locator and may include protocols such as file:
So here we use the URL class and the code to get the port number is as follows:
/** * Get the port number* * @param href URL, ftp, http, nntp, ... etc. * @return * @throws IOException */ public static int parsePort(String href) throws IOException { // URL url = new URL(href); // Port number; If not explicitly specified in href, it is -1 int port = url.getPort(); if (port < 0) { // Get the default port number of the corresponding protocol port = url.getDefaultPort(); } return port; }The URL class is a class that existed in Java in the early days. The internal logic is quite complex. If you are interested, you can view the relevant JDK implementation code by yourself.
There are 2 ways to get the port number:
getPort() is to get the port number specified in the URL. If not specified, it returns -1.
getDefaultPort() is to get the default port number corresponding to the protocol, such as the default port number of the http protocol is 80, the default port number of the https protocol is 443, etc.
Then we look at the code that extracts the Host part:
/** * Get the Host part* * @param href URL, ftp, http, nntp, ... etc* @return * @throws IOException */ public static String parseHost(String href) throws IOException { // URL url = new URL(href); // Get the host part String host = url.getHost(); return host; }In essence, you can also directly intercept Host through regular expressions or String, but if you encounter complex situations, it is not easy to deal with, for example: complex URLs such as https://yourname:[email protected]/mumu-osc/NiceFish.git.
After extracting the domain name, you can search for the IP address through the java.net.InetAddress class.
The code looks like this:
/** * Resolve IP address according to domain name (host) * @param host Domain name * @return * @throws IOException */ public static String parseIp(String host) throws IOException { // Find IP address based on domain name InetAddress inetAddress = InetAddress.getByName(host); // IP address String address = inetAddress.getHostAddress(); return address; }As you can see, we used the InetAddress.getByName() static method to find the IP.
This class also provides other static methods, but it is generally not used very much. If you are interested, you can click the open source code to take a look.
Then, we perform a simple test through the main() method:
public static void main(String[] args) throws IOException { // String href = "http://www.cncounter.com/tools/shorturl.php"; // Port number int port = parsePort(href); // Domain name String host = parseHost(href); // IP address String address = parseIp(host); // System.out.println("host=" + host); System.out.println("port=" + port); System.out.println("address=" + address); }The execution result is:
host=www.cncounter.comport=80address=198.11.179.83
Knowing the IP and port number, we can connect directly through Socket.
Of course, if it is the http protocol, you can use Apache's HttpClient tool, which is powerful and easy to use. But one of the disadvantages of this library is that the versions are not compatible, and the API is often changed, and it needs to be processed according to the specific version number when programming.
The complete code looks like this:
import java.io.IOException;import java.net.*;/** * Find IP address*/public class TestFindDNS { public static void main(String[] args) throws IOException { // String href = "http://www.cncounter.com/tools/shorturl.php"; // Port number int port = parsePort(href); // Domain name String host = parseHost(href); // IP address String address = parseIp(host); // System.out.println("host=" + host); System.out.println("port=" + port); System.out.println("address=" + address); } /** * Get the port number* * @param href URL, ftp, http, nntp, ... etc* @return * @throws IOException */ public static int parsePort(String href) throws IOException { // URL url = new URL(href); // Port number; If not explicitly specified in href, it is -1 int port = url.getPort(); if (port < 0) { // Get the default port number of the corresponding protocol port = url.getDefaultPort(); } return port; } /** * Get the Host part* * @param href URL, ftp, http, nntp, ... etc* @return * @throws IOException */ public static String parseHost(String href) throws IOException { // URL url = new URL(href); // Get the host part String host = url.getHost(); return host; } /** * Resolve IP address based on domain name (host)* * @param host Domain name* @return * @throws IOException */ public static String parseIp(String host) throws IOException { // Find the IP address based on domain name InetAddress.getAllByName(host); InetAddress inetAddress = InetAddress.getByName(host); // IP address String address = inetAddress.getHostAddress(); return address; }}OK, please carry out appropriate packaging and processing according to the specific situation.
Summarize
The above is the Java method introduced by the editor to you by querying DNS/IP addresses based on the URL. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!