Download file from http server
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Berikco.
Hi all,
Here an example of http download.
With: Animation, Counter, Progressbar and CancelButton.
Http header in debugwindow.
Added Download Rate
You can use proxy server to, just set server to 'yourproxy.yourprovider' and set port correct, ex. '8080'
2002/25/09 new version below
http://www.benny.zeb.be
Hi all,
Here an example of http download.
With: Animation, Counter, Progressbar and CancelButton.
Http header in debugwindow.
Added Download Rate
You can use proxy server to, just set server to 'yourproxy.yourprovider' and set port correct, ex. '8080'
2002/25/09 new version below
http://www.benny.zeb.be
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Berikco.
Is the download speed correct?
I could not test good on my cable connection with a file of 1MB, takes only two seconds.
But i ceck 10 times a second, the final display of the download speed should be correct, the timer resolution is high enough.
Regards,
Berikco
http://www.benny.zeb.be
Hi Justin,Originally posted by Justin
Great example, i like the system to display the download speed, thanks for sharing![]()
Is the download speed correct?
I could not test good on my cable connection with a file of 1MB, takes only two seconds.
But i ceck 10 times a second, the final display of the download speed should be correct, the timer resolution is high enough.
Regards,
Berikco
http://www.benny.zeb.be
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Berikco.
Strange, i tested with a file of 7360196 bytes, 15 secs to download.
7360196/1024=7187.69 KB
7187.69/15=479.1 , exactly what the final displays shows, the average speed is correct.
I noticed the speed at download is a little to low, between 420 and 450 KB/s
So i should take a look at that routine again.
Regards,
Berikco
http://www.benny.zeb.be
Strange, i tested with a file of 7360196 bytes, 15 secs to download.
7360196/1024=7187.69 KB
7187.69/15=479.1 , exactly what the final displays shows, the average speed is correct.
I noticed the speed at download is a little to low, between 420 and 450 KB/s
So i should take a look at that routine again.
Regards,
Berikco
http://www.benny.zeb.be
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Justin.
I think is because the wm_timer is a low priority message, you can't ensure that the window will receive it exactly every 100 ms, so if you check it so many times you are adding some kind of error.
Setting the timer to 1 sec and making the appropiate changes to your code showed a correct average (17).
Also note that you have to return 0 if you process this message.
I think is because the wm_timer is a low priority message, you can't ensure that the window will receive it exactly every 100 ms, so if you check it so many times you are adding some kind of error.
Setting the timer to 1 sec and making the appropiate changes to your code showed a correct average (17).
Also note that you have to return 0 if you process this message.
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Berikco.
Thats write justin, in noticed that the error rate is much bigger on slower connections, and does not show so much on cable.
Thanks for testing this.
Regards,
Berikco
http://www.benny.zeb.be
Thats write justin, in noticed that the error rate is much bigger on slower connections, and does not show so much on cable.
Thanks for testing this.
Regards,
Berikco
http://www.benny.zeb.be
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Berikco.
Regards,
Berikco
http://www.benny.zeb.be
Changed, and put the counter in the timer loop, not jumping up/down so much, very smooth.Originally posted by Justin
I think is because the wm_timer is a low priority message, you can't ensure that the window will receive it exactly every 100 ms, so if you check it so many times you are adding some kind of error.
Code: Select all
; HTTP file download
; 09/22/2002
; By Berikco
; v1.1
;
If InitNetwork() = 0
MessageRequester("Error", "Can't initialize the network !", 0)
End
EndIf
#bufferlengte=10240
Global buffer
buffer = AllocateMemory(#1,#bufferlengte , 0)
Global buf$
Global EOL$
EOL$ = Chr(13)+Chr(10)
Global filenaam$
filenaam$="pb.zip"
server$="users.telenet.be"
url$="[url]http://users.telenet.be/sels-alamo/[/url]"
;
Port = 80
Global size
Global oldsize
Global rate
Global ratetel
Global buf$
Global header
Global startReceive
Global hwnd
Global ConnectionID
Global filesize
Global timeout
Global aniwin
Global h
Global einde
h=LoadLibrary_("Shell32.dll")
;
Procedure incoming(result)
b.b=0
b$=""
rest=0
Select header
Case 0
sendmessage_(AniWin,#ACM_OPEN,h,160)
i=0
Repeat
If PeekB(buffer+i)=13 And PeekB(buffer+i+1)=10 And PeekB(buffer+i+2)=13 And PeekB(buffer+i+3)=10
b$=Space(i+4)
CopyMemory(buffer,@b$,i+4)
rest=i
i=result-1
EndIf
i+1
Until i=result
;
Repeat
;
If Left(b$,2)=EOL$
If startreceive=1
b$=Mid(b$,3,Len(b$)-3)
If CreateFile(1,filenaam$)
WriteData(buffer+rest+4, result-rest-4)
header=1
size=result-rest-4
Else
header=2
EndIf
EndIf
Else
search=FindString(b$, EOL$ , 1)
If search>0
l$=Left(b$,search-1)
b$=Mid(b$,search+2,Len(b$))
pos=FindString(l$,"200 " , 1)
If pos
startreceive=1 ; ok
Else
pos=FindString(l$,"404 " , 1)
If pos
;error 404 not founf
Else
pos=FindString(l$,"Content-Length:" , 1)
If pos
pos=FindString(l$," " , 1)
filesize=Val(Mid(l$,pos+1,Len(l$)))
EndIf
EndIf
EndIf
Debug l$
Else
l$=""
EndIf
EndIf ;
Until search=0
Case 1
timeout=0
WriteData(buffer, result)
size+result
stap=100*size/filesize
SetGadgetState(2, stap)
If filesize=size
Debug "File Received"
header=2
Debug Str(size)+" of "+Str(filesize)+" bytes"
EndIf
Case 2
CloseFile(1)
CloseNetworkConnection(ConnectionID)
Debug "Connection Closed"
header=3
DestroyWindow_(AniWin)
einde=1
EndSelect
EndProcedure
;
Procedure MyWindowCallback(WindowID, Message, wParam, lParam)
Result = #PB_ProcessPureBasicEvents
Select WindowID
Case hwnd
Select message
Case #WM_TIMER
result=0
Select wparam
Case 1 ; timer
result=ReceiveNetworkData(ConnectionID, buffer, #bufferlengte)
incoming(result)
Case 2 ; timeout
timeout+1
If timeout>10
CloseFile(1)
header=2
Debug "Timeout"
EndIf
Case 3
ratetel+500
If ratetel>0
rate=size/ratetel
EndIf
SetGadgetText(3,"Download speed..."+Str(rate)+" KB/s. Time "+Str(ratetel/1000)+" s.")
EndSelect
EndSelect
EndSelect
ProcedureReturn Result
EndProcedure
;
hwnd=OpenWindow(0,100,450,335,160,#PB_Window_SystemMenu,"Downloading "+filenaam$+" from "+server$)
;
If hwnd
If CreateGadgetList(WindowID())
ButtonGadget(1, 250, 128, 72, 20, "Cancel")
ProgressBarGadget(2, 10, 100, 313, 20, 0, 100)
TextGadget(3, 20, 75, 280, 20, "")
;
AniWin=CreateWindowEx_(0,"SysAnimate32","",#ACS_AUTOPLAY|#ACS_CENTER|#ACS_TRANSPARENT|#WS_CHILD|#WS_VISIBLE|#WS_CLIPCHILDREN|#WS_CLIPSIBLINGS,25,10,280,50, hwnd,0,GetModuleHandle_(0),0)
;
ConnectionID = OpenNetworkConnection(server$, Port)
;
If ConnectionID
SendNetworkString(ConnectionID, "GET "+URL$+filenaam$+" HTTP/1.0"+eol$)
SendNetworkString(ConnectionID, eol$)
;
SetWindowCallback(@MyWindowCallback())
; -------------- timers ----------------
SetTimer_(hWnd,1,20,0) ; 20 milisecond timer
SetTimer_(hWnd,2,1000,0) ; 1 sec timer
SetTimer_(hWnd,3,500,0) ; 500 msec timer
;
Repeat
EventID=WaitWindowEvent()
If EventID = #PB_EventGadget
Select EventGadgetID()
Case 1
header=2
EndSelect
EndIf
Until einde=1
EndIf
EndIf
EndIf
FreeLibrary_(h)
Delay(4000)
;
End
Regards,
Berikco
http://www.benny.zeb.be
If you look at the windows API there are a few useful functions..
URLDownloadToFile()
InternetOpen()
InternetOpenURL()
InternetReadFile()
... and probably a few others..
Hope that helps a bit.
URLDownloadToFile()
InternetOpen()
InternetOpenURL()
InternetReadFile()
... and probably a few others..
Hope that helps a bit.
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net