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

发布于 2024-07-05 03:22:27 字数 59 浏览 11 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

甜中书 2024-07-12 03:22:27

我在这里找到了一个完美的 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());
            }
         }
      }
   }
}

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());
            }
         }
      }
   }
}
谁对谁错谁最难过 2024-07-12 03:22:27

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

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

英雄似剑 2024-07-12 03:22:27

这是 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 = outputSB.toString();
    }
}

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();
    }
}
鸠书 2024-07-12 03:22:27

如果将 leaveOpen: true 添加到 StreamWriterStreamReader 构造函数中。 您不会收到“无法访问关闭的流”异常

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

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());
        }
    }
}
江南月 2024-07-12 03:22:27

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

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.

请你别敷衍 2024-07-12 03:22:27

仅当您知道要连接到哪个“whois”服务器时,托马斯的答案才有效。

有许多不同的方法可以找到这一点,但没有一种方法(据我所知)对每个域名注册机构都适用。

某些域名支持 DNS 中 _nicname._tcp 服务的 SRV 记录,但这存在问题,因为目前还没有关于如何防止子域被接受的标准提供覆盖官方注册表记录的 SRV 记录(请参阅 https://datatracker.ietf.org/doc/html/draft-sanz-whois-srv-00)。

对于许多 TLD,可以将您的查询发送至 .whois-servers.net。 这实际上工作得很好,但请注意,它并不是在所有有官方委托的二级域名的情况下都工作。

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

令人困惑的是,还有“非官方”注册机构,例如 .uk.com,它们位于 whois-servers.net 数据库中。

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

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.

别念他 2024-07-12 03:22:27

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

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

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文