阅读TIdDNSResolver的回应? [英] Reading response from TIdDNSResolver?

查看:259
本文介绍了阅读TIdDNSResolver的回应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到使用Indy 10的 TIdDNSResolver 组件的DNS查找的简单示例。他们都是为了我不需要的东西(比如MX / SMTP),或者说条款,没有代码。我已经尝试阅读结果,基于我可以找到的几个资源,但不知道我应该如何读取结果。

I cannot find any simple examples of a DNS lookup using Indy 10's TIdDNSResolver component. They're all either for something I don't need (such as MX/SMTP), or are talking terms with no code. I have tried reading the result based on the few resources I can find, but don't know how I'm supposed to be reading the result.

这是我有到目前为止...

Here's what I have so far...

uses
  IdBaseComponent, IdComponent, IdTCPConnection, IdDNSResolver;

function TForm1.Lookup(const Name: String): String;
var
  X: Integer;
begin
  //DNS: TIdDNSResolver
  DNS.QueryType:= [qtA];
  DNS.Resolve(Name);
  for X:= 0 to DNS.QueryResult.Count-1 do begin
    if DNS.QueryResult[X].RecType = qtA then
      //Result:= DNS.QueryResult[X].RData;    <--- ????
  end;
end;

使用...

HostIP:= Lookup('www.google.com');

如何阅读此回复?

推荐答案

您需要键入 QueryResult 收集项目到一个特定的 TResultRecord 后裔取决于 RecType 项目的属性值。从 项目 属性参考:

You will need to typecast the QueryResult collection item to a specific TResultRecord descendant depending on the RecType property value of the item. From the Items property reference:


使用转换返回一个对象引用,允许访问任何
属性或特定于与
关联的后代类的方法在TResultRecord.RecType中的值。

Use casting to return an object reference that allows access to any properties or method specific to the descendant class associated with the value in TResultRecord.RecType.

TResultRecord 后代类是这样的:

The name pattern of the TResultRecord descendant classes is like this:

T<DNS lookup type>Record

所以在你的情况下,它将如下所示:

So in your case it would look like this:

for X := 0 to DNS.QueryResult.Count - 1 do 
begin
  if DNS.QueryResult[X].RecType = qtA then
    Result := TARecord(DNS.QueryResult[X]).IPAddress; // "A" lookup -> TARecord
end;

对于 AAAA 查找类型将是:

For a AAAA lookup type it would be:

for X := 0 to DNS.QueryResult.Count - 1 do 
begin
  if DNS.QueryResult[X].RecType = qtAAAA then
    Result := TAAAARecord(DNS.QueryResult[X]).Address; // "AAAA" lookup -> TAAAARecord
end;

可以使用IPv4和IPv6 DNS查找的示例功能 在这里找到

Example functions for IPv4 and IPv6 DNS lookups you may find here.

这篇关于阅读TIdDNSResolver的回应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆