HTTP Download

Just starting out? Need help? Post your questions and find answers here.
sabre
User
User
Posts: 11
Joined: Mon Apr 26, 2004 11:41 pm

HTTP Download

Post by sabre »

Hi -

I am trying to use the following code for a project at work that Berico posted in viewtopic.php?t=8591.

The code is:

Code: Select all

; HTTP file download 
; 
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$="PureBasic_Update_3.81_Beta_1.zip" 
server$="www.purebasic.com" 
url$="http://www.purebasic.com/" 

;http://www.purebasic.com/PureBasic_Update_3.81_Beta_1.zip 
; 
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 
            SetGadgetText(4,"Received "+Str(size)+" of "+Str(filesize)+" bytes") 
          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 
    SetGadgetText(4,"Received "+Str(size)+" of "+Str(filesize)+" bytes") 
    stap=100*size/filesize 
    SetGadgetState(2, stap) 
    If filesize=size 
      Debug "File Received" 
      header=2 
      Debug Str(size)+" of "+Str(filesize)+" bytes" 
      killTimer_(hWnd,1) ; 20 milisecond timer 
      killTimer_(hWnd,2) ; 1 sec timer 
      killtimer_(hWnd,3) ; 500 msec timer    
      CloseFile(1) 
      CloseNetworkConnection(ConnectionID) 
      Debug "Connection Closed" 
      DestroyWindow_(AniWin) 
      einde=1 
    EndIf 
  EndSelect 
EndProcedure 
; 
Procedure MyWindowCallback(WindowID, Message, wParam, lParam) 
  Result = #PB_ProcessPureBasicEvents 
  Select WindowID 
  Case hwnd 
    Select message 
    Case #WM_TIMER 
      result=1 
      Select wparam 
      Case 1 ; timer 
        If NetworkClientEvent(ConnectionID) 
          result=ReceiveNetworkData(ConnectionID, buffer, #bufferlengte) 
          incoming(result) 
        EndIf 
      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, 80,  280, 15, "") 
    TextGadget(4, 20, 65,  280, 15, "") 
    ; 
    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,1,0) ; 1 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
When I try to compile it fails with the following:

Line 10: A Constant Name need to start with a character (a-z or _)

The line it fails on is: buffer = AllocateMemory(#1,#bufferlengte , 0)

Any Ideas how to fix this? Thanks in advance
MadMax
Enthusiast
Enthusiast
Posts: 237
Joined: Mon Oct 06, 2003 11:56 am

Post by MadMax »

The line it fails on is: buffer = AllocateMemory(#1,#bufferlengte , 0)
the comand has changed recently now it is:

*MemoryID = AllocateMemory(Size)

Hope this helps
sabre
User
User
Posts: 11
Joined: Mon Apr 26, 2004 11:41 pm

Post by sabre »

Thanks how would you know how much memory to allocate? Files will range from 1mb to as much as 700mb.

buffer=allocatememory(????)

Thanks so much sorry I am a total newbie to Purebasic but have been using VB for a little while.

Also I assume this is open code and I can use it as I wish in my commerical projects?

Thanks to everyone sorry for the silly ? just trying to learn.
MadMax
Enthusiast
Enthusiast
Posts: 237
Joined: Mon Oct 06, 2003 11:56 am

Post by MadMax »

My guess is #bufferlengte is the size :roll:
sabre
User
User
Posts: 11
Joined: Mon Apr 26, 2004 11:41 pm

Post by sabre »

Thanks so much, you been a great help everything is working fine now, the only other question I have is the code downloads the file to the same folder where it runs from; is there anyway to specify a different path such as "c:\myfiles" for example to download to.

This is the last ? I had regarding this and I really do appreciate all the help
thanks so much
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post by TerryHough »

sabre wrote: the only other question I have is the code downloads the file to the same folder where it runs from; is there anyway to specify a different path such as "c:\myfiles" for example to download to.

Code: Select all

If CreateFile(1,filenaam$)
Change the line shown above to

Code: Select all

 If CreateFile(1,"C:\MyFiles"+filenaam$)
Make sure the "C:\MyFiles" path exists. You might want to use a
PathRequester to determine the output path at the beginning of the
program

Code: Select all

PathName$ = PathRequester("Download Path", "C:")
and then use

Code: Select all

 If CreateFile(1,PathName$+filenaam$)
Hope this helps.
sabre
User
User
Posts: 11
Joined: Mon Apr 26, 2004 11:41 pm

Post by sabre »

Thanks

I appreciate all the help you guys have given me this forum is great, Now I need to start learning the language.

Regards
sabre
User
User
Posts: 11
Joined: Mon Apr 26, 2004 11:41 pm

Post by sabre »

Oh btw Purebasic is really cool as I am sure you guys already know again thanks so much :D
Post Reply