c'est une mise a jour personnel a la fonction de PB "ReceiveHTTPFile".
Fonctionnalités:
+ utilisation de la méthode "multi segment" connu sur le logiciel "IDM"
+ télécharger un fichier avec le progresse
+ Mettre en pause le téléchargement
+ Stopper le téléchargement
''
Prochain mise a jour:
-un thread pour chaque connexion et pour chaque téléchargement
-support pour les fichiers torrent et Streaming direct des vidéos
-support pour les vidéos youtube
ReceiveHTTPFile_Ex.pbi
Code : Tout sélectionner
;//////////////////////////////////////////////////
;ReceiveHTTPFile_Ex() version 0.2 beta test
;Author Celtic88(c)
;//////////////////////////////////////////////////
EnableExplicit
#INTERNET_SCHEME_HTTPS_WinHttp = 2
#WINHTTP_ACCESS_TYPE_DEFAULT_PROXY = 0
#WINHTTP_OPTION_CONTEXT_VALUE=45
#WINHTTP_FLAG_ESCAPE_DISABLE = $00000040
#WINHTTP_FLAG_SECURE = $00800000
#WINHTTP_QUERY_CONTENT_RANGE = 53
;#WINHTTP_QUERY_CONTENT_DISPOSITION=47
#WINHTTP_ACCESS_TYPE_NAMED_PROXY = 3
#WINHTTP_QUERY_CONTENT_LENGTH = 5
Prototype ReceiveHTTPFile_Info_CallBack(iTotalReceivedBytes.l, iDownloadSize.l,User)
Prototype WinHttpCrackUrl(a.p-unicode,b.l,c.l,lpUrlC)
Prototype WinHttpSetOption(a,b.l,Setting,d.l)
Prototype WinHttpCloseHandle(a)
Prototype WinHttpOpen(a.p-unicode,b.l,c.p-unicode,d.p-unicode,e.l)
Prototype WinHttpConnect(a,b.p-unicode,c.l,d.l)
Prototype WinHttpOpenRequest(a,b.p-unicode,c.p-unicode,d.p-unicode,e.p-unicode,pAc,g.l)
Prototype WinHttpSendRequest(a,b.p-unicode,c.l,pOpt,e.l,f.l,g)
Prototype WinHttpReceiveResponse(a,b)
Prototype WinHttpQueryHeaders(a,b.l,c.p-unicode,d,e,f);<=====
Prototype WinHttpQueryDataAvailable(a,b)
Prototype WinHttpReadData(a,b,c.l,d)
Prototype WinHttpSetCredentials(a,b.l,c.l,d.p-unicode,e.p-unicode,f)
Global winhttp_dll=OpenLibrary(#PB_Any,"winhttp.dll")
Global WinHttpCrackUrl_.WinHttpCrackUrl=GetFunction(winhttp_dll,"WinHttpCrackUrl")
Global WinHttpSetCredentials_.WinHttpSetCredentials=GetFunction(winhttp_dll,"WinHttpSetCredentials")
Global WinHttpQueryDataAvailable_.WinHttpQueryDataAvailable=GetFunction(winhttp_dll,"WinHttpQueryDataAvailable")
Global WinHttpReadData_.WinHttpReadData=GetFunction(winhttp_dll,"WinHttpReadData")
Global WinHttpReceiveResponse_.WinHttpReceiveResponse=GetFunction(winhttp_dll,"WinHttpReceiveResponse")
Global WinHttpQueryHeaders_.WinHttpQueryHeaders=GetFunction(winhttp_dll,"WinHttpQueryHeaders")
Global WinHttpSendRequest_.WinHttpSendRequest=GetFunction(winhttp_dll,"WinHttpSendRequest")
Global WinHttpSetOption_.WinHttpSetOption=GetFunction(winhttp_dll,"WinHttpSetOption")
Global WinHttpOpen_.WinHttpOpen=GetFunction(winhttp_dll,"WinHttpOpen")
Global WinHttpCloseHandle_.WinHttpCloseHandle=GetFunction(winhttp_dll,"WinHttpCloseHandle")
Global WinHttpConnect_.WinHttpConnect=GetFunction(winhttp_dll,"WinHttpConnect")
Global WinHttpOpenRequest_.WinHttpOpenRequest=GetFunction(winhttp_dll,"WinHttpOpenRequest")
Structure ReceiveHTTPFile_ExSt
REQUEST_HANDLE.i
RECEIVED_BYTES.l
DOWNLOAD_OFFSET.l
BYTES_TO_DOWNLOAD.l
EndStructure
; Description ...: Download a single file by splitting it in segments
; Parameters ....: url - Url of the file to download
; Savename - Local filename to download to
; iNumberOfConnections - Number of simultaneous connections
; ProcedureCallBack - An Application defined callback function called during Downloading File.
; User - An application-defined value to be passed to the callback function.
Procedure ReceiveHTTPFile_Ex(url.s,Savename.s="",iNumberOfConnections.b=1,ProcedureCallBack=0,User=0)
Protected ReceiveHTTPFile_Info_CallBack_.ReceiveHTTPFile_Info_CallBack=ProcedureCallBack
Protected DownloadError.b =1
Protected lpUrlComponents.URL_COMPONENTS
lpUrlComponents\dwStructSize=SizeOf(URL_COMPONENTS)
Protected HttpHostName.s=Space(Len(url)*2)
lpUrlComponents\dwHostNameLength = Len(url)*2
lpUrlComponents\lpszHostName=@HttpHostName
Protected HttpFilePath.s=Space(Len(url)*2)
lpUrlComponents\dwUrlPathLength = Len(url)*2
lpUrlComponents\lpszUrlPath=@HttpFilePath
Protected HttpFilePathExtrainfo.s=Space(Len(url)*2)
lpUrlComponents\dwExtraInfoLength = Len(url)*2
lpUrlComponents\lpszExtraInfo=@HttpFilePathExtrainfo
If Not WinHttpCrackUrl_(url,Len(url),#ICU_DECODE,@lpUrlComponents)
ProcedureReturn -1
EndIf
Protected HttpPort= lpUrlComponents\nPort
Protected HttpScheme= lpUrlComponents\nScheme
HttpHostName=PeekS(@HttpHostName,-1,#PB_Unicode)
HttpFilePath=PeekS(@HttpFilePath,-1,#PB_Unicode)+PeekS(@HttpFilePathExtrainfo,-1,#PB_Unicode)
If Savename = #NUL$
Savename=StringField(HttpFilePath,CountString(HttpFilePath,"/")+1,"/")
EndIf
Debug HttpHostName
Debug HttpFilePath
Debug HttpScheme
Debug Savename
Debug "------------------------"
Protected hSession = WinHttpOpen_("Mozilla/5.0 (WinHTTP/5.1) like Gecko", #WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, "","", 0)
If Not hSession
ProcedureReturn -2
EndIf
Protected hConnect =WinHttpConnect_(hSession,HttpHostName,HttpPort,0)
If hConnect
WinHttpSetOption_(hConnect,#WINHTTP_OPTION_CONTEXT_VALUE,@HttpScheme,SizeOf(Integer))
Protected iFlag=0
If HttpScheme = #INTERNET_SCHEME_HTTPS_WinHttp
iFlag=#WINHTTP_FLAG_SECURE
EndIf
Protected hRequest=WinHttpOpenRequest_(hConnect,"GET",HttpFilePath,"HTTP/1.1","",0,iFlag)
If hRequest
WinHttpSendRequest_(hRequest,"Range: bytes=0-0",0,0,0,0,0)
WinHttpReceiveResponse_(hRequest,0)
Protected iindex.l,rqFilesize.s=Space(200),msize.l=200,iDownloadfileSize
WinHttpQueryHeaders_(hRequest,#WINHTTP_QUERY_CONTENT_RANGE,"",@rqFilesize,@msize,@iindex)
Debug rqFilesize
;Debug GetLastError_()
WinHttpCloseHandle_(hRequest)
rqFilesize=PeekS(@rqFilesize,-1,#PB_Unicode)
iDownloadfileSize=Val(StringField(rqFilesize,2,"bytes 0-0/"))
If iDownloadfileSize
Debug iDownloadfileSize
Protected iDownloadSizepercon=iDownloadfileSize/iNumberOfConnections
Protected iLastByteToDownload,ok,Dowreq.s
Dim ReceiveHTTPFile_Exls.ReceiveHTTPFile_ExSt(iNumberOfConnections-1)
For ok=0 To iNumberOfConnections-1
ReceiveHTTPFile_Exls(ok)\REQUEST_HANDLE=WinHttpOpenRequest_(hConnect,"GET",HttpFilePath,"HTTP/1.1","",0,iFlag)
ReceiveHTTPFile_Exls(ok)\DOWNLOAD_OFFSET=ok*iDownloadSizepercon
ReceiveHTTPFile_Exls(ok)\BYTES_TO_DOWNLOAD=iDownloadSizepercon
iLastByteToDownload=ReceiveHTTPFile_Exls(ok)\DOWNLOAD_OFFSET+iDownloadSizepercon-1
If ok=iNumberOfConnections-1
ReceiveHTTPFile_Exls(ok)\BYTES_TO_DOWNLOAD=iDownloadSizepercon+Mod(iDownloadfileSize,iNumberOfConnections)
iLastByteToDownload=iDownloadfileSize-1
EndIf
Dowreq=Str(ReceiveHTTPFile_Exls(ok)\DOWNLOAD_OFFSET)+"-"+Str(iLastByteToDownload)
WinHttpSendRequest_(ReceiveHTTPFile_Exls(ok)\REQUEST_HANDLE,"Range: bytes="+Dowreq,0,0,0,0,0)
WinHttpReceiveResponse_(ReceiveHTTPFile_Exls(ok)\REQUEST_HANDLE,0)
Next
Protected wrDataFile=1024*1024
Protected iTotalReceivedBytes,ReciveSize,*mem=AllocateMemory(wrDataFile),ReciveSizetp
Protected CreateFile=CreateFile(#PB_Any,Savename)
If CreateFile
For ok=0 To iDownloadfileSize Step 1024*1024
If iDownloadfileSize-ok < wrDataFile
wrDataFile=iDownloadfileSize-Loc(CreateFile)
EndIf
WriteData(CreateFile,*mem,wrDataFile)
Next
FreeMemory(*mem)
FlushFileBuffers(CreateFile)
FileSeek(CreateFile,0)
Repeat
For ok=0 To iNumberOfConnections-1
If ReceiveHTTPFile_Info_CallBack_
Repeat
Select ReceiveHTTPFile_Info_CallBack_(iTotalReceivedBytes,iDownloadfileSize,User)
Case 2
Delay(100)
Continue
Case 1
DownloadError =0
Break 2
EndSelect
Break
ForEver
EndIf
If ReceiveHTTPFile_Exls(ok)\BYTES_TO_DOWNLOAD=ReceiveHTTPFile_Exls(ok)\RECEIVED_BYTES
Continue
EndIf
ReciveSize=0
If WinHttpQueryDataAvailable_(ReceiveHTTPFile_Exls(ok)\REQUEST_HANDLE,@ReciveSize)
iTotalReceivedBytes+ReciveSize
If ReciveSize
*mem=AllocateMemory(ReciveSize)
If Not *mem
DownloadError -8
Break
EndIf
ReciveSizetp=0
If Not WinHttpReadData_(ReceiveHTTPFile_Exls(ok)\REQUEST_HANDLE,*mem,ReciveSize,@ReciveSizetp)
FreeMemory(*mem)
Debug "-10 Error Code ====> " + Str(GetLastError_())
DownloadError -10
Break
EndIf
FileSeek(CreateFile,ReceiveHTTPFile_Exls(ok)\DOWNLOAD_OFFSET+ReceiveHTTPFile_Exls(ok)\RECEIVED_BYTES)
WriteData(CreateFile,*mem,ReciveSizetp)
FreeMemory(*mem)
Else
DownloadError -9
Break
EndIf
ReceiveHTTPFile_Exls(ok)\RECEIVED_BYTES+ReciveSizetp
If ReceiveHTTPFile_Exls(ok)\BYTES_TO_DOWNLOAD=ReceiveHTTPFile_Exls(ok)\RECEIVED_BYTES
WinHttpCloseHandle_(ReceiveHTTPFile_Exls(ok)\REQUEST_HANDLE)
ReceiveHTTPFile_Exls(ok)\REQUEST_HANDLE=0
EndIf
Else
Debug "-7 Error Code ====> " + Str(GetLastError_())
DownloadError -7
Break
EndIf
Next
If DownloadError < 1
For ok=0 To iNumberOfConnections-1
If ReceiveHTTPFile_Exls(ok)\REQUEST_HANDLE
WinHttpCloseHandle_(ReceiveHTTPFile_Exls(ok)\REQUEST_HANDLE)
EndIf
Next
Break
EndIf
If iTotalReceivedBytes = iDownloadfileSize
If ReceiveHTTPFile_Info_CallBack_
ReceiveHTTPFile_Info_CallBack_(iTotalReceivedBytes,iDownloadfileSize,User)
EndIf
Break
EndIf
ForEver
FreeArray(ReceiveHTTPFile_Exls())
CloseFile(CreateFile)
Else
DownloadError=-6
EndIf
Else
DownloadError=-5
EndIf
Else
DownloadError=-4
EndIf
WinHttpCloseHandle_(hConnect)
Else
DownloadError=-3
EndIf
WinHttpCloseHandle_(hSession)
ProcedureReturn DownloadError
EndProcedure
Code : Tout sélectionner
Procedure _DownloadFileCallBack(iTotalReceivedBytes.l, iDownloadSize.l,User)
Debug Str(iTotalReceivedBytes) + " Of " + Str(iDownloadSize) + " Dow iD " + Str(User)
; ProcedureReturn 1 ; <=== pour arrêter le téléchargement
; ProcedureReturn 2 ; <=== pour Mettre en pause
ProcedureReturn 0
EndProcedure
Debug ReceiveHTTPFile_Ex("http://ak3.picdn.net/shutterstock/videos/378211/thumb/1.jpg","",5,@_DownloadFileCallBack(),1006)