開発者ドキュメント

Java Whoisの例


whois image、width = 250、height = 250

このチュートリアルでは、ドメインのWHOISデータを取得するためのJavaライブラリ「Apache Commons Net」の使用方法を説明します。

1.単純なJavaのWhoisの例

internic.netに登録されているドメインの場合は、whoisデータを直接取得できます。

WhoisTest.java

package com.mkyong.whois.bo;

import java.io.IOException;
import java.net.SocketException;
import org.apache.commons.net.whois.WhoisClient;

public class WhoisTest {

    public static void main(String[]args) {

        WhoisTest obj = new WhoisTest();
        System.out.println(obj.getWhois("mkyong.com"));
        System.out.println("Done");

    }

    public String getWhois(String domainName) {

        StringBuilder result = new StringBuilder("");

        WhoisClient whois = new WhoisClient();
        try {

           //default is internic.net
            whois.connect(WhoisClient.DEFAULT__HOST);
            String whoisData1 = whois.query("=" + domainName);
            result.append(whoisData1);
            whois.disconnect();

        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result.toString();

    }

}

2.複雑なJava Whoisの例

他のレジストラ(internic.netではなく)に登録されているドメインの場合は、whoisデータを取得するためにいくつかの追加手順が必要です。

$ whois google.com
### It will returns many non related subdomain.google.com data

$ whois =google.com
### Add '=' sign to get the exact result.
### refer to the "Whois server" URL

### Sample data
### Domain Name: GOOGLE.COM
### Registrar: MARKMONITOR INC.
### Whois Server: whois.markmonitor.com
### Referral URL: http://www.markmonitor.com

### The google.com whois is available under whois.markmonitor.com

$ whois -h whois.markmonitor.com google.com
### done, finally, get the whois data of google.com

上記の手順をJavaで実装してみましょう。コメントは自明です。

WhoisTest.java

package com.mkyong.whois.bo;

import java.io.IOException;
import java.net.SocketException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.net.whois.WhoisClient;

public class WhoisTest {

    private static Pattern pattern;
    private Matcher matcher;

   //regex whois parser
    private static final String WHOIS__SERVER__PATTERN = "Whois Server:\\s(.** )";
    static {
        pattern = Pattern.compile(WHOIS__SERVER__PATTERN);
    }

    public static void main(String[]args) {

        WhoisTest obj = new WhoisTest();
        System.out.println(obj.getWhois("google.com"));

        System.out.println("Done");

    }

   //example google.com
    public String getWhois(String domainName) {

        StringBuilder result = new StringBuilder("");

        WhoisClient whois = new WhoisClient();
        try {

          whois.connect(WhoisClient.DEFAULT__HOST);

         //whois =google.com
          String whoisData1 = whois.query("=" + domainName);

         //append first result
          result.append(whoisData1);
          whois.disconnect();

         //get the google.com whois server - whois.markmonitor.com
          String whoisServerUrl = getWhoisServer(whoisData1);
          if (!whoisServerUrl.equals("")) {

           //whois -h whois.markmonitor.com google.com
            String whoisData2 =
                            queryWithWhoisServer(domainName, whoisServerUrl);

           //append 2nd result
            result.append(whoisData2);
          }

        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result.toString();

    }

    private String queryWithWhoisServer(String domainName, String whoisServer) {

        String result = "";
        WhoisClient whois = new WhoisClient();
        try {

            whois.connect(whoisServer);
            result = whois.query(domainName);
            whois.disconnect();

        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;

    }

    private String getWhoisServer(String whois) {

        String result = "";

        matcher = pattern.matcher(whois);

       //get last whois server
        while (matcher.find()) {
            result = matcher.group(1);
        }
        return result;
    }

}

出力

Whois Server Version 2.0

Domain names in the .com and .net domains can now be registered
with many different competing registrars. Go to http://www.internic.net
for detailed information.
//......

Registrant:
        Dns Admin
        Google Inc.
        Please contact contact-admin@google.com 1600 Amphitheatre Parkway
         Mountain View CA 94043
        US
        dns-admin@google.com +1.6502530000 Fax: +1.6506188571

    Domain Name: google.com

        Registrar Name: Markmonitor.com
        Registrar Whois: whois.markmonitor.com
        Registrar Homepage: http://www.markmonitor.com

   //......

    Created on..............: 1997-09-15.
    Expires on..............: 2020-09-13.
    Record last updated on..: 2013-02-28.

   //......
  • Whois parser ** 上では正規表現を使ってwhoisサーバのURLを取得しています。もしあなたがJavaのwhoisパーサを知っているなら、下記にコメントしてください。ありがとう。

モバイルバージョンを終了