IP获取

来源:互联网 发布:淘宝在哪里申请退货 编辑:程序博客网 时间:2024/05/17 07:07
获取外网IP的函数
function GetIP: string;
var
r: string;
p1, p2: Integer;
begin
r := idhttp1.get('http://www.net.cn/static/customercare/yourIP.asp');
p1 := Pos('<h2>', r); // 找到 h2 标签
p2 := Pos('</h2>', r); // 找到 h2 结束标签
Result := Copy(r, p1 + 4, p2 - p1 - 4);
end;
/////////////获取内网IP
function GetLocalIP: string;
type
TaPInAddr = array[0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe: PHostEnt;
pptr: PaPInAddr;
Buffer: array[0..63] of char;
I: Integer;
GInitData: TWSADATA;
begin
WSAStartup($101, GInitData);
Result := '';
GetHostName(Buffer, SizeOf(Buffer));
phe := GetHostByName(Buffer);
if phe = nil then Exit;
pptr := PaPInAddr(phe^.h_addr_list);
I := 0;
while pptr^[I] <> nil do begin
Result := StrPas(inet_ntoa(pptr^[I]^));
Inc(I);
end;
WSACleanup;
end;


Indy9里面的HTTP组件 



  1. uses IdHTTP;  
  2. function GetPublicIP: string;  
  3. var  
  4.   strIP, URL: string;  
  5.   iStart, iEnd: Integer;  
  6.   MyIdHTTP: TIdHTTP;  
  7. begin  
  8.   Result := '';  
  9.   MyIdHTTP := TIdHTTP.Create(nil);  
  10.   try  
  11.     try  
  12.       URL := MyIdHTTP.Get('http://www.ip138.com/ip2city.asp');  
  13.     except  
  14.     end;  
  15.   finally  
  16.     MyIdHTTP.Free;  
  17.   end;  
  18.     
  19.   if Length(URL) <> 0 then  
  20.   begin  
  21.     iStart := Pos('[', URL);  
  22.     iEnd := Pos(']', URL);  
  23.     if (iStart <> 0and (iEnd <> 0then  
  24.     begin  
  25.       strIP := Trim(Copy(URL, iStart + 1, iEnd - iStart - 1));  
  26.       if strIP <> '' then  
  27.         Result := strIP;  
  28.     end;  
  29.   end;  
  30. end;  

这个函数不错
function TGetIp.GetIp: string;
var
   xml : OleVariant;
   HtmlStr:string;
   p1,p2 : Integer;
begin
  Result :='' ;
  try
     xml := CreateOleObject('Microsoft.XMLHTTP');
     xml.Open('GET','http://www.net.cn/static/customercare/yourIP.asp', False);
     xml.Send;
     HtmlStr := xml.responseText;
     p1:=Pos('<h2>',HtmlStr);    // 找到 h2 标签
     p2:=Pos('</h2>',HtmlStr);   // 找到 h2 结束标签
     Result := Copy(HtmlStr, p1+4, p2-p1-4);
  except


  end;
end;

0 0
原创粉丝点击