Results 1 to 2 of 2

Thread: Timeout for wininet

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Timeout for wininet

    Hi, I'm using the code below to download a very small text file from a server (maximum size of 500 bytes) into a string, because I rather not add the Inet control for something as simple as this.

    If there are server problems the application hangs. The Inet control has "RequestTimeout" to set a timeout. If the data hasn't been downloaded within a certain time it shows an error that can be trapped.

    Is it possible to set a timeout with wininet?

    vb Code:
    1. Option Explicit
    2. Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
    3. Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal sURL As String, ByVal sHeaders As String, ByVal lHeadersLength As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
    4. Private Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
    5. Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
    6. Private Declare Function DeleteUrlCacheEntry Lib "wininet.dll" (ByVal lpszUrlName As String) As Long
    7.  
    8. Public Const IF_FROM_CACHE = &H1000000
    9. Public Const IF_MAKE_PERSISTENT = &H2000000
    10. Public Const IF_NO_CACHE_WRITE = &H4000000
    11.        
    12. Private Const BUFFER_LEN = 256
    13.  
    14.  
    15. Public Function GetUrlSource(sURL As String) As String
    16.     Dim sBuffer As String * BUFFER_LEN, iResult As Integer, sData As String
    17.     Dim hInternet As Long, hSession As Long, lReturn As Long
    18.  
    19.     DeleteUrlCacheEntry sURL
    20.    
    21.     'get the handle of the current internet connection
    22.     hSession = InternetOpen("vb wininet", 1, vbNullString, vbNullString, 0)
    23.     'get the handle of the url
    24.     If hSession Then hInternet = InternetOpenUrl(hSession, sURL, vbNullString, 0, IF_NO_CACHE_WRITE, 0)
    25.     'if we have the handle, then start reading the web page
    26.     If hInternet Then
    27.         'get the first chunk & buffer it.
    28.         iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
    29.         sData = sBuffer
    30.         'if there's more data then keep reading it into the buffer
    31.         Do While lReturn <> 0
    32.             iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
    33.             sData = sData + Mid(sBuffer, 1, lReturn)
    34.         Loop
    35.     End If
    36.    
    37.     'close the URL
    38.     iResult = InternetCloseHandle(hInternet)
    39.  
    40.     GetUrlSource = sData
    41. End Function
    Last edited by Chris001; Nov 30th, 2007 at 04:48 PM.

  2. #2
    Lively Member Spetnik's Avatar
    Join Date
    Jan 2002
    Posts
    121

    Re: Timeout for wininet

    Sure, you can use WinHTTP.
    Make sure to Reference "Microsoft WinHTTP Services" in your app.

    Code:
    Dim oRequest As WinHttp.WinHttpRequest
        
        Set oRequest = New WinHttp.WinHttpRequest
        
        oRequest.SetTimeouts lngResolveTimeout, lngConnTimeOut, lngSendTimeout, lngReceiveTimeout
        oRequest.Open "GET", "http://servername/filename.html"
        oRequest.Send
        GetUrlSource = oRequest.ResponseText

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width