I want to get whois information of a domain name from my c#/java programs. Is there a simple way to do this?
7 Answers
I found a perfect C# example on dotnet-snippets.com (which doesn't exist anymore).
It's 11 lines of code to copy and paste straight into your own application.
/// <summary>
/// Gets the whois information.
/// </summary>
/// <param name="whoisServer">The whois server.</param>
/// <param name="url">The URL.</param>
/// <returns></returns>
private string GetWhoisInformation(string whoisServer, string url)
{
StringBuilder stringBuilderResult = new StringBuilder();
TcpClient tcpClinetWhois = new TcpClient(whoisServer, 43);
NetworkStream networkStreamWhois = tcpClinetWhois.GetStream();
BufferedStream bufferedStreamWhois = new BufferedStream(networkStreamWhois);
StreamWriter streamWriter = new StreamWriter(bufferedStreamWhois);
streamWriter.WriteLine(url);
streamWriter.Flush();
StreamReader streamReaderReceive = new StreamReader(bufferedStreamWhois);
while (!streamReaderReceive.EndOfStream)
stringBuilderResult.AppendLine(streamReaderReceive.ReadLine());
return stringBuilderResult.ToString();
}

- 44,254
- 30
- 139
- 205
-
1Adding, that I had to add `using System.Net.Sockets; using System.IO;` to complete the code... – KingsInnerSoul Jul 07 '16 at 02:43
-
1TcpClient, NetworkStream, BufferedStream, StreamWriter & StreamReader all need to be disposed of after use. – Paul Williams Jun 07 '21 at 13:33
I think, the easiest way is a socket connection to a whois server on port 43. Send the domainname followed by a newline and read the response.

- 999
- 8
- 14
-
You need to determine *which* whois server to use first. There's an example of that here: here's a good example: http://flipbit.co.uk/2009/06/querying-whois-server-data-with-c.html – Colin Pickard Jun 17 '14 at 13:05
-
'All requests are terminated with ASCII CR and then ASCII LF.' https://tools.ietf.org/html/rfc3912 2. Protocol Specification – brewmanz Apr 11 '17 at 03:31
Thomas' answer will only work if you know which "whois" server to connect to.
There are many different ways of finding that out, but none (AFAIK) that works uniformly for every domain registry.
Some domain names support an SRV
record for the _nicname._tcp
service in the DNS, but there are issues with that because there's no accepted standard yet on how to prevent a subdomain from serving up SRV
records which override those of the official registry (see https://datatracker.ietf.org/doc/html/draft-sanz-whois-srv-00).
For many TLDs it's possible to send your query to <tld>.whois-servers.net
. This actually works quite well, but beware that it won't work in all cases where there are officially delegated second level domains.
For example in .uk
there are several official sub-domains, but only some of them are run by the .uk
registry and the others have their own WHOIS services and those aren't in the whois-servers.net
database.
Confusingly there are also "unofficial" registries, such as .uk.com
, which are in the whois-servers.net
database.
p.s. the official End-Of-Line delimiter in WHOIS, as with most IETF protocols is CRLF
, not just LF
.
I found a perfect C# example here. It's 11 lines of code to copy and paste straight into your own application. BUT FIRST you should add some using statements to ensure that the dispose methods are properly called to prevent memory leaks:
StringBuilder stringBuilderResult = new StringBuilder();
using(TcpClient tcpClinetWhois = new TcpClient(whoIsServer, 43))
{
using(NetworkStream networkStreamWhois = tcpClinetWhois.GetStream())
{
using(BufferedStream bufferedStreamWhois = new BufferedStream(networkStreamWhois))
{
using(StreamWriter streamWriter = new StreamWriter(bufferedStreamWhois))
{
streamWriter.WriteLine(url);
streamWriter.Flush();
using (StreamReader streamReaderReceive = new StreamReader(bufferedStreamWhois))
{
while (!streamReaderReceive.EndOfStream) stringBuilderResult.AppendLine(streamReaderReceive.ReadLine());
}
}
}
}
}

- 5,069
- 7
- 37
- 47

- 21
- 1
-
Thanks for posting this answer. I have edited your post to fixed some code formatting issues. – Brian Apr 23 '13 at 21:48
-
1Did you actually run the code? I got a "Cannot access a closed stream" exception when running this code. If I did not put a using before StreamReader streamReaderReceive = new StreamReader(bufferedStreamWhois), I got no exception. – Silent Sojourner Jan 19 '16 at 21:17
I found some web services that offer this information. This one is free and worked great for me. http://www.webservicex.net/whois.asmx?op=GetWhoIS

- 5,913
- 2
- 28
- 30
-
Looked promising but I get: System.Net.Sockets.SocketException: The requested name is valid, but no data of the requested type was found at whois.whois.GetWhoIS(String HostName) (even when entering an ip address in the field on the info page itself) – Jimmy Mar 25 '14 at 14:13
if you add leaveOpen: true
to the StreamWriter
and StreamReader
constructors. You will not get "Cannot access a closed stream" exception
var stringBuilderResult = new StringBuilder();
using (var tcpClinetWhois = new TcpClient(whoIsServer, 43))
using (var networkStreamWhois = tcpClinetWhois.GetStream())
using (var bufferedStreamWhois = new BufferedStream(networkStreamWhois))
using (var streamWriter = new StreamWriter(networkStreamWhois, leaveOpen: true))
{
streamWriter.WriteLine(url);
streamWriter.Flush();
using (var streamReaderReceive = new StreamReader(networkStreamWhois, leaveOpen: true))
{
while (!streamReaderReceive.EndOfStream)
{
stringBuilderResult.AppendLine(streamReaderReceive.ReadLine());
}
}
}

- 13
- 3
Here's the Java solution, which just opens up a shell and runs whois
:
import java.io.*;
import java.util.*;
public class ExecTest2 {
public static void main(String[] args) throws IOException {
Process result = Runtime.getRuntime().exec("whois stackoverflow.com");
BufferedReader output = new BufferedReader(new InputStreamReader(result.getInputStream()));
StringBuffer outputSB = new StringBuffer(40000);
String s = null;
while ((s = output.readLine()) != null) {
outputSB.append(s + "\n");
System.out.println(s);
}
String whoisStr = outputSB.toString();
}
}

- 2,813
- 2
- 25
- 43

- 87,773
- 37
- 126
- 127
-
Do not shell out just to run a whois command this will create an endless stream of security and performance problems. Use instead the libraries inside your programming language to do whois queries or since the protocol is so simple just open a TCP socket to port 43 and send your query. Read RFC3912 for details. – Patrick Mevzek Jan 08 '18 at 17:22