如何在vb中利用window internet透過http下載檔案

VB相關技術
回覆文章
頭像
tim
文章: 1318
註冊時間: 2008年 11月 26日, 00:49

如何在vb中利用window internet透過http下載檔案

文章 tim » 2010年 8月 11日, 10:57

查到相當多相當的資訊, 但大多為處理文字的 .htm 檔, 未能有效處理 binary 檔案, 將整理好的方式列出如下,

語法:

代碼: 選擇全部

     
    Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" ( _ 
                        ByVal sAgent As String, ByVal lAccessType As Long, _ 
                        ByVal sProxyName As String, ByVal sProxyBypass As String, _ 
                        ByVal lFlags As Long) As Long 
    Public Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" ( _ 
                        ByVal hInternetSession As Long, ByVal sUrl As String, _ 
                        ByVal sHeaders As String, ByVal lHeadersLength As Long, _ 
                        ByVal lFlags As Long, ByVal lContext As Long) As Long 
    Public Declare Function InternetReadFile Lib "wininet.dll" ( _ 
                        ByVal hFile As Long, sBuffer As Byte, _ 
                        ByVal lNumBytesToRead As Long, _ 
                        lNumberOfBytesRead As Long) As Integer 
    Public Declare Function InternetCloseHandle Lib "wininet.dll" ( _ 
                        ByVal hInet As Long) As Integer 
     
    Public Const INTERNET_FLAG_NO_CACHE_WRITE = &H4000000 
     
     
    Public Function GetUrlFile(stUrl As String, sSave As String) As Boolean 
        Dim lgInternet As Long, lgSession As Long 
        Dim stBuf() As Byte 
        Dim inRes As Integer 
        Dim lgRet As Long 
        Dim stTotal As String 
        Dim i As Integer 
        Dim bRet As Boolean 
         
         
        stTotal = vbNullString 
         
        GetUrlFile = False 
         
        lgSession = InternetOpen("VBTagEdit", 1, vbNullString, vbNullString, 0) 
        If lgSession Then 
     
            lgInternet = InternetOpenUrl(lgSession, stUrl, vbNullString, _ 
                                         0, INTERNET_FLAG_NO_CACHE_WRITE, 0) 
            If Dir(sSave) <> "" Then 
                Kill sSave 
            End If 
            Open sSave For Binary Access Write As #1 
     
            ReDim stBuf(1024) 
            If lgInternet Then 
                Do 
                    bRet = InternetReadFile(lgInternet, stBuf(0), 1024, lgRet) 
                    If bRet = False Then 
                        Exit Do 
                    End If 
                     
                    If lgRet = 0 Then 
                        GetUrlFile = True 
                        Exit Do 
                    End If 
                    ReDim Preserve stBuf(lgRet - 1) 
                    Put #1, , stBuf 
                    'DoEvents 
                Loop While (lgRet <> 0) 
            End If 
                     
            Close #1 
             
            bRet = InternetCloseHandle(lgInternet) 
            bRet = InternetCloseHandle(lgSession) 
             
        End If 
         
    End Function 
多多留言, 整理文章, 把經驗累積下來.....

回覆文章