Function Gethttpdownload(sUrl AsString) As boolen Dim s AsString Dim hOpen AsLong Dim hOpenUrl AsLong Dim bDoLoop AsBoolean Dim bRet AsBoolean Dim sReadBuffer AsString*2048 Dim lNumberOfBytesRead AsLong hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0) hOpenUrl = InternetOpenUrl(hOpen, sUrl, vbNullString, 0, INTERNET_FLAG_RELOAD, 0) bDoLoop =True DoWhile bDoLoop sReadBuffer = vbNullString bRet = InternetReadFile(hOpenUrl, sReadBuffer, Len(sReadBuffer), lNumberOfBytesRead) s = s &Left$(sReadBuffer, lNumberOfBytesRead) IfNotCBool(lNumberOfBytesRead) Then bDoLoop =False Loop FileName ="E:/BitSpirit/Torrent/121212.torrent" F1 = FreeFile Open FileName For Binary As F1 Put F1, , s Close F1 If hOpen <>0Then InternetCloseHandle (hOpen) Gethttpdownload =true End Function
Public Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
发现其定义
ByVal sBuffer As String 看来只能用String了,在网上查找过程中,发现人家用VC写的 程序中这人参数可以是其它的,所以查看了一下Internetreadfile的原型。发现的确可以, 所以我把internetreadfile的定义修改了一下,为了通用,我为新的internetreadfile定义了一个别名。 Internetreadfilebyte申明如下:
Public Declare Function InternetReadFileByte Lib "wininet.dll" Alias "InternetReadFile" (ByVal hFile As Long, ByRef sBuffer As Byte, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
Function FileDownload(sUrl As Variant) AsBoolean Dim b(99) AsByte Dim EndByte() AsByte Dim s AsString Dim hOpen AsLong Dim hOpenUrl AsLong Dim bDoLoop AsBoolean Dim bRet AsBoolean Dim bbuffer AsByte Dim sReadBuffer AsString Dim FileName AsString Dim lNumberOfBytesRead AsLong Dim F1 AsInteger Dim strsize AsString Dim size AsLong strsize =String$(1024, "") F1 = FreeFile stTotal = vbNullString FileName ="E:/BitSpirit/Torrent/121212.torrent" Open FileName For Binary As F1 hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0) hOpenUrl = InternetOpenUrl(hOpen, sUrl, vbNullString, 0, INTERNET_FLAG_RELOAD, 0) bDoLoop =True HttpQueryInfo hOpenUrl, HTTP_QUERY_CONTENT_LENGTH Or HTTP_QUERY_FLAG_NUMBER, ByVal strsize, Len(strsize), 0 size =CLng(Trim(strsize)) For j =1To size /100 bDoLoop = InternetReadFileByte(hOpenUrl, b(0), 100, lNumberOfBytesRead) Put F1, , b IfNotCBool(lNumberOfBytesRead) ThenExitFor Next If size Mod100<>0Then tmp = (size Mod100) -1 ReDim EndByte(tmp) bDoLoop = InternetReadFileByte(hOpenUrl, EndByte(0), tmp +1, lNumberOfBytesRead) Put F1, , EndByte EndIf If hOpenUrl <>0Then InternetCloseHandle (hOpenUrl) If hOpen <>0Then InternetCloseHandle (hOpen) Close #1 FileDownload =True End Function
手上的一个VB项目(过程中发现,.Net果然是好啊),需要在一个ActiveX中实现HTTP下载功能,我是采用InternetreadFile这个API来实现,一开始的代码我是这么写的 Function Gethttpdownload(sUrl As String) As boolen Dim s As StringDim hOpen As LongDim hOpenUrl As