Download - Brug IE's proxy settings?
jeg har et program der downloader en fil fra Internettet til harddisken.Det virker som det skal, eneste problem, er at hvis computeren er bag en proxy-server, virker det ikke.
Er der ikke en let måde at implemtere support for IE's proxy-settings?
jeg bruger følgende kode:
'--------- FileDownloader.bas ------------
Private SessionHandle As Long
Private ConnectionHandle As Long
Public AbortDownload As Boolean
Private Declare Function InternetCloseHandle Lib "wininet" (ByVal hInet As Long) As Integer
Private Declare Function InternetOpen Lib "wininet" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Private Declare Function InternetOpenUrl Lib "wininet" Alias "InternetOpenUrlA" (ByVal hOpen As Long, ByVal sURL As String, ByVal sHeaders As String, ByVal lLength As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
Private Declare Function InternetReadFile Lib "wininet" (ByVal hFile As Long, ByVal tmp As String, ByVal lNumBytesToRead As Long, bytesread As Long) As Integer
Private Declare Function MoveFile Lib "kernel32" Alias "MoveFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String) As Long
Public Function DownloadFile(ByVal URL As String, ByVal LocalFile As String, Optional ByVal TempFile As String) As Boolean
Dim iBuffer As String * 2048, bytesread As Long
Dim filedata As String, Filenum As Long
AbortDownload = False
If TempFile = "" Then TempFile = "C:\downloadfile.tempfile"
On Error GoTo ErrProc
If Left(URL, 7) <> "http://" Then
URL = "http://" & URL
End If
Filenum = FreeFile
Open TempFile For Binary Access Write As #Filenum
SessionHandle = InternetOpen("OpenUrl", 0, vbNullString, vbNullString, 0)
ConnectionHandle = InternetOpenUrl(SessionHandle, URL, vbNullString, 0, INTERNET_FLAG_RELOAD, 0)
Do
iBuffer = vbNullString
InternetReadFile ConnectionHandle, iBuffer, Len(iBuffer), bytesread
DoEvents
If AbortDownload Or bytesread = 0 Then
Exit Do
Else
filedata = Left(iBuffer, bytesread)
Put #Filenum, , filedata
End If
Loop
If ConnectionHandle <> 0 Then
InternetCloseHandle ConnectionHandle
End If
If SessionHandle <> 0 Then
InternetCloseHandle SessionHandle
End If
Close #Filenum
If AbortDownload Then
Kill TempFile
DownloadFile = False
ElseIf FileLen(TempFile) > 0 Then
'MsgBox "Moving file from: " & TempFile & " to: " & LocalFile & " ."
If Not Dir(LocalFile) = "" Then Kill LocalFile '-- If the file exists, delete it before renaming the tempfile
MoveFile TempFile, LocalFile '-- Rename the tempfile to the desired filename
DownloadFile = True
Else
DownloadFile = False
End If
Exit Function
ErrProc:
Close #Filenum
DownloadFile = False
End Function
'--------- FileDownloader.bas ------------
'-------------- Form1.frm ----------------
Private Sub Command1_Click()
DownloadFile "http://www.eksperten.dk/img/elogo.png", "C:\elogo.png", "C:\downloadfile.tempfile"
End Sub
'-------------- Form1.frm ----------------
