View RSS Feed

Development Team Blog

How to change the internet connection timeout

Rating: 10 votes, 5.00 average.
When your Visual DataFlex application makes an external call to a webservice the main program waits for its completion. If the request does not return immediately several things can be the cause. Your connection to your ISP can slow, the webserver you try to reach is busy, can be difficult found etc. If you want your application to continue as soon as possible and know (and this is they unknown keyfactor) that particular call should not take more than a couple of milliseconds to complete you can query and change the internet timeout value. There are two Windows API calls (InternetQueryOption and InternetSetOption). General information can be read here.

When changing any option you have to be aware of the range and possible side effects that may occur. For example; in the sample code of this blog I query and set the global default options which means it changes the behavior for Internet Explorer and all Internet actions afterwards. I do that because we change an option before calling a webservice and thus we do not operate on the specific handle.

There are many internet options you can query and can set. I would advise to always query first, then set, and later reset the option to the queried value.

The Visual DataFlex side of the option query and set instructions is:
Code:
External_Function WinAPI_InternetQueryOption "InternetQueryOptionA" WinInet.Dll Handle hInternet DWORD dwOption Pointer lpBuffer Pointer lpdwBufferLength Returns Boolean
External_Function WinAPI_InternetSetOption "InternetSetOptionA" WinInet.Dll Handle hInternet Dword dwOption Pointer lpBuffer Dword dwBufferLength Returns Boolean
Back to the timeout issue. As you can read here there are many options that do something with timeout. Which one to choose? Should we use a connection timeout setting or a receive (and send) timeout setting? In case of a webservice I would choose for a receive timeout but I leave that to you.

So let us first retrieve the current receive timeout.
Code:
Move (SizeOfType (Integer)) To dwBufferLength
Move 0 To iCurrentReceiveTimeOut

Move (WinAPI_InternetQueryOption (0, INTERNET_OPTION_RECEIVE_TIMEOUT, AddressOf (iCurrentReceiveTimeOut), AddressOf (dwBufferLength))) To bQueryOptionSucceeded
The IntenetQueryOption can also return the size of the buffer needed but in this case an integer is fine.
In my computer the returned timeout is 30000 milliseconds (= 30 seconds).
We change the timeout to 2000 milliseconds by:
Code:
Move (WinAPI_InternetSetOption (0, INTERNET_OPTION_RECEIVE_TIMEOUT, AddressOf (iNewReceiveTimeOut), dwBufferLength)) To bQueryOptionSucceeded
Then we call the webservice
Code:
Get wsSayHello Of oWSTestService "Vincent" To sText
Get peTransferStatus Of oWSTestService To eTransferStatus
As said before I think it is a good habit to reset the just changed value back to the queried value
Code:
Move (WinAPI_InternetSetOption (0, INTERNET_OPTION_RECEIVE_TIMEOUT, AddressOf (iCurrentReceiveTimeOut), dwBufferLength)) To bQueryOptionSucceeded
In full the code is:
Code:
Define INTERNET_OPTION_CALLBACK                                 For 1
Define INTERNET_OPTION_CONNECT_TIMEOUT                          For 2
Define INTERNET_OPTION_CONNECT_RETRIES                          For 3
Define INTERNET_OPTION_CONNECT_BACKOFF                          For 4
Define INTERNET_OPTION_SEND_TIMEOUT                             For 5
Define INTERNET_OPTION_CONTROL_SEND_TIMEOUT                     For INTERNET_OPTION_SEND_TIMEOUT
Define INTERNET_OPTION_RECEIVE_TIMEOUT                          For 6
Define INTERNET_OPTION_CONTROL_RECEIVE_TIMEOUT                  For INTERNET_OPTION_RECEIVE_TIMEOUT
Define INTERNET_OPTION_DATA_SEND_TIMEOUT                        For 7
Define INTERNET_OPTION_DATA_RECEIVE_TIMEOUT                     For 8
Define INTERNET_OPTION_HANDLE_TYPE                              For 9
Define INTERNET_OPTION_LISTEN_TIMEOUT                           For 11
Define INTERNET_OPTION_READ_BUFFER_SIZE                         For 12
Define INTERNET_OPTION_WRITE_BUFFER_SIZE                        For 13

Define INTERNET_OPTION_ASYNC_ID                                 For 15
Define INTERNET_OPTION_ASYNC_PRIORITY                           For 16

Define INTERNET_OPTION_PARENT_HANDLE                            For 21
Define INTERNET_OPTION_KEEP_CONNECTION                          For 22
Define INTERNET_OPTION_REQUEST_FLAGS                            For 23
Define INTERNET_OPTION_EXTENDED_ERROR                           For 24

Define INTERNET_OPTION_OFFLINE_MODE                             For 26
Define INTERNET_OPTION_CACHE_STREAM_HANDLE                      For 27
Define INTERNET_OPTION_USERNAME                                 For 28
Define INTERNET_OPTION_PASSWORD                                 For 29
Define INTERNET_OPTION_ASYNC                                    For 30
Define INTERNET_OPTION_SECURITY_FLAGS                           For 31
Define INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT              For 32
Define INTERNET_OPTION_DATAFILE_NAME                            For 33
Define INTERNET_OPTION_URL                                      For 34
Define INTERNET_OPTION_SECURITY_CERTIFICATE                     For 35
Define INTERNET_OPTION_SECURITY_KEY_BITNESS                     For 36
Define INTERNET_OPTION_REFRESH                                  For 37
Define INTERNET_OPTION_PROXY                                    For 38
Define INTERNET_OPTION_SETTINGS_CHANGED                         For 39
Define INTERNET_OPTION_VERSION                                  For 40
Define INTERNET_OPTION_USER_AGENT                               For 41
Define INTERNET_OPTION_END_BROWSER_SESSION                      For 42
Define INTERNET_OPTION_PROXY_USERNAME                           For 43
Define INTERNET_OPTION_PROXY_PASSWORD                           For 44
Define INTERNET_OPTION_CONTEXT_VALUE                            For 45
Define INTERNET_OPTION_CONNECT_LIMIT                            For 46
Define INTERNET_OPTION_SECURITY_SELECT_CLIENT_CERT              For 47
Define INTERNET_OPTION_POLICY                                   For 48
Define INTERNET_OPTION_DISCONNECTED_TIMEOUT                     For 49
Define INTERNET_OPTION_CONNECTED_STATE                          For 50
Define INTERNET_OPTION_IDLE_STATE                               For 51
Define INTERNET_OPTION_OFFLINE_SEMANTICS                        For 52
Define INTERNET_OPTION_SECONDARY_CACHE_KEY                      For 53
Define INTERNET_OPTION_CALLBACK_FILTER                          For 54
Define INTERNET_OPTION_CONNECT_TIME                             For 55
Define INTERNET_OPTION_SEND_THROUGHPUT                          For 56
Define INTERNET_OPTION_RECEIVE_THROUGHPUT                       For 57
Define INTERNET_OPTION_REQUEST_PRIORITY                         For 58
Define INTERNET_OPTION_HTTP_VERSION                             For 59
Define INTERNET_OPTION_RESET_URLCACHE_SESSION                   For 60
Define INTERNET_OPTION_ERROR_MASK                               For 62
Define INTERNET_OPTION_FROM_CACHE_TIMEOUT                       For 63
Define INTERNET_OPTION_BYPASS_EDITED_ENTRY                      For 64
Define INTERNET_OPTION_DIAGNOSTIC_SOCKET_INFO                   For 67
Define INTERNET_OPTION_CODEPAGE                                 For 68
Define INTERNET_OPTION_CACHE_TIMESTAMPS                         For 69
Define INTERNET_OPTION_DISABLE_AUTODIAL                         For 70
Define INTERNET_OPTION_MAX_CONNS_PER_SERVER                     For 73
Define INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER                 For 74
Define INTERNET_OPTION_PER_CONNECTION_OPTION                    For 75
Define INTERNET_OPTION_DIGEST_AUTH_UNLOAD                       For 76
Define INTERNET_OPTION_IGNORE_OFFLINE                           For 77
Define INTERNET_OPTION_IDENTITY                                 For 78
Define INTERNET_OPTION_REMOVE_IDENTITY                          For 79
Define INTERNET_OPTION_ALTER_IDENTITY                           For 80
Define INTERNET_OPTION_SUPPRESS_BEHAVIOR                        For 81
Define INTERNET_OPTION_AUTODIAL_MODE                            For 82
Define INTERNET_OPTION_AUTODIAL_CONNECTION                      For 83
Define INTERNET_OPTION_CLIENT_CERT_CONTEXT                      For 84
Define INTERNET_OPTION_AUTH_FLAGS                               For 85
Define INTERNET_OPTION_COOKIES_3RD_PARTY                        For 86
Define INTERNET_OPTION_DISABLE_PASSPORT_AUTH                    For 87
Define INTERNET_OPTION_SEND_UTF8_SERVERNAME_TO_PROXY            For 88
Define INTERNET_OPTION_EXEMPT_CONNECTION_LIMIT                  For 89
Define INTERNET_OPTION_ENABLE_PASSPORT_AUTH                     For 90

Define INTERNET_OPTION_HIBERNATE_INACTIVE_WORKER_THREADS        For 91
Define INTERNET_OPTION_ACTIVATE_WORKER_THREADS                  For 92
Define INTERNET_OPTION_RESTORE_WORKER_THREAD_DEFAULTS           For 93
Define INTERNET_OPTION_SOCKET_SEND_BUFFER_LENGTH                For 94
Define INTERNET_OPTION_PROXY_SETTINGS_CHANGED                   For 95

// Internet Handle types
Define INTERNET_HANDLE_TYPE_INTERNET           for 1
Define INTERNET_HANDLE_TYPE_CONNECT_FTP        for 2
Define INTERNET_HANDLE_TYPE_CONNECT_GOPHER     for 3
Define INTERNET_HANDLE_TYPE_CONNECT_HTTP       for 4
Define INTERNET_HANDLE_TYPE_FTP_FIND           for 5
Define INTERNET_HANDLE_TYPE_FTP_FIND_HTML      for 6
Define INTERNET_HANDLE_TYPE_FTP_FILE           for 7
Define INTERNET_HANDLE_TYPE_FTP_FILE_HTML      for 8
Define INTERNET_HANDLE_TYPE_GOPHER_FIND        for 9
Define INTERNET_HANDLE_TYPE_GOPHER_FIND_HTML   for 10
Define INTERNET_HANDLE_TYPE_GOPHER_FILE        for 11
Define INTERNET_HANDLE_TYPE_GOPHER_FILE_HTML   for 12
Define INTERNET_HANDLE_TYPE_HTTP_REQUEST       for 13
Define INTERNET_HANDLE_TYPE_FILE_REQUEST       for 14

External_Function WinAPI_InternetQueryOption "InternetQueryOptionA" WinInet.Dll Handle hInternet DWORD dwOption Pointer lpBuffer Pointer lpdwBufferLength Returns Boolean
External_Function WinAPI_InternetSetOption "InternetSetOptionA" WinInet.Dll Handle hInternet Dword dwOption Pointer lpBuffer Dword dwBufferLength Returns Boolean

Use dfClient.pkg
Use cWSTestService.pkg
Use Windows.pkg

DEFERRED_VIEW Activate_oNewView FOR ;
;
Object oNewView is a dbView
    Set Border_Style to Border_Thick
    Set Location to 2 2
    Set Size to 200 300

    Object oWSTestService is a cWSTestService
    End_Object

    Object oButton1 is a Button
        Set Label to "oButton1"
        Set Location to 57 63

        Procedure OnClick
            String sText
            dWord dwBufferLength
            Boolean bQueryOptionSucceeded
            Integer iCurrentReceiveTimeOut iNewReceiveTimeOut eTransferStatus
        
            Move (SizeOfType (Integer)) to dwBufferLength
            Move 0 To iCurrentReceiveTimeOut
        
            Move (WinAPI_InternetQueryOption (0, INTERNET_OPTION_RECEIVE_TIMEOUT, AddressOf (iCurrentReceiveTimeOut), AddressOf (dwBufferLength))) To bQueryOptionSucceeded
            Move 2000 To iNewReceiveTimeOut
            Move (WinAPI_InternetSetOption (0, INTERNET_OPTION_RECEIVE_TIMEOUT, AddressOf (iNewReceiveTimeOut), dwBufferLength)) To bQueryOptionSucceeded
        
            Get wsSayHello Of oWSTestService "Vincent" To sText
            Get peTransferStatus Of oWSTestService To eTransferStatus
        
            Move (WinAPI_InternetSetOption (0, INTERNET_OPTION_RECEIVE_TIMEOUT, AddressOf (iCurrentReceiveTimeOut), dwBufferLength)) To bQueryOptionSucceeded
        
            If (eTransferStatus = wssOK) Begin
                Send Info_Box sText "TestService Results"
            End
            Else Begin
                Send Stop_Box eTransferStatus "Transfer Status"
            End
        End_Procedure
    End_Object
CD_End_Object