Home >Java >javaTutorial >Java method to query DNS/IP address based on website address
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 and directly uses the API provided by the Java platform for operation.
DNS is Domain Name Service, which is 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, which may include protocols such as file:.
So here we use the URL class. The code to obtain the port number is as follows:
/** * 获取端口号 * * @param href 网址, ftp, http, nntp, ... 等等 * @return * @throws IOException */ public static int parsePort(String href) throws IOException { // URL url = new URL(href); // 端口号; 如果 href 中没有明确指定则为 -1 int port = url.getPort(); if (port < 0) { // 获取对应协议的默认端口号 port = url.getDefaultPort(); } return port; }
The URL class is a class that existed in the early days of Java. . The internal logic is relatively complex. If you are interested, you can view the relevant JDK implementation code yourself.
There are two methods to get the port number:
getPort() is to get the port number specified in the URL. If not specified, -1 is returned.
getDefaultPort() is to obtain the default port number corresponding to the protocol. For example, the default port number of the http protocol is 80, the default port number of the https protocol is 443, etc.
Then let’s look at the code to extract the Host part:
/** * 获取Host部分 * * @param href 网址, ftp, http, nntp, ... 等等 * @return * @throws IOException */ public static String parseHost(String href) throws IOException { // URL url = new URL(href); // 获取 host 部分 String host = url.getHost(); return host; }
Essentially, you can also directly intercept the Host through regular expressions or String, but If you encounter a complicated situation, it is not easy to handle, for example: a complex URL like https://yourname:passwd@gitee.com/mumu-osc/NiceFish.git.
After extracting the domain name, you can use the java.net.InetAddress class to find the IP address.
The code is as follows:
/** * 根据域名(host)解析IP地址 * * @param host 域名 * @return * @throws IOException */ public static String parseIp(String host) throws IOException { // 根据域名查找IP地址 InetAddress inetAddress = InetAddress.getByName(host); // IP 地址 String address = inetAddress.getHostAddress(); return address; }
As you can see, we use the InetAddress.getByName() static method to find the IP.
This class also provides other static methods, but they are generally not used much. If you are interested, you can click on the open source code to take a look.
Then, we conduct a simple test through the main() method:
public static void main(String[] args) throws IOException { // String href = "http://www.cncounter.com/tools/shorturl.php"; // 端口号 int port = parsePort(href); // 域名 String host = parseHost(href); // IP 地址 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.com port=80 address=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. However, the disadvantage of this library is that the various versions are not compatible with each other, and the API is often changed. When programming, it needs to be processed according to a specific version number.
The complete code is as follows:
import java.io.IOException; import java.net.*; /** * 查找IP地址 */ public class TestFindDNS { public static void main(String[] args) throws IOException { // String href = "http://www.cncounter.com/tools/shorturl.php"; // 端口号 int port = parsePort(href); // 域名 String host = parseHost(href); // IP 地址 String address = parseIp(host); // System.out.println("host=" + host); System.out.println("port=" + port); System.out.println("address=" + address); } /** * 获取端口号 * * @param href 网址, ftp, http, nntp, ... 等等 * @return * @throws IOException */ public static int parsePort(String href) throws IOException { // URL url = new URL(href); // 端口号; 如果 href 中没有明确指定则为 -1 int port = url.getPort(); if (port < 0) { // 获取对应协议的默认端口号 port = url.getDefaultPort(); } return port; } /** * 获取Host部分 * * @param href 网址, ftp, http, nntp, ... 等等 * @return * @throws IOException */ public static String parseHost(String href) throws IOException { // URL url = new URL(href); // 获取 host 部分 String host = url.getHost(); return host; } /** * 根据域名(host)解析IP地址 * * @param host 域名 * @return * @throws IOException */ public static String parseIp(String host) throws IOException { // 根据域名查找IP地址 InetAddress.getAllByName(host); InetAddress inetAddress = InetAddress.getByName(host); // IP 地址 String address = inetAddress.getHostAddress(); return address; } }
OK, please encapsulate and process appropriately according to the specific situation.
The above content is how to query the DNS/IP address based on the URL in Java. I hope it can help everyone.
Related recommendations:
Detailed explanation of variable length parameter code in Java
Complete code example of Java encryption, decryption and digital signature
PHP programmers quickly develop Java
The above is the detailed content of Java method to query DNS/IP address based on website address. For more information, please follow other related articles on the PHP Chinese website!