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('<h2>', 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('</code', URL);
      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;

相关阅读 >>

Delphi findfiles获取目录下所有文件

Delphi高亮显示trichedit当前行

Delphi把域名转换成ip

Delphi 10.3.1 android沉浸式透明状态栏

Delphi 获取文件创建时间,修改时间,最后

processid, process handle, window handle 之间的互相转换

Delphi listbox1列表随机打乱

Delphi post登陆Delphi盒子论坛源码

cvcode.pas

Delphi 取出鼠百标点击的 stringgrid 中某单度元格的值

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...