需求: 給定一個(gè)URL地址, 例如: http://www.cncounter.com/tools/shorturl.php, 解析對(duì)應(yīng)的IP地址和端口號(hào)。
說(shuō)明: 本文不涉及底層的 DNS 協(xié)議, 直接使用Java平臺(tái)提供的API進(jìn)行操作。
DNS也就是 Domain Name Service,即 域名服務(wù)。
我們知道, Java中與網(wǎng)址有關(guān)的類包括 java.net.URL 和 java.net.URI 等, 其中 URI 是資源定位符, 可能包括 file: 之類的協(xié)議。
所以此處我們使用 URL 類, 獲取端口號(hào)的代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
/** * 獲取端口號(hào) * * @param href 網(wǎng)址, ftp, http, nntp, ... 等等 * @return * @throws IOException */ public static int parsePort(String href) throws IOException { // URL url = new URL(href); // 端口號(hào); 如果 href 中沒(méi)有明確指定則為 -1 int port = url.getPort(); if (port < 0 ) { // 獲取對(duì)應(yīng)協(xié)議的默認(rèn)端口號(hào) port = url.getDefaultPort(); } return port; } |
URL 類是Java早期就存在的一個(gè)類。 內(nèi)部邏輯比較復(fù)雜, 有興趣可以自己查看相關(guān)的JDK實(shí)現(xiàn)代碼。
其中獲取端口號(hào)的2個(gè)方法:
getPort() 就是獲取網(wǎng)址里面指明的端口號(hào), 如果沒(méi)有指定, 則返回 -1。
getDefaultPort() 是獲取協(xié)議對(duì)應(yīng)的默認(rèn)端口號(hào), 如 http 協(xié)議默認(rèn)端口號(hào)為 80, https 協(xié)議默認(rèn)端口號(hào)是 443 等。
然后我們看提取 Host 部分的代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/** * 獲取Host部分 * * @param href 網(wǎng)址, 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; } |
本質(zhì)上, 也可以通過(guò)正則表達(dá)式或者String直接截取 Host, 但如果碰上復(fù)雜情況, 也不好處理, 例如: https://yourname:[email protected]/mumu-osc/NiceFish.git 這樣的復(fù)雜網(wǎng)址。
提取出域名之后, 可以通過(guò) java.net.InetAddress 類來(lái)查找IP地址。
代碼如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/** * 根據(jù)域名(host)解析IP地址 * * @param host 域名 * @return * @throws IOException */ public static String parseIp(String host) throws IOException { // 根據(jù)域名查找IP地址 InetAddress inetAddress = InetAddress.getByName(host); // IP 地址 String address = inetAddress.getHostAddress(); return address; } |
可以看到,我們使用了 InetAddress.getByName() 靜態(tài)方法來(lái)查找IP。
該類也提供了其他靜態(tài)方法, 但一般不怎么使用, 有興趣可以點(diǎn)開(kāi)源碼看看。
然后, 我們通過(guò) main() 方法進(jìn)行簡(jiǎn)單的測(cè)試:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public static void main(String[] args) throws IOException { // String href = "http://www.cncounter.com/tools/shorturl.php" ; // 端口號(hào) 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); } |
執(zhí)行結(jié)果為:
1
2
3
|
host=www.cncounter.com port= 80 address= 198.11 . 179.83 |
知道IP和端口號(hào), 我們就可以直接通過(guò) Socket 來(lái)進(jìn)行連接了。
當(dāng)然, 如果是 http 協(xié)議, 可以使用 Apache 的 HttpClient 工具, 功能強(qiáng)大而且使用方便。 但這個(gè)庫(kù)有個(gè)不好的地方在于,各個(gè)版本之間并不兼容, API 也經(jīng)常換, 編程時(shí)需要根據(jù)特定版本號(hào)來(lái)進(jìn)行處理。
完整的代碼如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
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" ; // 端口號(hào) 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); } /** * 獲取端口號(hào) * * @param href 網(wǎng)址, ftp, http, nntp, ... 等等 * @return * @throws IOException */ public static int parsePort(String href) throws IOException { // URL url = new URL(href); // 端口號(hào); 如果 href 中沒(méi)有明確指定則為 -1 int port = url.getPort(); if (port < 0 ) { // 獲取對(duì)應(yīng)協(xié)議的默認(rèn)端口號(hào) port = url.getDefaultPort(); } return port; } /** * 獲取Host部分 * * @param href 網(wǎng)址, 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; } /** * 根據(jù)域名(host)解析IP地址 * * @param host 域名 * @return * @throws IOException */ public static String parseIp(String host) throws IOException { // 根據(jù)域名查找IP地址 InetAddress.getAllByName(host); InetAddress inetAddress = InetAddress.getByName(host); // IP 地址 String address = inetAddress.getHostAddress(); return address; } } |
OK, 請(qǐng)根據(jù)具體情況進(jìn)行適當(dāng)?shù)姆庋b和處理。
總結(jié)
以上所述是小編給大家介紹的Java 根據(jù)網(wǎng)址查詢DNS/IP地址的方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!
原文鏈接:http://blog.csdn.net/renfufei/article/details/78722127