DownloadToMemory (for all OS's)

Share your advanced PureBasic knowledge/code with the community.
Heathen
Enthusiast
Enthusiast
Posts: 498
Joined: Tue Sep 27, 2005 6:54 pm
Location: At my pc coding..

Post by Heathen »

Code: Select all

Procedure.s DownloadToMemory(URL.s,maxbytes=0)
  Protected ServerID.l, Header.s, *Buffer = AllocateMemory(1000), String.s, Server.s, Path.s, i.l, DataLength.l,Bytes.l
 
  URL = RemoveString(URL,"http://",1)
  i = FindString(URL,"/",1)
  If i
    Server = Left(URL,i-1)
    Path = Right(URL,Len(URL)-i)
  Else
    Server = URL
  EndIf
 
  ServerID = OpenNetworkConnection(Server,80)
  If ServerID
    ;Header for the GET request
    Header = "GET /"+Path+" HTTP/1.1"+#CRLF$
    Header + "Host: "+Server+#CRLF$+#CRLF$
    SendNetworkData(ServerID,@Header,Len(Header)) ;Send the GET request
   
    Repeat ;Wait for it to start sending data
      Delay(2) ;No need to use 100% CPU while waiting
    Until NetworkClientEvent(ServerID) = #PB_NetworkEvent_Data
   
    Repeat ;Put all recieved data in a sring
      DataLength = ReceiveNetworkData(ServerID,*Buffer,1000)
      String + PeekS(*Buffer,DataLength)
      Bytes + DataLength
    Until DataLength = 0 or (maxbytes > 0 and bytes > maxbytes)
   
    FreeMemory(*Buffer) ;Don't need to use this memory anymore
   
    ;Cut of the header of the recieved data
    i = FindString(String,#CRLF$+#CRLF$,1)
    String = Mid(String,i+4,Len(String)-i-3)
    ProcedureReturn String
  EndIf
EndProcedure
I love Purebasic.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

There we have it, no need for panic! :P
I like logic, hence I dislike humans but love computers.
Heathen
Enthusiast
Enthusiast
Posts: 498
Joined: Tue Sep 27, 2005 6:54 pm
Location: At my pc coding..

Post by Heathen »

Joakim Christiansen wrote:There we have it, no need for panic! :P
:lol:
I love Purebasic.
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Post by ricardo »

And what about if i want to download a binary file (executable, mp3, sfw, etc) and keep it in memory (don't write it to disk)? How can do that?
CadeX
Enthusiast
Enthusiast
Posts: 124
Joined: Mon Oct 02, 2006 2:56 pm
Location: Australia
Contact:

Post by CadeX »

By simply... using the procedure and not writing it to a file? Read the script... It's not all just one procedure... there's stuff under it :)
Pro-Gamer, Programmer, Pro-Grammer
dracflamloc
Addict
Addict
Posts: 1648
Joined: Mon Sep 20, 2004 3:52 pm
Contact:

Post by dracflamloc »

you could just use my procedure. just instead of using a file, use a buffer the size of the maxlen and writedata to the large buffer.
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

Hmm, but the problem is, all the code posted so far only deal with strings and not binary data. Anybody have code for binary?
dracflamloc
Addict
Addict
Posts: 1648
Joined: Mon Sep 20, 2004 3:52 pm
Contact:

Post by dracflamloc »

If you look in my code you'll notice that it only deals with strings until the header information has been received. After that its just getting the raw data.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

CadeX wrote:Is there a way to start it at a specific place?
CadeX wrote:(Nevermind, my uberness worked it out. Thanks for the tip, DarkDragon)
Care about sharing some code? :D
I like logic, hence I dislike humans but love computers.
User avatar
thyphoon
Enthusiast
Enthusiast
Posts: 345
Joined: Sat Dec 25, 2004 2:37 pm

Post by thyphoon »

Hello

I try to mix some code to have a Download command who can use proxy and who are All Os Compatible
But i don't know how to erase header.
And i don't know why if i don't use Poxy After the header i have a value and at the end of the file a 0 ! Why ?

Code: Select all

Structure ProxyStructure
Adr.s
Port.l
login.s
password.s
EndStructure

Global Proxy.ProxyStructure
Proxy\Adr="xxxxxxxxxxxx" ;Url ou IP
Proxy\Port=xxxx ;Numero de port
Proxy\login="" ;Le login si il y en a un
Proxy\password="";Le mot de passe si il y en a un

InitNetwork()

Procedure.s MyDownload(Url.s,UseProxy=#False)
  Protected Server.s,Port.l,conc$,enc$,Header.s,Res.l,*Buffer = AllocateMemory(1024),String.s,DataLength.l
 
  ;Si j'ai un Proxy je dois me connecter au Proxy
  If UseProxy=#True
    Path.s=Url
    Server.s=Proxy\Adr
    Port.l=Proxy\Port
    conc$=Proxy\login+":"+Proxy\password
    OutputBuffer = AllocateMemory(Len(conc$)*4)
    Base64Encoder(@conc$,Len(conc$),OutputBuffer,Len(conc$)*4)
    enc$=PeekS(OutputBuffer)
 
  ;Si je n'ai pas de proxy on se connecte directement au serveur 
  ElseIf UseProxy=#False
    Port=80
    Path.s = Url
    Url.s = RemoveString(Url,"http://",1)
     i = FindString(Url,"/",1)
     If i
       Server = Left(Url,i-1)
     Else
       Server = Url
     EndIf
  EndIf
 
  ;Connection au Serveur
  ConnectionID = OpenNetworkConnection(Server, Port)
  If ConnectionID

    ;Creation de l'entête a envoyer
    Header ="GET "+Path+" HTTP/1.1"+#CRLF$
   
    If UseProxy=#True
      Header + "Proxy-Authorization: Basic "+enc$+#CRLF$
    Else
      Header + "Host: "+Server+#CRLF$+#CRLF$
    EndIf
 
    Header+#CRLF$
     Debug Header
    Res=SendNetworkData(ConnectionID,@Header,Len(Header))
 
    Delay(10)
    result = NetworkClientEvent(ConnectionID)
    Repeat ;On lit les données qui arrive
      DataLength = ReceiveNetworkData(ConnectionID,*Buffer,1024)
      String + PeekS(*Buffer,DataLength)
    Until DataLength<1024
    FreeMemory(*Buffer) ;POuff! Pu besoin on efface

    MessageRequester("Done!","Your Data" + Chr(13) + Chr(10) + Trim(String),0)
           
    ;C'est finit on se deconnecte
    CloseNetworkConnection(ConnectionID)
    ProcedureReturn String
  EndIf
EndProcedure

String.s=MyDownload("http://fr.yahoo.com",#True)
;  If CreateFile(0,"test.txt")
;    WriteData(0,@String,Len(String))
;    CloseFile(0)
;  EndIf
;  RunProgram("test.txt")

String.s=MyDownload("http://localhost/",#False)
can you help me ?

best regards

Thyphoon
CadeX
Enthusiast
Enthusiast
Posts: 124
Joined: Mon Oct 02, 2006 2:56 pm
Location: Australia
Contact:

Post by CadeX »

Joakim Christiansen wrote:
CadeX wrote:Is there a way to start it at a specific place?
CadeX wrote:(Nevermind, my uberness worked it out. Thanks for the tip, DarkDragon)
Care about sharing some code? :D
It honestly isn't hard, i don't have the code now seeing as it has been so long. Google is your friend.
Pro-Gamer, Programmer, Pro-Grammer
Post Reply