Check Internet Connection from NAV

nav_126nav_126 Posts: 77Member
Hi,

On a machine where Dynamics NAV 2009 is installed, i want to check if internet connectivity is there or not.

If connectivity is there, system should execute further code else show error and exit.

I searched the forum and found the below code
httpreq is an automation with 'Microsoft WinHTTP Services, version 5.1'.WinHttpRequest
CREATE(httpreq,TRUE);
httpreq.Open('GET','HTTP://www.msn.com');
httpreq.Send;
//-------- if no connenction the system stops here because unable to send.
CLEAR(httpreq);

Now the issue is if connectivity is not there system gives an error as
"The call to member send failed. WinHttp.WinHttpRequest Returned error message".

System does not process any further code but I want to handle this exception through C/AL and give the
customized error message.

If anyone can help on this.


Regards,
NAV Developer

Comments

  • delybydelyby Posts: 1Member
    Try to create a Codeunit with only your code in it.
    CREATE(httpreq,TRUE);
    httpreq.Open('GET','HTTP://www.msn.com');
    httpreq.Send;
    //
    if no connenction the system stops here because unable to send.
    CLEAR(httpreq);

    Where you have written this line;
    //
    if no connenction the system stops here because unable to send. instead of your following code

    add this;
    IF httpreq.status <> 200 THEN;
    

    Then you should call this codeunit in your actual object like for example;
    IF NOT isInternetConnectionOpen.RUN() THEN ERROR(Text001);
    

    Hope this helps.
    Good Luck
  • koubekkoubek Posts: 84Member
    To achieve this functionality you can run the code within a Codeunit. This codeunit must be run from another place:
    IF NOT CODEUNIT.RUN(CODEUNIT::"Connection Check") THEN BEGIN
      ErrorText := GETLASTERRORTEXT;
      CLEARLASTERROR;
      ...
    END ELSE
      ...
    
  • MagnoMagno Posts: 168Member
    I created a function you can call to see if you can ping the site:
    isSiteActive(SiteURL : Text[250]) : Boolean
    VARS
    Name	DataType	Subtype	Length
    wShell	Automation	'Windows Script Host Object Model'.WshShell	
    pingFlag	Integer		
    WindowStyle	Integer		
    WaitForReturn	Boolean		
    /VARS
    CREATE(wShell,TRUE);
    
    IF LOWERCASE(COPYSTR(SiteURL,1,7)) = 'http://' THEN
      SiteURL := COPYSTR(SiteURL,8);
    IF LOWERCASE(COPYSTR(SiteURL,1,8)) = 'https://' THEN
      SiteURL := COPYSTR(SiteURL,9);
    
    IF STRPOS(SiteURL,'\') <> 0 THEN
      SiteURL := COPYSTR(SiteURL,1,STRPOS(SiteURL,'\')-1);
    IF STRPOS(SiteURL,'/') <> 0 THEN
      SiteURL := COPYSTR(SiteURL,1,STRPOS(SiteURL,'/')-1);
    
    WindowStyle := 0;
    WaitForReturn := TRUE;
    pingFlag := wShell.Run('ping -n 1 ' + SiteURL,WindowStyle,WaitForReturn);
    IF pingFlag = 0 THEN
      EXIT(TRUE);
    EXIT(FALSE);
    

    this should be a good indicator if the internet is on.
    But also note, that some servers are not allowing ping (like msn.com, google.com does allow it)
    There are no bugs, only random undocumented features...
    ---
    My Blog: http://NAV-Magno.be
  • nav_126nav_126 Posts: 77Member
    Hi Magno,

    Tried your suggestion but I am always getting pingflag = 1 even if internet connection is there or not.

    Can you please help on this ?


    Thanks in advance
  • MagnoMagno Posts: 168Member
    Hi nav_126,

    as I told just below my code:
    this should be a good indicator if the internet is on.
    But also note, that some servers are not allowing ping (like msn.com, google.com does allow it)

    It could be that the server you are testing does not allow pinging.
    I just did another test here, first i got a pingFlag 0. When I pulled out my network cable I got a 1 as pingFlag.

    Maybe you could select http://www.google.com as your ping test?

    Another thing could be that you domain does not allow outgoing ping requests (which sometimes happens, but not that much).
    You can test if this is the case by using Google (it should always allow ping), so try opening a command window and do ping http://www.google.com to see if you get a result there.

    you should see something like:
    Pinging http://www.l.google.com [74.125.79.103] with 32 bytes of data:
    Reply from 74.125.79.103: bytes=32 time=20ms TTL=53
    Reply from 74.125.79.103: bytes=32 time=16ms TTL=53
    Reply from 74.125.79.103: bytes=32 time=17ms TTL=53

    if you only get a result as following: Request timed out.
    that probably means your domain does not allow outgoing ping requests and you will not be able to use this function. (you can always try to convince your network administrator to allow these requests)
    There are no bugs, only random undocumented features...
    ---
    My Blog: http://NAV-Magno.be
Sign In or Register to comment.