分享

Java技巧: 根据网址/域名查询DNS/IP地址

 VoidOc 2018-05-31

需求: 给定一个URL地址, 例如: http://www./tools/shorturl.php, 解析对应的IP地址和端口号。

说明: 本文不涉及底层的 DNS 协议, 直接使用Java平台提供的API进行操作。

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

我们知道, Java中与网址有关的类包括 java.net.URLjava.net.URI 等, 其中 URI 是资源定位符, 可能包括 file: 之类的协议。

所以此处我们使用 URL 类, 获取端口号的代码如下:

    
  • 1/**
         * 获取端口号
         *
         * @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;
        }
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

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;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

本质上, 也可以通过正则表达式或者String直接截取 Host, 但如果碰上复杂情况, 也不好处理, 例如: https://yourname:passwd@/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;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

可以看到,我们使用了 InetAddress.getByName() 静态方法来查找IP。

该类也提供了其他静态方法, 但一般不怎么使用, 有兴趣可以点开源码看看。

然后, 我们通过 main() 方法进行简单的测试:

    public static void main(String[] args) throws IOException {
        //
        String href = "http://www./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); 
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

执行结果为:

host=www.
port=80
address=198.11.179.83
  • 1
  • 2
  • 3

知道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./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;
    }
}
  • 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
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

OK, 请根据具体情况进行适当的封装和处理。

日期: 2017年12月05日

作者: 铁锚: http://blog.csdn.net/renfufei

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多