You are correct. ReceiveHTTPFile is too slow for your planned use. I don't know why, but I believe it is waiting for a timeout to be received..
I have a program using ReceiveHTTPFile that takes 10.031 seconds.
The exact same program using the Windows API URLDownloadToFile_() takes .656 seconds.
And slightly modified using ts-soft's ReceiveHTTPMemory() takes .187 seconds.
The example program that follows uses the Yahoo Finance API to download CSV info for a selected set of symbols, processes the CSV info, formats, and displays it. You should be able to modify it for your use.
The desired symbols are embedded in the URL created at line 80. Substitute your own stock symbols. That is followed by the switches for the Yahoo Finance API.
You can see an explanation of those switches by running the program and clicking on the Yahoo Finance API tab.
Code: Select all
; Example by terryhough for PB Forum - Using Yahoo Finance API to grab quotes from returned CSV file
Procedure.i ReceiveHTTPMemory(URL.s, BufferSize = 4096, Timeout = 5000)
;====================== ReceiveHTTPMemory ============================
; Author: Thomas Schulz (ts-soft)
; Date: January 13, 2011
; Target OS: All
; Target Compiler: Requires PureBasic 4.xx
;=====================================================================
Protected Connection, Time, Time2, Event, Size, Size2, SizeAll, pos
Protected.s Server
Protected *Mem, *Buffer, *Mem2
Size = 1
If LCase(Left(URL, 7)) <> "http://" : URL = "http://" + URL : EndIf
Server = GetURLPart(URL, #PB_URL_Site)
If Not Server : ProcedureReturn #False : EndIf
Connection = OpenNetworkConnection(Server, 80, #PB_Network_TCP)
If Not Connection : ProcedureReturn #False : EndIf
If BufferSize <= 0 : BufferSize = 4096 : EndIf
*Buffer = AllocateMemory(BufferSize)
If Not *Buffer : ProcedureReturn #False : EndIf
SendNetworkString(Connection, "GET " + URL + " HTTP/1.0" + #CRLF$ + #CRLF$)
Time = ElapsedMilliseconds()
Repeat
Event = NetworkClientEvent(Connection)
If Event = #PB_NetworkEvent_Data
Repeat
Size = ReceiveNetworkData(Connection, *Buffer, BufferSize)
If Size > 0
Time = ElapsedMilliseconds()
SizeAll + Size
*Mem = ReAllocateMemory(*Mem, SizeAll)
If *Mem
CopyMemory(*Buffer, *Mem + (SizeAll - Size), Size)
Else
CloseNetworkConnection(Connection)
FreeMemory(*Buffer)
ProcedureReturn #False
EndIf
EndIf
Until Size <= 0
EndIf
Time2 = ElapsedMilliseconds() - Time
Until Time2 > Timeout Or Size <= 0
CloseNetworkConnection(Connection)
FreeMemory(*Buffer)
If Time2 > Timeout
If *Mem : FreeMemory(*Mem) : EndIf
ProcedureReturn #False
EndIf
pos = FindString(PeekS(*mem, -1, #PB_UTF8), #CRLF$ + #CRLF$, 1) - 1
pos = Len(#CRLF$ + #CRLF$) + pos
Size2 = MemorySize(*Mem) - pos
*Mem2 = AllocateMemory(Size2)
If *Mem2
CopyMemory(*Mem + pos, *Mem2, Size2)
FreeMemory(*Mem)
ProcedureReturn *Mem2
EndIf
FreeMemory(*Mem)
ProcedureReturn #False
EndProcedure
lvc.LV_COLUMN ; set up structure for global use to format ListIconGadget columns
lvc\mask = #LVCF_FMT
lvc\fmt = #LVCFMT_RIGHT
DownloadFilename.s = "C:\MyStocks.csv"
InitNetwork() ; Necessary in order to use PB's network function later (ReceiveHTTPMemory)
hWnd.l = OpenWindow(0,0,0,800,600,"Stock History",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
If hWnd
PanelGadget(999,10,10,780,580)
AddGadgetItem(999,-1,"Quote List")
URL.s = "http://download.finance.yahoo.com/d/quotes.csv?s=C+GLAD+HIMX+INSP+INTU+HRB+JTX+PODD+SILU+WFC&f=snoghl1b2b3p2"
Starttime.l = ElapsedMilliseconds() ; Start the processing timer
; ---- Use ts-soft's ReceiveHTTPMemory procedure to download the file ----------------
*mem = ReceiveHTTPMemory(URL) ; fastest method
; ----------------------------------------------------------------------
; Got the file, now process it.
If *mem
File$ = PeekS(*mem)
FreeMemory(*mem)
ListIconGadget(i,10,10,760,540,"Symbol",100)
AddGadgetColumn(i, 1, "Name", 150)
AddGadgetColumn(i, 2, "Open", 60)
AddGadgetColumn(i, 3, "Low", 60)
AddGadgetColumn(i, 4, "High", 60)
AddGadgetColumn(i, 5, "Last", 60)
AddGadgetColumn(i, 6, "Ask", 60)
AddGadgetColumn(i, 7, "Bid", 60)
AddGadgetColumn(i, 8, "% Chg", 60)
LIG = GadgetID(i)
SendMessage_(LIG, #LVM_SETCOLUMN, 2, @lvc) ; Right justify column 2
SendMessage_(LIG, #LVM_SETCOLUMN, 3, @lvc) ; Right justify column 3
SendMessage_(LIG, #LVM_SETCOLUMN, 4, @lvc) ; Right justify column 4
SendMessage_(LIG, #LVM_SETCOLUMN, 5, @lvc) ; Right justify column 5
SendMessage_(LIG, #LVM_SETCOLUMN, 6, @lvc) ; Right justify column 6
SendMessage_(LIG, #LVM_SETCOLUMN, 7, @lvc) ; Right justify column 7
SendMessage_(LIG, #LVM_SETCOLUMN, 8, @lvc) ; Right justify column 8
While WindowEvent():Wend ; force window to repaint
LockWindowUpdate_(hWnd) ; speed - lock window form updating until completely built
j = 1
Repeat
DayData$ = RTrim(StringField(File$, j, #LF$), #CR$)
j + 1
DayData$ = RemoveString(DayData$,Chr(34))
; ---- Remove any commas from company name field --------------------
Result = CountString(DayData$, ",")
If result > 8
pos = FindString(DayData$,",",1)
unwanted = FindString(DayData$,",",pos+1)
DayData$ = RemoveString(DayData$, ",", #PB_String_NoCase, unwanted, 1)
EndIf
; -------------------------------------------------------------------
Line$ = StringField(DayData$,1,",") + #LF$
Line$ + StringField(DayData$,2,",") + #LF$
Line$ + StringField(DayData$,3,",") + #LF$
Line$ + StringField(DayData$,4,",") + #LF$
Line$ + StringField(DayData$,5,",") + #LF$
Line$ + StringField(DayData$,6,",") + #LF$
Line$ + StringField(DayData$,7,",") + #LF$
Line$ + StringField(DayData$,8,",") + #LF$
Line$ + StringField(DayData$,9,",") + #LF$
AddGadgetItem(i, -1, Line$)
While WindowEvent():Wend
Until Not DayData$
LockWindowUpdate_(#Null) ; speed - unlock window for updates
Else
; failure to download the file
MessageRequester("DB","Download failure " + "Ticker")
EndIf
CloseGadgetList()
; Show processing time for the heck of it.
; MessageRequester("Time to process",StrF((ElapsedMilliseconds() - Starttime)/1000,3) + " seconds.",#MB_ICONINFORMATION)
AddGadgetItem(999,-1,"Yahoo Finance API")
URL.s = "http://elfecc.no-ip.info/purebasic/Yahoo_Finance_API.htm"
WebGadget(998, 10, 10, 780, 590, URL)
CloseGadgetList()
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
Else
; failed to open window
EndIf
Hope this gives you some ideas.