BUG: ITC OpenUrl Method Doesn't Return Complete Files

ID: Q232194


The information in this article applies to:


SYMPTOMS

When you use the OpenUrl method of Microsoft Internet Transfer Control (ITC) to download files from Web servers, the resulting file may not be complete if the transfer-encoding of the responses is "chunked."


CAUSE

ITC allocates a buffer of 64K bytes and calls InternetReadFile in a loop. Whenever the buffer containing the read data is not full after the API returns, ITC will exit the loop. However InternetReadFile can return when less than the total number of bytes requested were read into the buffer, which causes the problem. It is the case if the transfer-encoding is "chunked."


RESOLUTION

One possible workaround is to call the WinInet APIs directly in Visual Basic as follows:


Private Sub Command1_Click()
    Dim hOpen               As Long
    Dim hOpenUrl            As Long
    Dim sUrl                As String
    Dim bDoLoop             As Boolean
    Dim bRet                As Boolean
    Dim sReadBuffer         As String * 2048
    Dim lNumberOfBytesRead  As Long
    Dim sBuffer             As String
   
    hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)    
    hOpenUrl = InternetOpenUrl(hOpen, sUrl, vbNullString, 0, INTERNET_FLAG_RELOAD, 0)

    bDoLoop = True
    While bDoLoop        
        sReadBuffer = vbNullString
        bRet = InternetReadFile(hOpenUrl, sReadBuffer, Len(sReadBuffer), lNumberOfBytesRead)
        sBuffer = sBuffer & Left$(sReadBuffer, lNumberOfBytesRead)
        If Not CBool(lNumberOfBytesRead) Then bDoLoop = False
    Wend
    
    Open "C:\Temp\log.txt" For Binary Access Write As #1
    Put #1, , sBuffer
    Close #1
    
    If hOpenUrl <> 0 Then InternetCloseHandle (hOpenUrl)
    If hOpen <> 0 Then InternetCloseHandle (hOpen)
End Sub  


STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.


REFERENCES

For an example of how to call the WinInet APIs directly from Visual Basic, please see the following article in the Microsoft Knowledge Base:

Q175179 FILE: VBFTP.EXE: Implementing FTP Using WinInet API from VB

Additional query words: resolution


Keywords          : kbVBp500bug kbVBp600bug kbGrpVB 
Version           : WINDOWS:5.0,6.0
Platform          : WINDOWS 
Issue type        : kbbug 

Last Reviewed: May 24, 1999