需求: 給定一個URL地址, 例如: http://www.cncounter.com/tools/shorturl.php, 解析對應的IP地址和端口號。
說明: 本文不涉及底層的DNS 協議, 直接使用Java平台提供的API進行操作。
DNS也就是Domain Name Service,即域名服務。
我們知道, Java中與網址有關的類包括java.net.URL 和java.net.URI 等, 其中URI 是資源定位符, 可能包括file: 之類的協議。
所以此處我們使用URL 類, 獲取端口號的代碼如下:
/** * 獲取端口號* * @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; }URL 類是Java早期就存在的一個類。 內部邏輯比較複雜, 有興趣可以自己查看相關的JDK實現代碼。
其中獲取端口號的2個方法:
getPort() 就是獲取網址裡面指明的端口號, 如果沒有指定, 則返回-1。
getDefaultPort() 是獲取協議對應的默認端口號, 如http 協議默認端口號為80, https 協議默認端口號是443 等。
然後我們看提取Host 部分的代碼:
/** * 獲取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; }本質上, 也可以通過正則表達式或者String直接截取Host, 但如果碰上複雜情況, 也不好處理, 例如: https://yourname:[email protected]/mumu-osc/NiceFish.git 這樣的複雜網址。
提取出域名之後, 可以通過java.net.InetAddress 類來查找IP地址。
代碼如下所示:
/** * 根據域名(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; }可以看到,我們使用了InetAddress.getByName() 靜態方法來查找IP。
該類也提供了其他靜態方法, 但一般不怎麼使用, 有興趣可以點開源碼看看。
然後, 我們通過main() 方法進行簡單的測試:
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); }執行結果為:
host=www.cncounter.comport=80address=198.11.179.83
知道IP和端口號, 我們就可以直接通過Socket 來進行連接了。
當然, 如果是http 協議, 可以使用Apache 的HttpClient 工具, 功能強大而且使用方便。 但這個庫有個不好的地方在於,各個版本之間並不兼容, API 也經常換, 編程時需要根據特定版本號來進行處理。
完整的代碼如下所示:
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, 請根據具體情況進行適當的封裝和處理。
總結
以上所述是小編給大家介紹的Java 根據網址查詢DNS/IP地址的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!