Avatar billede angelenglen Nybegynder
16. april 2004 - 10:25 Der er 1 løsning

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 ----------------
Avatar billede angelenglen Nybegynder
09. december 2004 - 15:56 #1
lukker
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester