WHOIS協(xié)議原理
Whois 協(xié)議原理及使用RFC812定義了一個(gè)非常簡(jiǎn)單的Internet 信息查詢協(xié)議——WHOIS 協(xié)議。其基本內(nèi)容是,先向服務(wù)器的TCP 端口43建立一個(gè)連接,發(fā)送查詢關(guān)鍵字并加上回車換行,然后
Whois 協(xié)議原理及使用
RFC812定義了一個(gè)非常簡(jiǎn)單的Internet 信息查詢協(xié)議——WHOIS 協(xié)議。其基本內(nèi)容是,先向服務(wù)器的TCP 端口43建立一個(gè)連接,發(fā)送查詢關(guān)鍵字并加上回車換行,然后接收服務(wù)器的查詢結(jié)果。
世界上各級(jí)Internet 管理機(jī)構(gòu)秉承公開(kāi)、公正、共享的原則,設(shè)立了可以查知IP 地址和域名所有者登記資料的WHOIS 服務(wù)器,以便所有Internet 的使用者排除故障、打擊網(wǎng)上非法活動(dòng)。全世界國(guó)際區(qū)域性的IP 地址管理機(jī)構(gòu)有四個(gè):ARIN 、RIPE 、APNIC 、LACNIC ,他們負(fù)責(zé)的IP 地址的地理區(qū)域如下圖所示。
四個(gè)國(guó)際區(qū)域性IP 地址管理機(jī)構(gòu)所負(fù)責(zé)的區(qū)域
(此圖摘自《RIPE 2002年度報(bào)告》)
重要的Internet 管理機(jī)構(gòu)和常用的WHOIS 服務(wù)器
機(jī)構(gòu)縮寫(xiě) WHOIS 服務(wù)器地址 機(jī)構(gòu)全名及地點(diǎn) 提供查詢內(nèi)容
CERNIC whois.edu.cn 中國(guó)教育與科研計(jì)算機(jī)網(wǎng)網(wǎng)絡(luò)信息中心
(清華大學(xué)·中國(guó)北京) 中國(guó)教育網(wǎng)內(nèi)的IP 地址和.edu.cn 域名信息
CNNIC whois.cnnic.net.cn 中國(guó)互聯(lián)網(wǎng)絡(luò)信息中心
(中國(guó)科學(xué)院計(jì)算機(jī)網(wǎng)絡(luò)信息中心·中國(guó)北京) .cn 域名(除.edu.cn) 信息
INTERNIC whois.internic.net 互聯(lián)網(wǎng)絡(luò)信息中心
(美國(guó)洛杉磯市Marina del Rey鎮(zhèn)) .com,.net,.org,.biz,.info,.name
域名的注冊(cè)信息(只給出注冊(cè)代理公司)
ARIN whois.arin.net 美國(guó)Internet 號(hào)碼注冊(cè)中心
(美國(guó)弗吉尼亞州Chantilly 市) 全世界早期網(wǎng)絡(luò)及現(xiàn)在的美國(guó)、加拿大、撒哈拉沙漠以南非洲的IP 地址信息
APNIC whois.apnic.net 亞洲與太平洋地區(qū)網(wǎng)絡(luò)信息中心
(澳大利亞昆士蘭州密爾頓鎮(zhèn)) 東亞(包括中國(guó)大陸和臺(tái)灣) 、南亞、大洋洲IP 地址注信息 RIPE whois.ripe.net 歐州IP 地址注冊(cè)中心(荷蘭阿姆斯特丹) 歐洲、北非、西亞地區(qū)的IP 地址信息
TWNIC whois.twnic.net 臺(tái)灣互聯(lián)網(wǎng)絡(luò)信息中心(中國(guó)臺(tái)灣臺(tái)北) .tw 域名和部分臺(tái)灣島內(nèi)IP 地址信息
JPNIC whois.nic.ad.jp 日本互聯(lián)網(wǎng)絡(luò)信息中心(日本東京) .jp 域名和日本境內(nèi)的IP 地址信息
www.jianfeidongli.com
,KRNIC whois.krnic.net 韓國(guó)互聯(lián)網(wǎng)絡(luò)信息中心(韓國(guó)漢城) .kr 域名和韓國(guó)境內(nèi)的IP 地址信息
LACNIC whois.lacnic.net 拉丁美洲及加勒比互聯(lián)網(wǎng)絡(luò)信息中心(巴西圣保羅) 拉丁美洲及加勒比海諸島IP 地址信息
本機(jī)上的自動(dòng)WHOIS 服務(wù),是按照下圖所示的流程,依次查詢?nèi)舾蓚€(gè)WHOIS 服務(wù)器之后,得到某個(gè)IP 地址的WHOIS 信息。
當(dāng)我們準(zhǔn)備建立一個(gè)Web 站點(diǎn),就必須向域名登記機(jī)構(gòu)申請(qǐng)一個(gè)Internet 域名,因此,我們通常希望了解自己準(zhǔn)備使用的域名是否已經(jīng)被注冊(cè),這時(shí),可以簡(jiǎn)單地訪問(wèn)InterNIC 站點(diǎn)http://www.internic.net/whois.html,在"Registry Whois" 輸入框中輸入需查詢的域名,就可以得到我們需要的結(jié)果。本文介紹了如何使用Java 編程來(lái)實(shí)現(xiàn)這個(gè)過(guò)程。
一 原理
原理非常簡(jiǎn)單,域名的查詢主要是基于RFC 954提供的WHOIS 協(xié)議。在上述過(guò)程中,我們實(shí)際上是訪問(wèn)了InterNIC 站點(diǎn)的WHOIS 服務(wù)器,該服務(wù)器從WHOIS 數(shù)據(jù)庫(kù)中查詢我們所需要的內(nèi)容。
WHOIS 服務(wù)器是一個(gè)基于" 查詢/響應(yīng)" 的TCP 事務(wù)服務(wù)器,它運(yùn)行在SRI-NIC 機(jī)器上(26.0.0.73或10.0.0.51) ,向用戶提供internet 范圍內(nèi)的目錄服務(wù)。本地主機(jī)上的用戶程序可以通過(guò)Internet 訪問(wèn)該服務(wù)器,其過(guò)程主要有下面三步:
(1)在TCP 服務(wù)端口43(十進(jìn)制) 連接SRI-NIC 服務(wù)主機(jī);
(2)發(fā)送一個(gè)命令,以回車和換行()結(jié)尾;
(3)接受相應(yīng)命令的返回信息,一旦輸出結(jié)束,服務(wù)器將關(guān)閉連接。
命令的格式非常簡(jiǎn)單??梢灾苯虞斎胗蛎纾梢允褂?sohu.com" 查詢" 搜狐" 網(wǎng)站的域名信息;也可以使用"help" 得到詳細(xì)的幫助信息。
二 Java socket編程簡(jiǎn)述
在Java 中,使用Socket 類可以實(shí)現(xiàn)客戶端的sockets ,建立與服務(wù)器的網(wǎng)絡(luò)連接。本文www.jianfeidongli.com
,使用下面所示的Socket 類的構(gòu)造器來(lái)創(chuàng)建一個(gè)流socket ,并連接到主機(jī)"whois.internic.net" 的端口43。
public Socket(String host, int port, boolean stream) throws IOException
其中,參數(shù)host 為遠(yuǎn)程主機(jī)的主機(jī)名,port 為遠(yuǎn)程主機(jī)的端口號(hào),如果參數(shù)stream 為true ,則創(chuàng)建一個(gè)流socket ,否則創(chuàng)建一個(gè)數(shù)據(jù)報(bào)socket 。
如果創(chuàng)建socket 時(shí)發(fā)生I/O錯(cuò)誤,將拋擲一個(gè)IOException 異常。
當(dāng)創(chuàng)建了一個(gè)連接到遠(yuǎn)程主機(jī)的socket 對(duì)象后,我們可以使用getInputStream()和getOutputStream()方法分別得到該socket 對(duì)象的輸入流和輸出流,用于對(duì)該socket 進(jìn)行數(shù)據(jù)讀寫(xiě),為了使應(yīng)用程序設(shè)計(jì)簡(jiǎn)單,這些方法返回的流通常使用java.io 包中的實(shí)例對(duì)象來(lái)處理,例如:DataInputStream 和PrintWriter 。
從socket 讀數(shù)據(jù)使用readline()方法,一次讀取一行數(shù)據(jù)(字符串) :
public String readLine() throws IOException
向socket 寫(xiě)數(shù)據(jù)使用print()方法:
public void print(String s)
當(dāng)完成socket 通訊后,應(yīng)該首先關(guān)閉DataInputStream 和PrintWriter 對(duì)象,最后才關(guān)閉socket 對(duì)象。
三 源程序
import java.net.*;
import java.io.*;
public class whois
{
public final static int port = 43;
public final static String hostname = "whois.internic.net";
public static void main(String[] args)
{
Socket theSocket;
DataInputStream theWhoisStream;
PrintStream ps;
//檢查命令行參數(shù)
if (args.length <1)
{
System.out.println("nUsage: java whois ");
System.out.println("Parameters:");
www.jianfeidongli.com
,System.out.println(
"tcommand = one or more Domain name, or other command."); System.out.println("Example:");
System.out.println("tjava whois sohu.com");
System.out.println("tjava whois help");
System.exit(1); //退出
}
try {
//在TCP 服務(wù)端口43(十進(jìn)制) 連接SRI-NIC 服務(wù)主機(jī)
theSocket = new Socket(hostname, port, true);
ps = new PrintStream(theSocket.getOutputStream());
//發(fā)送用戶提供的一個(gè)或多個(gè)命令
for (int i = 0; i < args.length; i )
ps.print(args[i] " ");
//以回車和換行()結(jié)尾
ps.print("rn");
//接受相應(yīng)命令的返回信息
theWhoisStream = new DataInputStream(theSocket.getInputStream()); String s;
while ((s = theWhoisStream.readLine()) != null) {
System.out.println(s);
}
//關(guān)閉DataInputStream 和PrintWriter
theWhoisStream.close();
ps.close();
//關(guān)閉socket
theSocket.close();
}
catch (IOException e) {
System.err.println(e);
}
}
}
運(yùn)行:java whois sina.com
運(yùn)行結(jié)果:
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.
www.jianfeidongli.com
,SINA.COM.CN
SINA.COM
To single out one record, look it up with "xxx", where xxx is one of the of the records displayed above. If the records are the same, look them up with "=xxx" to receive a full display for each record.
>>> Last update of whois database: Tue, 03 Mar 2009 03:37:55 UTC <<<
NOTICE: The expiration date displayed in this record is the date the registrar's sponsorship of the domain name registration in the registry is currently set to expire. This date does not necessarily reflect the expiration date of the domain name registrant's agreement with the sponsoring
registrar. Users may consult the sponsoring registrar's Whois database to view the registrar's reported date of expiration for this registration.
TERMS OF USE: You are not authorized to access or query our Whois database through the use of electronic processes that are high-volume and automated except as reasonably necessary to register domain names or modify existing registrations; the Data in VeriSign Global Registry Services' ("VeriSign") Whois database is provided by VeriSign for
information purposes only, and to assist persons in obtaining information about or related to a domain name registration record. VeriSign does not guarantee its accuracy. By submitting a Whois query, you agree to abide by the following terms of use: You agree that you may use this Data only for lawful purposes and that under no circumstances will you use this Data to: (1) allow, enable, or otherwise support the transmission of mass
unsolicited, commercial advertising or solicitations via e-mail, telephone, or facsimile; or (2) enable high volume, automated, electronic processes that apply to VeriSign (or its computer systems). The compilation, repackaging, dissemination or other use of this Data is expressly
prohibited without the prior written consent of VeriSign. You agree not to use electronic processes that are automated and high-volume to access or query the Whois database except as reasonably necessary to register
domain names or modify existing registrations. VeriSign reserves the right to restrict your access to the Whois database in its sole discretion to ensure operational stability. VeriSign may restrict or terminate your access to the Whois database for failure to abide by these terms of use. VeriSign reserves the right to modify these terms at any time.
The Registry database contains ONLY .COM, .NET, .EDU domains and Registrars.
www.jianfeidongli.com