我有这样的代码,我想用来检查域名注册:
private final static String WHO ="cnn.com";
private final static String WHOIS_HOST = "whois.verisign-grs.com";
private final static int WHOIS_PORT = 43;
public static void main(final String[] args) throws IOException {
SpringApplication.run(TestApplication.class, args);
int c;
Socket socket = null;
String query = WHO + "\r\n";
byte buf[] = query.getBytes();
String regex = ".*Registry Expiry Date:*";
try {
socket = new Socket(WHOIS_HOST, WHOIS_PORT);
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
out.write(buf);
out.flush();
StringBuilder text = new StringBuilder();
while ((c = in.read()) != -1) {
System.out.print((char) c);
text.append(c);
}
boolean matches = Pattern.matches(regex, text.toString());
System.out.print("\nDone\n" + matches);
} catch (IOException ex) {
System.out.print(ex.getMessage());
} finally {
if(socket != null){
try {
socket.close();
} catch (IOException ex) {
}
}
}
}
我得到了这个输出:
Domain Name: CNN.COM
Registry Domain ID: 3269879_DOMAIN_COM-VRSN
Registrar WHOIS Server: whois.corporatedomains.com
Registrar URL: http://cscdbs.com
Updated Date: 2018-04-10T16:43:38Z
Creation Date: 1993-09-22T04:00:00Z
Registry Expiry Date: 2026-09-21T04:00:00Z
Registrar: CSC Corporate Domains, Inc.
Registrar IANA ID: 299
Registrar Abuse Contact Email: domainabuse@cscglobal.com
Registrar Abuse Contact Phone: 8887802723
Domain Status: clientTransferProhibited https://icann.org/epp#clientTransferProhibited
Domain Status: serverDeleteProhibited https://icann.org/epp#serverDeleteProhibited
Domain Status: serverTransferProhibited https://icann.org/epp#serverTransferProhibited
Domain Status: serverUpdateProhibited https://icann.org/epp#serverUpdateProhibited
Name Server: NS-1086.AWSDNS-07.ORG
Name Server: NS-1630.AWSDNS-11.CO.UK
Name Server: NS-47.AWSDNS-05.COM
Name Server: NS-576.AWSDNS-08.NET
DNSSEC: unsigned
URL of the ICANN Whois Inaccuracy Complaint Form: https://www.icann.org/wicf/
>>> Last update of whois database: 2022-07-29T20:55:54Z <<<
For more information on Whois status codes, please visit https://icann.org/epp
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.
Done
false
你知道我怎样才能得到行内容Registry Expiry Date: 2026-09-21T04:00:00Z
发布于 2022-07-29 22:27:31
当您将字节值附加到StringBuilder中时,存在一个问题,因此,如果像这样更改,则匹配器无法工作,您可以得到所需正则表达式的值。
StringBuilder text = new StringBuilder();
while ((c = in.read()) != -1) {
// System.out.print((char) c);
text.append((char) c);
}
System.out.println(text);
String regex = ".*Registry Expiry Date.*Z";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text.toString());
if (matcher.find())
{
System.out.println("\nDone\n" + matcher.group(0));
}
在这种情况下,结果将是
Done
Registry Expiry Date: 2026-09-21T04:00:00Z
发布于 2022-07-29 23:19:41
有几件事需要清理:
c
被定义为int,所以当您调用text.append(c)
时,就会追加一个整数值--为了解决这个问题,您可以将"c“转换为一个字符:text.append((char) c)
以下是一个解决方案:
resources
String WHO = "cnn.com";
String WHOIS_HOST = "whois.verisign-grs.com";
int WHOIS_PORT = 43;
try (Socket socket = new Socket(WHOIS_HOST, WHOIS_PORT)) {
try (OutputStream out = socket.getOutputStream()) {
out.write((WHO + "\r\n").getBytes());
out.flush();
try (BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
String line;
while ((line = input.readLine()) != null) {
if (line.contains("Registry Expiry Date")) {
System.out.println("---> " + line);
break; // don't need to read any more input
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
这是输出:
---> Registry Expiry Date: 2026-09-21T04:00:00Z
https://stackoverflow.com/questions/73171051
复制相似问题