Пример Java Whois

Пример Java Whois

whois image

В этом руководстве мы покажем вам, как использовать библиотеку Java - «Apache Commons Net» для получения данных WHOIS домена.

1. Простой пример Java Whois

Для домена, зарегистрированного на сайте internic.net, вы можете получить данные whois напрямую.

WhoisTest.java

package com.example.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("example.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 в * nix и mac.

$ 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.example.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 [email protected] 1600 Amphitheatre Parkway
         Mountain View CA 94043
        US
        [email protected] +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
Выше используется регулярное выражение для получения URL-адреса сервера Whois. Если вы знаете какой-либо парсер Java whois, сделайте комментарий ниже, спасибо.

Рекомендации