Java examples for Network:URL Download
Whois Query
import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.SocketChannel; import java.util.regex.Pattern; public class WhoisQuery { public static void main(String[] args) { if (args.length < 1) { System.err.println("Usage: java WhoisQuery [port] host..."); return; }/*from w w w. j av a2 s . c o m*/ int port = 43; int firstArg = 0; if (Pattern.matches("[0-9]+", args[0])) { port = Integer.parseInt(args[0]); ++firstArg; } for (int i = firstArg; i < args.length; i++) { String host = args[i]; try { query(host, port); } catch (IOException e) { System.err.println(host + ": " + port + "=" + e); } } } private static void query(String host, int port) throws IOException { InetAddress i = InetAddress.getByName("whois.internic.net"); InetSocketAddress isa = new InetSocketAddress(i, port); ByteBuffer buf = ByteBuffer.allocateDirect(2048); try (SocketChannel sc = SocketChannel.open()) { sc.connect(isa); buf.clear(); buf.put(host.getBytes()); buf.putChar('\r'); buf.putChar('\n'); buf.flip(); sc.write(buf); buf.clear(); while (sc.read(buf) > -1) { buf.flip(); byte[] receivedData = new byte[buf.remaining()]; buf.get(receivedData); System.out.println(new String(receivedData)); buf.clear(); } } finally { } } }