delphi GetPublicIP 获取公网iP  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi GetPublicIP 获取公网iP


USES IdHTTP;

function GetPublicIP: string;
var
   strIP, URL: string;
   iStart, iEnd, i: Integer;
   MyIdHTTP: TIdHTTP;
begin
   Result := '';
   MyIdHTTP := TIdHTTP.Create(nil);

    //第1次尝试获取IP
    try
      try

        MyIdHTTP.request.UserAgent:= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; MyIE2; CustomExchangeBrowser; .NET CLR 1.1.4322) ';
        //URL := MyIdHTTP.Get('http://www.ip138.com/ip2city.asp'); http://www.ip138.com/
        URL := MyIdHTTP.Get('http://www.net.cn/static/customercare/yourip.asp');
        //URL := MyIdHTTP.Get('http://www.ip.cn/');
      except

      end;
   finally
      MyIdHTTP.Free;
   end;
    //Form1.Memo1.Lines.Add(url);
    if Length(URL) <> 0 then
    begin
      iStart := Pos('

', URL);

      if (iStart <> 0) then
      begin
          for I := 0 to 15 do
          begin
              if URL[istart+4+i] in [',' , ' ', '<'] then
                break;
              //strIP[i] := URL[istart+4+i];
              //Result := strIP;
          end;
          strIP := Trim(Copy(URL, iStart + 4, i));
          if strIP <> '' then
          begin
            Result := strIP;
            //ShowMessage('2 ' + Result);
            Exit;
          end;
      end;
    end;

    // 第2次尝试获取IP
   try
      try

        MyIdHTTP.request.UserAgent:= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; MyIE2; CustomExchangeBrowser; .NET CLR 1.1.4322) ';
        //URL := MyIdHTTP.Get('http://www.ip138.com/ip2city.asp'); http://www.ip138.com/
        //URL := MyIdHTTP.Get('http://www.net.cn/static/customercare/yourip.asp');
        URL := MyIdHTTP.Get('http://www.ip.cn/');
      except

      end;
   finally
      MyIdHTTP.Free;
   end;
    //Form1.Memo1.Lines.Add(url);
    if Length(URL) <> 0 then
    begin
      iStart := Pos('code>', URL);
      iEnd := Pos('
      if (iStart <> 0) and (iEnd <> 0) then
      begin
          strIP := Trim(Copy(URL, iStart + 5, iEnd - iStart - 5));
          if strIP <> '' then
          begin
            Result := strIP;
            //ShowMessage('1 ' + Result);
            Exit;
          end;
      end;
    end;
end;

推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.036187887191772 seconds