Advertisement
HEX0x29A

GetIPAndCountry

Nov 1st, 2015
510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.08 KB | None | 0 0
  1. uses
  2.   IdHTTP, IdSSLOpenSSL, System.RegularExpressions;
  3.  
  4. function GetIPAndCountry(var IP: string; var Country: string): Boolean;
  5. const
  6.   UserAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko';
  7. var
  8.   http: TIdHttp;
  9.   ssl: TIdSSLIOHandlerSocketOpenSSL;
  10.   buf: string;
  11.   mch: TMatch;
  12. begin
  13.   IP := '';
  14.   Country := '';
  15.   http := TIdHTTP.Create(nil);
  16.   with http do
  17.   try
  18.     HandleRedirects := True;
  19.     Request.UserAgent := UserAgent;
  20.     ssl := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  21.     IOHandler := ssl;
  22.     ConnectTimeout := 60000;
  23.     ReadTimeout := 60000;
  24.     buf := Get('https://2ip.ru/');
  25.     if not buf.IsEmpty then
  26.     begin
  27.       mch := TRegEx.Match(buf, '>(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})<');
  28.       if mch.Success then
  29.         IP := mch.Groups.Item[1].Value;
  30.       mch := TRegEx.Match(buf, '\"\/geoip\/\"\/>([^\,<]+)');
  31.       if mch.Success then
  32.         Country := mch.Groups.Item[1].Value;
  33.     end;
  34.   finally
  35.     FreeAndNil(http);
  36.     FreeAndNil(ssl);
  37.   end;
  38.   Result := not (IP.IsEmpty and Country.IsEmpty);
  39. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement