C# 如何在我的程序中获取域名的whois信息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 10:07:34  来源:igfitidea点击:

How to get whois information of a domain name in my program?

提问by Niyaz

I want to get whois information of a domain name from my c#/java programs. Is there a simple way to do this?

我想从我的 c#/java 程序中获取域名的 whois 信息。有没有一种简单的方法可以做到这一点?

采纳答案by Thomas

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.

我认为,最简单的方法是在端口 43 上建立到 whois 服务器的套接字连接。发送域名后跟一个换行符并读取响应。

回答by Chris Bunch

Here's the Java solution, which just opens up a shell and runs whois:

这是 Java 解决方案,它只打开一个 shell 并运行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 = output.toString();
    }
}

回答by Alnitak

Thomas' answer will only work if you know which"whois" server to connect to.

托马斯的回答只有在您知道要连接到哪个“whois”服务器时才有效。

There are many different ways of finding that out, but none (AFAIK) that works uniformly for every domain registry.

有许多不同的方法可以找出这一点,但没有一种方法 (AFAIK) 对每个域注册机构都有效。

Some domain names support an SRVrecord for the _nicname._tcpservice 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 SRVrecords which override those of the official registry (see http://tools.ietf.org/html/draft-sanz-whois-srv-00).

一些域名支持DNS 中SRV_nicname._tcp服务记录,但存在问题,因为目前还没有公认的标准来说明如何防止子域提供SRV覆盖官方注册中心记录的记录(请参阅http://tools.htm )。 ietf.org/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.

对于许多 TLD,可以将您的查询发送至<tld>.whois-servers.net. 这实际上工作得很好,但请注意,它不适用于官方授权的二级域的所有情况。

For example in .ukthere are several official sub-domains, but only some of them are run by the .ukregistry and the others have their own WHOIS services and those aren't in the whois-servers.netdatabase.

例如,.uk有几个官方子域,但只有其中一些由.uk注册管理机构运行,而其他子域有自己的 WHOIS 服务,而那些不在whois-servers.net数据库中。

Confusingly there are also "unofficial" registries, such as .uk.com, which arein the whois-servers.netdatabase.

令人困惑的是也有“非官方”的注册,比如.uk.com,它whois-servers.net数据库中。

p.s. the official End-Of-Line delimiter in WHOIS, as with most IETF protocols is CRLF, not just LF.

ps WHOIS 中的官方行尾分隔符,与大多数 IETF 协议一样,是CRLF,而不仅仅是LF.

回答by Jeff

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

我发现了一些提供此信息的网络服务。这个是免费的,对我来说效果很好。 http://www.webservicex.net/whois.asmx?op=GetWhoIS

回答by Andrew Shepherd

I found a perfect C# example on dotnet-snippets.com (which doesn't exist anymore).

我在 dotnet-snippets.com 上找到了一个完美的 C# 示例(已经不存在了)。

It's 11 lines of code to copy and paste straight into your own application.

只需 11 行代码即可将其直接复制并粘贴到您自己的应用程序中。

/// <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();
}

回答by user2313093

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:

我在这里找到了一个完美的 C# 示例。只需 11 行代码即可将其直接复制并粘贴到您自己的应用程序中。但首先,您应该添加一些 using 语句以确保正确调用 dispose 方法以防止内存泄漏:

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());
            }
         }
      }
   }
}