使用URLDownloadToFile这个API下载一个文件。
但是却发现下载到的是存在于CATCH中的东西,我手动在IE中可以下载到最新的文件,而使用URLDownloadToFile却只能下载到一个旧的文件。不知道是怎么回事。我到底是应该在程序中再写点什么东西解决呢,还是需要设置我的操作系统呢?
顺便说一下我这里的情况:客户机通过PROXY SERVER上网的,我查看了PROXY服务器的设置,没有开启CATCH功能。因为需要下载的是一个LOG文件,随时都可能需要用到,并且数据也随时改变的。最好不要没头没脑贴一大段代码上来,偶看不懂别人的程序的,呵呵。说个原理就行,偶会自己琢磨的。顺便一说,可用分又过1000了,散散。
但是却发现下载到的是存在于CATCH中的东西,我手动在IE中可以下载到最新的文件,而使用URLDownloadToFile却只能下载到一个旧的文件。不知道是怎么回事。我到底是应该在程序中再写点什么东西解决呢,还是需要设置我的操作系统呢?
顺便说一下我这里的情况:客户机通过PROXY SERVER上网的,我查看了PROXY服务器的设置,没有开启CATCH功能。因为需要下载的是一个LOG文件,随时都可能需要用到,并且数据也随时改变的。最好不要没头没脑贴一大段代码上来,偶看不懂别人的程序的,呵呵。说个原理就行,偶会自己琢磨的。顺便一说,可用分又过1000了,散散。
有没有遇到过同样情况的朋友阿?你们是如何解决的呢?
清楚是什麼意思?
我下载的是exe文件。
http://vbnet.mvps.org/index.html?code/internet/urldownloadtofilenocache.htmOption Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2005 VBnet, Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
' applications, but you may not reproduce
' or publish this code on any web site,
' online service, or distribute as source
' on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Declare Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" _
(ByVal pCaller As Long, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As Long, _
ByVal lpfnCB As Long) As Long
Private Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" _
Alias "DeleteUrlCacheEntryA" _
(ByVal lpszUrlName As String) As Long
Private Const ERROR_SUCCESS As Long = 0
Private Const BINDF_GETNEWESTVERSION As Long = &H10
Private Const INTERNET_FLAG_RELOAD As Long = &H80000000
Private Sub Form_Load() Command1.Caption = "Download File"
Command2.Caption = "Clear Cache && Download"
End Sub
Private Sub Command1_Click() Dim sSourceUrl As String
Dim sLocalFile As String
Dim hfile As Long
sSourceUrl = "http://vbnet.mvps.org/code/faq/fileloadtext.htm"
sLocalFile = "c:\deleteme.htm"
Label1.Caption = sSourceUrl
Label2.Caption = sLocalFile
If DownloadFile(sSourceUrl, sLocalFile) Then
hfile = FreeFile
Open sLocalFile For Input As #hfile
Text1.Text = Input$(LOF(hfile), hfile)
Close #hfile
End IfEnd Sub
Private Sub Command2_Click() Dim sSourceUrl As String
Dim sLocalFile As String
Dim hfile As Long
sSourceUrl = "http://vbnet.mvps.org/code/faq/fileloadtext.htm"
sLocalFile = "c:\deleteme.htm"
Label1.Caption = sSourceUrl
Label2.Caption = sLocalFile
'Attempt to delete any cached version of
'the file. Since we're only interested in
'nuking the file, the routine is called as
'a sub. If the return value is requires
'(calling as a function), DeleteUrlCacheEntry
'returns 1 if successful, or 0 otherwise, e.g.
' If DeleteUrlCacheEntry(sourceUrl) = 1 Then
' Debug.Print "cached file found and deleted"
' Else
' Debug.Print "no cached file for " & sourceUrl
' End If
'Note that the remote URL is passed as this is the
'name the cached file is known by. This does NOT
'delete the file from the remote server.
Call DeleteUrlCacheEntry(sSourceUrl)
If DownloadFile(sSourceUrl, sLocalFile) = True Then hfile = FreeFile
Open sLocalFile For Input As #hfile
Text1.Text = Input$(LOF(hfile), hfile)
Close #hfile End If
End Sub
Private Function DownloadFile(sSourceUrl As String, _
sLocalFile As String) As Boolean
'Download the file. BINDF_GETNEWESTVERSION forces
'the API to download from the specified source.
'Passing 0& as dwReserved causes the locally-cached
'copy to be downloaded, if available. If the API
'returns ERROR_SUCCESS (0), DownloadFile returns True.
DownloadFile = URLDownloadToFile(0&, _
sSourceUrl, _
sLocalFile, _
BINDF_GETNEWESTVERSION, _
0&) = ERROR_SUCCESS
End Function
http://www.autohotkey.com/docs/commands/URLDownloadToFile.htm大致的意思就是传递一个假的参数来强制下载,例如:http://.../test.exe?fakeParam=42