login about faq

I'm using VFP 9 to develop business software.

I use Chilkat.Http to access some services in the web.

Some of my clients sometimes, temporarily, may not have internet access and because of it their computer hangs.

How can I check if internet access is available before using Chilkat.Http.

Best regards, Americo

asked Mar 05 '13 at 07:38

Americo%20Belinha's gravatar image

Americo Belinha

It's true that my value for ConnectTimeout was a bit high; I only check that once when the application loads, and I want to give it a fighting chance with all the network traffic on the client's machine... If I were to check the connection from time to time and didn't want to hurt my performance I would probably use 200-500. I would think, and this is just a guess, that 2 might be a little low.

I tried your example, and it failed on line 5, undefined variable. If that is the exact code you used, and your language is case sensitive, that might be part of the cause of your failure. After a quick fix it worked just fine (running windows 7).

As for the question of hanging- your on the right track. There is a setting missing, I forgot the heartbeat property! Thanks for helping me to catch that. I have modified my code as follows

var http = new ActiveXObject('Chilkat.Http');    
var success = http.UnlockComponent('****');
var html = '';

if (success) {
    http.ConnectTimeout = 200;
    http.HeartbeatMs = 200;

    html = http.QuickGetStr('http://www.mysite.com/connected.txt');

    if (html=='true') {                         // internet connectivity found
        RC.ID('ChooseEmail')['canEmail'] = true;
    else {
        RC.ID('ChooseEmail')['canEmail'] = false;

And tested it with and without internet connectivity. The results are without hanging, and return the expected results :)

Without setting the HearbeatMs property I don't think it can even check the ConnectTimeout for a while... so...

Thank you for finding this critical flaw in my implimentation, I'll be sending out this fix with my next updates!


answered Mar 06 '13 at 11:32

blaze4218's gravatar image


Another option is to use a the background threading feature of the ChilkatHTTP object to prevent your UI thread from hanging.

This example (in VB6, but should be easy enough to port to your language) demonstrates uses a Timer and the ChilkatHTTP object with the UseBgThread property set to 1 to continually check the Internet connection by attempting to get data from servers with known high availability (google.com, microsoft.com, etc...). It also tries to minimize the amount of time it hits those servers so that we aren't hammering them, but if you have your own domains you can use those (I recommend at least 2 different domains on different servers to know that it's not just your server that is down).

Option Explicit

Private WithEvents mo_Http As CHILKATHTTPLib.ChilkatHttp

Private m_ConnectedToInternet As Boolean
Private m_TestErrorCount As Long
Private m_MyServerUrl As String  ' URL to your own server for the bulk of testing

Private mo_TestUrls As Collection   ' Collection of known high-availability server list for secondary testing

Private Sub Form_Load()
   ' Set up your own server URL for the bulk of testing to avoid unnecessarily hitting other company's servers
   ' If the call to our server fails, only then will we try known high-availability servers (below)
   m_MyServerUrl = "http://www.MY_OWN_URL.com"

' Create a collection of URLs that are almost guaranteed to be available
   ' if the user's Internet connection is working
   Set mo_TestUrls = New Collection
   With mo_TestUrls
      .Add "http://www.google.com"
      .Add "http://www.microsoft.com"
      .Add "http://www.yahoo.com"
      .Add "http://www.apple.com"
   End With

' Create ChilkatHTTP object and set it up for background processing
   Set mo_Http = New CHILKATHTTPLib.ChilkatHttp
   With mo_Http
      .UnlockComponent "STATSLHttp_fCyJTNvsYE8t"
      .UseBgThread = 1  ' This will prevent UI thread from hanging by running test in a background thread
      .ConnectTimeout = 3
      .ReadTimeout = 3
   End With

' Create a timer object to continually test for Internet connectivity
   With Me.Timer1
      .Interval = 250   ' Short interval in MS
      .Enabled = True
   End With
End Sub

Private Sub Form_Unload(Cancel As Integer)
   ' Cleanup
   Me.Timer1.Enabled = False

' Make sure HTTP background thread cleans up before we leave the program to prevent crash
   If mo_Http.BgTaskRunning Then
      Do While mo_Http.BgTaskRunning
         Sleep 100
   End If
   Set mo_Http = Nothing
End Sub

Private Sub Timer1_Timer()
   Static s_LastChecked As Date  ' The last time a test was run
   Static s_CheckSeconds As Long ' The number of seconds to wait between tests
   Static s_TestStarted As Boolean   ' A test has been started

Dim l_TestUrl As String ' The URL we will be testing on this pass

Me.Timer1.Enabled = False

If (mo_Http.BgTaskRunning = 0) And Not s_TestStarted Then
      ' There is no test running
      If DateDiff("s", s_LastChecked, Now) > s_CheckSeconds Then
         ' Enough time has passed to warrant a new test

s_LastChecked = Now  ' Mark the test start time so the next test will be appropriately delayed
                              ' This prevents server hammering

If m_TestErrorCount = 0 Then
            ' No errors yet, so test our own server so we aren't abusing other's resources
            l_TestUrl = m_MyServerUrl
            ' Our server test failed! Check a known high-availability server to see if it fails too

' Get the next queued test URL
            l_TestUrl = mo_TestUrls.Item(1)
            ' Move the next queued test URL to the end of the queue
            mo_TestUrls.Remove 1
            mo_TestUrls.Add l_TestUrl
         End If

Debug.Print "Checking URL: " & l_TestUrl

s_TestStarted = True ' Mark the start of the test
         mo_Http.GetHead l_TestUrl     ' Get the URL in th background
      End If
   End If

If (mo_Http.BgTaskRunning = 0) And s_TestStarted Then
      ' The last test has finished processing
      s_TestStarted = False   ' Mark the test complete

If mo_Http.BgTaskSuccess = 1 Then
         ' Test succeeded, connected to Internet!

If Not m_ConnectedToInternet Then
            ' Internet is back up!
            ' You could raise an event here if you are using this code in a class
            ' e.g. RaiseEvent InternetUp()

Debug.Print "Internet UP!"
         End If

m_TestErrorCount = 0
         m_ConnectedToInternet = True
         s_CheckSeconds = 10  ' Leave lots of time between tests under the assumption that we will be connected for a while
                              ' and to avoid hammering test servers

         ' Test failed! We might not be connected to the Internet (or the site may just be down)
         ' We will need 2 failures from 2 different URLs to confirm that the Internet is down
         m_TestErrorCount = m_TestErrorCount + 1

If m_ConnectedToInternet Then
            s_CheckSeconds = 0   ' Test more often since we are disconnected and we want to know when the Internet is back ASAP
                                 ' This won't hammer the servers since we aren't connected to the Internet

If m_TestErrorCount > 1 Then
               ' More than 1 test failed! Assume we are disconnected from Internet.

' You could raise an event here if you are using this code in a class
               ' e.g. RaiseEvent InternetDown()

Debug.Print "Internet DOWN!"
            End If
         End If

If m_TestErrorCount > 1 Then
            ' Reset the error counter to test our own company server first again
            m_TestErrorCount = 0
            m_ConnectedToInternet = False
         End If
      End If

mo_Http.CloseAllConnections   ' Release "keep-alive" servers to prevent subsequent call failure
   End If

Me.Timer1.Enabled = True
End Sub

answered Mar 06 '13 at 13:00

jpbro's gravatar image

jpbro ♦

edited Mar 06 '13 at 15:45

I like this solution for continuous monitoring especially using 2 points of reference to determine failure. I would only recommend that the servers are tested 1 by 1 so that testing can be delayed at the first successful request, with the first server being your own (just my take, I like something I have control over 1st). If that's not already in there, I may have missed it.

But there's no point in UseBgThread if your app is going to wait for the response anyway-chilkat quote from below link

Also, reference for translating that is http://www.example-code.com/foxpro/http_backgroundThread.asp

(Mar 06 '13 at 13:48) blaze4218

To be clear- Americo wrote that there was only a hang when the computer didn't have internet connection, especially with the first code i posted. There is no real indication as to whether the app should proceed without knowing if internet is there... but if they're accessing web services and don't need the UI to wait for connectivity confirmation-- They might just consider moving the entire process to the bgthread, and only monitor the success/failure of the procedure.

IF (loHttp.BgTaskSuccess) THEN
    ? "Background task completed successfully."
(Mar 06 '13 at 14:03) blaze4218

I agree that this approach might not be suitable depending on the app requirements. If the application absolutely needs the Internet to be connected and use the HTTP object results in order to proceed, then your blocking approach is better. Mine works better for a soft approach (consider a status bar that can swap between connected and unconnected states, and where the user has other things to do that don't require Internet access).

(Mar 06 '13 at 14:36) jpbro ♦

I also agree that it's better etiquette to test your own server first, then only fallback to known high-availability servers if the call to your servers fails. I will update my sample.

(Mar 06 '13 at 14:37) jpbro ♦

Yes, I love it for the "soft approach". And since I can actually think of several usages for this, I have already copied it to my notes for future consideration. Very well thought out, and I love the logic branch that says "if we're disconnected, just go ahead and hammer away. You're not going to do any damage ;)".

(Mar 06 '13 at 14:43) blaze4218

I've just updated the sample code to implement your good suggestion to test your own server first before bothering any third-party servers. I also fixed a few minor things to make the code work better (explicitly call CloseAllConnections to dispose of "keep-alive" servers, and show where you could raise ConnectionUp and ConnectionDown events if you are using the code in a class). Thanks for your input!

(Mar 06 '13 at 15:47) jpbro ♦
showing 5 of 6 show all

This doesn't answer your questions "strictly" as it was posed... so it might not be the solution that your looking for; but I actually had the exact same issue recently, and the following is a snippet that shows how I was able to determine whether internet connectivity is currently present to allow the emailing of certain resources.

I decided to use a known resource to show connectivity, but I used the HTTP object to do so. If it can read the file then we are connected... the remote file contained only one word: "true" (unquoted)

var http = new ActiveXObject('Chilkat.Http');    
var success = http.UnlockComponent('****');
var html = '';

if (success) {
    http.ConnectTimeout = 2000;

    html = http.QuickGetStr('http://www.mysite.com/connected.txt');

    if (html=='true') {                         // internet connectivity found
        RC.ID('ChooseEmail')['canEmail'] = true;
    else {
        RC.ID('ChooseEmail')['canEmail'] = false;

And technically, if chilkat can't download a 4 byte file- then it's not going to satisfy any of my other HTTP needs


answered Mar 05 '13 at 18:32

blaze4218's gravatar image


Thanks for the answer.

The code I use in the beginning of that program is:

loxml = CreateObject('Chilkat.Xml')
loHttp = CreateObject('Chilkat.Http')
lnSuccess = loHttp.UnlockComponent("**********")
IF (lnSuccess = 1) THEN
    lohttp.ConnectTimeout = 2
    lnSuccess = loHttp.Download("http://www.elabora.pt/versoes.xml",SYS(2003)+"\versoes.xml")
    IF (lnSuccess = 1) THEN

The file "versoes.xml" is only 149 bytes long.

The strange thing is that only some computers, mainly using Windows XP, hangs when they don't have internet connection, yet most of the machines don't hang.

Could it be the ConnectTimeout too low?

I putted it low because when there was no internet connection it toke too long to advance to the next step.


answered Mar 06 '13 at 06:05

Americo%20Belinha's gravatar image

Americo Belinha

Just my 2cents - 7-19-14 If a windows computer is being used to check for internet connection, Internet Explorer is installed as part of windows....so I use the Internet Explorer Wininet.DLL

  • check if there is an internet connection
  • Fast and reliable web site *FUNCTION IsInet()


DECLARE LONG InternetCheckConnection IN Wininet.DLL STRING Url, LONG dwFlags, LONG RESERVED lcUrl = "http://www.google.com" IF InternetCheckConnection(lcUrl, FLAG_ICC_FORCE_CONNECTION, 0) <> 0 RETURN .T. ENDIF CLEAR DLLS InternetCheckConnection RETURN .F. ENDFUNC

Hope this helps


answered Jul 19 '14 at 13:28

ixl's gravatar image


Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported



Asked: Mar 05 '13 at 07:38

Seen: 25,185 times

Last updated: Jul 19 '14 at 13:28

powered by OSQA