Tip: Tiny code to download a web page/file

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Tip: Tiny code to download a web page/file

Post by BackupUser »

Code updated for 5.20+ (same as ReceiveHTTPFile())

Restored from previous forum. Originally posted by PB.

You're not going to believe this one... I almost soiled myself when I read it!
Can be used to download ANY type of file from the Internet: web page, zip, etc.

Code: Select all

; Syntax: URLDownloadToFile_(0, WebURL$, LocalFile$, 0, 0)
r = URLDownloadToFile_(0, "http://www.purebasic.com/images/logopb.gif", "C:\logopb.gif", 0, 0)
If r <> 0 : MessageRequester("Error", "Couldn't download file...", 0) : EndIf
Obviously this function returns 0 on success, and non-zero on failure.
Works on all systems from Win 95 upwards -- WOOHOO!


PB - Registered PureBasic Coder

Edited by - PB on 23 April 2002 06:46:05
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Fangbeast.
You're not going to believe this one... I almost soiled myself when I read it!
Ewww, thanks for letting me read that with half the coffee still in my mouth!!!
(ROFL ROFL ROFL)

Fangles
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.
Ewww, thanks for letting me read that with half the coffee still in my mouth!!!
(ROFL ROFL ROFL)
Hehehe. It's a great phrase ("soiled myself") -- I got it from the movie,
"The Shawshank Redemption" when the old guy (the librarian) said it.


PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Paul.

This command is nice but is there any way to kill it after a certain amount of time if there is a problem? This command doesn't seem to time out automatically.

(I have to kill it manualy through the task manager)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.
is there any way to kill it after a certain amount of time
The example I posted was the command in its simplest form. The API docs for
it state that the last parameter (the second 0), can abort the command but I'm
not sure how yet to do it. Here's the docs for that parameter (if it makes any
sense to anyone else):

"Address of the caller's IBindStatusCallback interface. URLDownloadToFile calls
this interface's IBindStatusCallback::OnProgress method on a connection activity,
including the arrival of data. IBindStatusCallback::OnDataAvailable is never
called. Implementing IBindStatusCallback::OnProgress allows a caller to
implement a user interface or other progress monitoring functionality. It also
allows the download operation to be canceled by returning E_ABORT from the
IBindStatusCallback::OnProgress call.
"


PB - Registered PureBasic Coder

Edited by - PB on 18 January 2002 04:45:52
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Paul.

Yeah, I looked it up on MSDN and that last bit made no sense to me :(

Oh well... anyone else figure it out?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.
anyone else figure it out?
The only approach I can think of (at the moment) is to call the API with a
thread, and then kill the thread and/or the download process after a timeout.
Obviously this has other problems, such as working out what the process ID of
the API call was... anyone?


PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

Hum, this method use an Object oriented class, which means you have to use an interface to access the callback. Should look like this (willn't work, just to explain):

Code: Select all

Structure IBindStatusCallback
  *OnProgress
  *Function2
  *Function3
EndStructure

Object1.IBindStatusCallback
Object1.OnPogress = @OnProgress()
Object1.Function2 = @Function2()

This is basically how OO API work. This why it's slower: 2 jumps instead of one, but it's easier to replace the functions (pointer only) and that's why you can do 'extends', 'polymorphism', etc... BTW, DirectX use this system, so if you want to use it in PB, you must define the interface. I will post an example on it when I found time.

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by TronDoc.

did anybody perfect this tiny download?
I could sure use a tiny upload for a non-pc-wise
friend so they could send two specific files to
a specific ftp. They need to send update files
to a website. ???

Joe B.


elecTRONics DOCtor
{registeredPB}P150 32Mb w98/DOS/Linux NO DirX NO IE :wink:
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tranquil.

Seems that an API Guru need to take a look on it. Ring? Something special for you? :)

Mike

Tranquilizer/ Secretly!
Registred PureBasic User
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

PureBasic OOP Guru needed here

You need to read something about

Code: Select all

"URL Moniker",
"IBindStatusCallback" (IBindStatusCallback::OnProgress),
"RegisterBindStatusCallback",
"BindToStorage" and "BindToObject"
Before you read about this you should know
how COM/ActiveX/OLE work on LowLevel...

Have fun,
...Danilo

(registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Don.
anyone else figure it out?

The only approach I can think of (at the moment) is to call the API with a
thread, and then kill the thread and/or the download process after a timeout.
Obviously this has other problems, such as working out what the process ID of
the API call was... anyone?


PB - Registered PureBasic Coder
The easiest way is not to use the IBindStatusCallback interface but just to use PB threads. For example, create a timeout procedure something like this:

Code: Select all

Procedure Timeout(millisecs)      ; general timeout procedure
  s = millisecs/10
  i=0
  While i  Tiny Web Browser&Forum_Title=Tricks %27n%27 Tips

If anyone's interested, I'm working on a web browser completely written in PureBasic, with no dependency on Internet Explorer related DLLs or any third party DLLs. It only uses the rich edit control and lots of API calls . It's currently about 1500 lines long and is quite different from the code in the link above (I've learnt a lot more about HTTP and the Windows API since I wrote that one!)

Don
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> This is the easiest way to do it, but it's not perfect because "Repeat : Until
> TimedOut Or GotData" is a loop which causes the program to run at high CPU.

You can prevent a loop from becoming a CPU hog by adding Sleep_(1) in the middle
of it, like this: Repeat : Sleep_(1) : Until TimedOut Or GotData.

This causes the loop to sleep for 1 millisecond every time it loops, and thus
the CPU load is vastly reduced. The only drawback, if considered one, is that
the 1 millisecond delay adds to the timeout, ever-so-slightly.


PB - Registered PureBasic Coder
Post Reply