首页 > 其他分享 >获取外网ip地址

获取外网ip地址

时间:2023-03-06 22:05:21浏览次数:42  
标签:HTTP1.1 ip i2 stxt 外网 地址 IdHTTP1

通过TIdHTTP空间的Get访问外网IP查询

procedure TForm1.Button1Click(Sender: TObject);
var
    stxt:string;
    i,i2:integer;
begin
   //如果访问出现“HTTP1.1/ 403 Forbidden”错误,需要以下4行代码
    IdHTTP1.HandleRedirects:=true;
    //IdHTTP1.HTTPOptions[hoForceEncodeParams];
    IdHTTP1.ProtocolVersion:= pv1_1;
    IdHTTP1.Request.UserAgent := 'Mozilla/5.0 (compatible; Test)';
    // 为解决出现“HTTP1.1/ 403 Forbidden”错误,需要以上4行代码
    stxt :=IdHTTP1.Get('http://www.ip138.com/ip2city.asp');   //获取数据
    i:=Pos(':[',stxt);//查找第一个位置
    Delete(stxt,1,i+2);//delete
    i2:=Pos(']',stxt);
    stxt:=Copy(stxt,1,i2-1);
    label1.Caption := stxt;
end;

 

标签:HTTP1.1,ip,i2,stxt,外网,地址,IdHTTP1
From: https://www.cnblogs.com/sixty-five/p/17185658.html

相关文章