Forum > Networking and Web Programming

Get external IP address without error if there is no internet

(1/3) > >>

Libra07:
I wrote a function that returns the IP address. It works fine, if I have internet. But if I have no internet, it stops with error message 217 .
My functions use FpHttpClient unit.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Function Get_Public_IP:String;       // #1Var   IdGetIP  : TFPHTTPClient;      Temp_IP  : String;      I:Longint;Const URL_List : Array[1..2] Of String               = ('http://dynupdate.no-ip.com/ip.php',                  'http://ipinfo.io/ip');Begin  Temp_IP:='0.0.0.0';   IdGetIP := TFPHTTPClient.Create(nil);  Temp_IP := IdGetIP.Get(URL_List[1]);  IdGetIP.Free;   Get_Public_IP:=Temp_IP;End; 
I read about it and tried another code, with TRY and EXCEPT, but it also works fine when i have is internet, but stops with an error 217 when there is no internet.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Function Get_Public_IP:String;       // #2Var  IdGetIP : TFPHTTPClient;  Html    : String;Const  gUrlIpA = 'http://dynupdate.no-ip.com/ip.php';  gUrlIpB = 'http://ipinfo.io/ip';Begin  Try    IdGetIP:=TFPHTTPClient.Create(nil);    Result := '';    Try      Html := IdGetIP.Get(gUrlIpA);    Except      Html := IdGetIP.Get(gUrlIpB);    End;    Result := Html;  Finally    IdGetIP.Free;  End;End; 
How can I extend my code to handle this error and not stop when there is no internet?
It's good for me if the functions return with '0.0.0.0' ip address, if I have no internet.

Libra07:
When I entered the two source codes here, I found the solution. :D
I edited the second source code:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Function Get_Public_IP:String;       // #2Var  IdGetIP : TFPHTTPClient;  Html    : String;Const  gUrlIpA = 'http://dynupdate.no-ip.com/ip.php';  gUrlIpB = 'http://ipinfo.io/ip';Begin  Try    IdGetIP:=TFPHTTPClient.Create(nil);    Result := '';    Try      Html := IdGetIP.Get(gUrlIpA);    Except      Html := '0.0.0.0';    End;    Result := Html;  Finally    IdGetIP.Free;  End;End; 

MarkMLl:
So the basic problem would appear to be that your computer or site is relying on an external name server (DNS etc.) to resolve e.g. http://ipinfo.io/ip to a dotted-quad address.

This whole area is rather fraught, since there is a vast number of possibilities relating to nameserver fallback etc. In fact your problems will really start when you try to work out whether you currently have a live route to the Internet at large.

MarkMLl

Dzandaa:
Hi,

You can try a validate function on the URL:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function CheckValidUrl(URL: String): integer;var HTTPSender: THTTPSend;begin  HTTPSender := THTTPSend.Create;  HTTPSender.Timeout := 50; try   HTTPSender.HTTPMethod('GET', URL);   if (HTTPSender.ResultCode >= 100) and (HTTPSender.ResultCode<=302) then Result := 0   else Result := HTTPSender.ResultCode; finally   HTTPSender.Free; end;end;   
If the function return 0, it's O.K.

B->

KodeZwerg:
I am using mostly same code (specific for Windows) that user Remy Lebeau posted here.
(Just to mention it, since I do not know about anything what the target platform is.)

Navigation

[0] Message Index

[#] Next page

Go to full version