Java如何實現簡單的whois查詢
利用Java socket 實現根據IP地址查詢該IP相關的'註冊信息。
源代碼直接下載:java_
相關的兩個類 和 代碼實現如下:
Java代碼
import edHashMap;
import ;
import ;
import y;
/**
*
* @author Michael sun
*/
public class WhoisQuery {
/**
*
* @param ip
*/
private void queryIpInfo(String ip) {
Map map = new LinkedHashMap();
try {
WhoisBean bean = new WhoisBean();
imeout(0);
// erver("");
erver("");
yInfoByIp(ip);
List infoList = nfoList();
String vALue = "";
String key = "";
for (String infoTmp : infoList) {
String[] arr = t(":[ ]*");
if (th > 1) {
key = arr[0];
value = arr[1];
} else {
value = arr[0]();
}
if (null == (key)) {
(key, "");
}
value = (key) + value;
(key, value);
}
} catch (Exception e) {
tStackTrace();
}
for (Entry entry : ySet()) {
tln(ey() + ":" + alue());
}
}
/**
* @param args
*/
public static void main(String[] args) {
String ip = "";// "";
WhoisQuery query = new WhoisQuery();
yIpInfo(ip);
}
}
Java代碼
import eredReader;
import tStreamReader;
import tStream;
import et;
import yList;
import ;
/**
* WhoisBean
* @author Michael sun
*/
public class WhoisBean {
/**
* server address
*/
private String server = "";
/**
* port
*/
private int port = 43;
/**
* timeout/minute
*/
private int timeout = 0;
/**
* infoList
*/
private List infoList = new ArrayList();
/**
* @param ip
* @throws Exception
*/
@SuppressWarnings("unchecked")
public void queryInfoByIp(String ip) throws Exception {
Socket theSocket = null;
BufferedReader br = null;
PrintStream ps = null;
int qryCount = 0;
while (qryCount < 5) {
qryCount++;
try {
theSocket = new Socket(server, port);
oTimeout(timeout * 1000);
ps = new PrintStream(utputStream());
tln(ip);
br = new BufferedReader(new InputStreamReader(theSocket
nputStream()));
("ip:" + ip);
String readLine = "";
int i = 0;
tln("Whois query read start.... ");
while ((readLine = Line()) != null) {
tln("***" + readLine);
if (th() > 0 && At(0) != '%') {
(readLine);
i++;
// 默認讀取100行數據
if (i > 100 || tsWith("source")) {
break;
}
}
}
tln("querylist size:" + ());
break;
} catch (Exception e) {
tln("EXCEPTION : " + e);
} finally {
if (null != br) {
e();
}
if (null != ps) {
e();
}
if (null != theSocket) {
e();
}
}
}
}
/**
* @return the server
*/
public String getServer() {
return server;
}
/**
* @return the port
*/
public int getPort() {
return port;
}
/**
* @return the timeout
*/
public int getTimeout() {
return timeout;
}
/**
* @param pServer the server to set
*/
public void setServer(String pServer) {
server = pServer;
}
/**
* @param pPort the port to set
*/
public void setPort(int pPort) {
port = pPort;
}
/**
* @param pTimeout the timeout to set
*/
public void setTimeout(int pTimeout) {
timeout = pTimeout;
}
/**
* @return the infoList
*/
public List getInfoList() {
return infoList;
}
/**
* @param pInfoList the infoList to set
*/
public void setInfoList(List pInfoList) {
infoList = pInfoList;
}
}
運行WhoisQuery這個類就可以看到如下信息:
Java代碼
Whois query read start....
***% This is the RIPE Database query service.
***% The objects are in RPSL format.
***%
***% The RIPE Database is subject to Terms and Conditions.
***% See
***
***% Note: This output has been filtered.
***% To receive output for a database update, use the "-B" flag.
***
***% Information related to ' - '
***
***inetnum: -
***netname: EU-ZZ-129
***descr: Various Registries
***country: EU # Country is really world wide
***remarks: These addresses were issued by
*** The IANA before the formation of
*** Regional Internet Registries.
***
***org: ORG-NCC1-RIPE
***admin-c: iana1-RIPE
***tech-c: iana1-RIPE
***status: ALLOCATED UNSPECIFIED
***mnt-by: RIPE-NCC-HM-MNT
***mnt-lower: RIPE-NCC-HM-MNT
***mnt-routes: RIPE-NCC-RPSL-MNT
***source: RIPE # Filtered
querylist size:17
ip:
inetnum: -
netname:EU-ZZ-129
descr:Various Registries
country:EU # Country is really world wide
remarks:These addresses were issued byThe IANA before the formation ofRegional Internet Registries.
org:ORG-NCC1-RIPE
admin-c:iana1-RIPE
tech-c:iana1-RIPE
status:ALLOCATED UNSPECIFIED
mnt-by:RIPE-NCC-HM-MNT
mnt-lower:RIPE-NCC-HM-MNT
mnt-routes:RIPE-NCC-RPSL-MNT
source:RIPE # Filtered
-
Sun Java認證考試科目
SunJava認證SunCertifiedAssociatefortheJavaPlatform,StandardEdition,ExamVersion1.0(310-019)SunCertifiedProgrammerforJava2Platform1.2(310-025)SunCertifiedDeveloperforJava2Platform(310-252A、310-027)SunCertifiedProgrammerforJava2Platform1.4(31...
-
2017年SUN-Java程序員認證考試試題
JAVA程序員廣義上是指一羣以JAVA為謀生手段的軟件開發人員。狹義的説,是指擁有SUN公司JAVA認證的程序員。下面是小編收集的SUN_Java程序員認證考試試題,希望大家認真練習!1.Java中的異常處理機制的簡單和應用。當Java程序違反了Java的語義規則時,Java虛擬機就會將...
-
sun java認證考試介紹
在當今信息高速發展的社會中,人才決定了企業發展。因此,如何評估員工的價值,或是個人如何證明自己的專業能力,就成為關鍵。Sun公司為了為業界建立一套認證的標準,特別針對最先進的科技,推出Java及Solaris技術認證方案。根據這些認證,在企業方面可以藉此作為招聘人才的...
-
SUN認證考試知識點:Java反射機制的應用
目標:用一個代理類實現兩個相似類的調用1.兩個相似類Man類Women類:代理類:packageReflect;importod;publicclassPerson{//代理類privateObjecttarget;//目標對象privateStringmethodName;//目標方法privateObject[]params;//參數數組privateMethodmethod;publicOb...