gpt4 book ai didi

java - php checkdnsrr java等价物

转载 作者:行者123 更新时间:2023-11-30 11:59:36 26 4
gpt4 key购买 nike

是否有 PHP 函数 checkdnsrr 的 Java 版本?

最佳答案

如果您只需要任何 IP 地址,您可以使用 InetAddress(虽然 API 没有指定这将匹配什么类型的主机):

InetAddress[] addresses = java.net.InetAddress.getAllByName(String host)

然后,您可以对 IP 地址的原始字节使用 getAddress(),对 IP 地址的格式化字符串表示形式使用 getHostAddress()。

如果您正在寻找特定类型的记录(例如 MX 记录),这是我在 this webpage 上找到的片段

import  java.util.Hashtable;
import javax.naming.*;
import javax.naming.directory.*;

public class MXLookup {
public static void main( String args[] ) {
if( args.length == 0 ) {
System.err.println( "Usage: MXLookup host [...]" );
System.exit( 99 );
}
for( int i = 0; i < args.length; i++ ) {
try {
System.out.println( args[i] + " has " +
doLookup( args[i] ) + " mail servers" );
}
catch( Exception e ) {
System.out.println(args[i] + " : " + e.getMessage());
}
}
}

static int doLookup( String hostName ) throws NamingException {
Hashtable env = new Hashtable();
env.put("java.naming.factory.initial",
"com.sun.jndi.dns.DnsContextFactory");
DirContext ictx = new InitialDirContext( env );
Attributes attrs =
ictx.getAttributes( hostName, new String[] { "MX" });
Attribute attr = attrs.get( "MX" );
if( attr == null ) return( 0 );
return( attr.size() );
}
}

关于java - php checkdnsrr java等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2474123/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com