激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java 根據(jù)網(wǎng)址查詢DNS/IP地址的方法

Java 根據(jù)網(wǎng)址查詢DNS/IP地址的方法

2021-02-28 10:25鐵錨 Java教程

這篇文章主要介紹了Java 根據(jù)網(wǎng)址查詢DNS/IP地址的方法,具體實現(xiàn)代碼,大家參考下本文

需求: 給定一個URL地址, 例如: http://www.cncounter.com/tools/shorturl.php, 解析對應的IP地址和端口號。

說明: 本文不涉及底層的 DNS 協(xié)議, 直接使用Java平臺提供的API進行操作。

DNS也就是 Domain Name Service,即 域名服務。

我們知道, Java中與網(wǎng)址有關的類包括 java.net.URL 和 java.net.URI 等, 其中 URI 是資源定位符, 可能包括 file: 之類的協(xié)議。

所以此處我們使用 URL 類, 獲取端口號的代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
  * 獲取端口號
  *
  * @param href 網(wǎng)址, 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) {
     // 獲取對應協(xié)議的默認端口號
     port = url.getDefaultPort();
   }
   return port;
 }

URL 類是Java早期就存在的一個類。 內(nèi)部邏輯比較復雜, 有興趣可以自己查看相關的JDK實現(xiàn)代碼。

其中獲取端口號的2個方法:

getPort() 就是獲取網(wǎng)址里面指明的端口號, 如果沒有指定, 則返回 -1。

getDefaultPort() 是獲取協(xié)議對應的默認端口號, 如 http 協(xié)議默認端口號為 80, https 協(xié)議默認端口號是 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ì)上, 也可以通過正則表達式或者String直接截取 Host, 但如果碰上復雜情況, 也不好處理, 例如: https://yourname:[email protected]/mumu-osc/NiceFish.git 這樣的復雜網(wǎng)址。

提取出域名之后, 可以通過 java.net.InetAddress 類來查找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)方法來查找IP。

該類也提供了其他靜態(tài)方法, 但一般不怎么使用, 有興趣可以點開源碼看看。

然后, 我們通過 main() 方法進行簡單的測試:

?
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";
   // 端口號
   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和端口號, 我們就可以直接通過 Socket 來進行連接了。

當然, 如果是 http 協(xié)議, 可以使用 Apache 的 HttpClient 工具, 功能強大而且使用方便。 但這個庫有個不好的地方在于,各個版本之間并不兼容, API 也經(jīng)常換, 編程時需要根據(jù)特定版本號來進行處理。

完整的代碼如下所示:

?
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";
    // 端口號
    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 網(wǎng)址, 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) {
      // 獲取對應協(xié)議的默認端口號
      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, 請根據(jù)具體情況進行適當?shù)姆庋b和處理。

總結(jié)

以上所述是小編給大家介紹的Java 根據(jù)網(wǎng)址查詢DNS/IP地址的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網(wǎng)站的支持!

原文鏈接:http://blog.csdn.net/renfufei/article/details/78722127

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本特级a一片免费观看 | 国产亚洲欧美日韩在线观看不卡 | 一边吃奶一边摸下娇喘 | 法国极品成人h版 | 中文字幕免费看 | 国产69精品久久久久9999不卡免费 | 国产电影精品久久 | 久久综合久久精品 | 激情97| 99精品欧美一区二区 | 久久精品国产99久久久古代 | 中文字幕欧美专区 | 99久久久精品 | 美女视频黄视大全视频免费网址 | 九九热视频在线免费观看 | 亚洲国产精品一区二区久久 | 91精品国产91 | 亚洲亚色| 国产精品成人一区二区三区吃奶 | jizzzxxxxhd | hd欧美free性xxxx护土 | 欧美大屁股精品毛片视频 | 欧美一极视频 | 欧美韩国一区 | 最新国产毛片 | 国产羞羞视频在线免费观看 | 国产成年人网站 | 日朝毛片 | 毛片av网址 | xxxx18韩国护士hd老师 | 精品一区二区电影 | 日韩精品中文字幕在线播放 | 又黄又爽免费无遮挡在线观看 | a黄网站 | 日本黄色一级电影 | 免费色片 | 九九热视频免费 | 美国一级毛片片aa久久综合 | 欧美a在线播放 | 久久久久久久久久久亚洲 | 日本大片在线播放 |