自己程序用到,一般查询A记录的代码到处都是,非A记录却很少见到,至少百度没搜到,记录下来,测试环境Delphi 2007 + Indy 10.1.5。

function GetDNS():string;
var
  DNS: TIdDNSResolver;
  MyRecord: TResultRecord;
  Txt: TTextRecord;
  Timeout:integer;
  s:string;
begin
  s:='';
  Timeout:=5000;
  DNS := TIdDNSResolver.Create(nil);
  try
    DNS.WaitingTime := Timeout;
    //这里据我测试,必须设置DNS服务器地址
    DNS.Host := '114.114.114.114';
    //重要的是这里,设置查询类型
    DNS.QueryType := [qtTXT];
    //解析这个主机名
    DNS.Resolve('tingtao.org');
    if DNS.QueryResult.Count>0 then
    begin     //如果同时查询多个纪录,用for循环一下
    MyRecord := DNS.QueryResult[0];
     Txt := TTextRecord(MyRecord);
     s:=txt.Text.Text;
    end;
  finally
    DNS.Free;
  end;
  result:=s;
end;

如果多个记录的话,这几行有所更改:

DNS.QueryType := [qtTXT, qtService];
for I := 0 to DNS.QueryResult.Count -1 do
MyRecord := DNS.QueryResult[I];
case Record.RecType of
      begin
        qtTXT: begin
          Txt := TTextRecord(Record);

 

作者 听涛

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注