BUG: ITC OpenUrl Method Doesn't Return Complete FilesID: Q232194
|
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."
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."
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
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.
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