Page 1 of 1

ReceiveHTTPFile

Posted: Sun Dec 20, 2020 7:46 pm
by arma
Is this normal? there is no file like that. But i recieve 1. Whatever i write there as file name... such as http://armasoft.net/... i always get result positive... How can i get normal result? I need true or false?

InitNetwork()
a=ReceiveHTTPFile("http://armasoft.net/xxx","/home/arma/TEST")
Debug a

Re: ReceiveHTTPFile

Posted: Sun Dec 20, 2020 10:19 pm
by Bisonte
If there is no file .... you get the server error page, like 404-Page....
So a #True result is normal, ReceiveHTTPFile download something.

Re: ReceiveHTTPFile

Posted: Mon Dec 21, 2020 12:13 am
by arma
Then what is the result meaning?
There is an help on document...

Example

InitNetwork()

Filename$ = SaveFileRequester("Where to save index.php ?", "", "", 0)

If ReceiveHTTPFile("http://www.purebasic.com/index.php", Filename$)
Debug "Success"
Else
Debug "Failed"
EndIf

It says always Success... Never says Failed... What a strange :(

Re: ReceiveHTTPFile

Posted: Mon Dec 21, 2020 12:30 am
by Bisonte
Because it is download this page, that comes with this url....

on your first try it downloads the error page, so 1 is ok.
0 is the result, if nothing is downloaded to your computer!

Re: ReceiveHTTPFile

Posted: Mon Dec 21, 2020 7:25 am
by arma
For me this is nothing useful... Some thing like bug... How can i detect if successfully download or not... and Why?
I solve my situation but this command works as stupid. I use wget now... It says error if there is no file to download. So i solve my problem. But in my oppinion this is wrong. If it will always says True... When will it say False? And what is it for?

Re: ReceiveHTTPFile

Posted: Mon Dec 21, 2020 10:59 pm
by helpy
You have to do a HTTPRequest first and check the HTTP status code.
Here an untested code:

Code: Select all

Procedure ReceiveHTTPFileX(URL.s, Filename.s, Flags=#PB_Ignore, UserAgent.s=#Empty$)
	Protected httpReq, httpStatus
	httpReq = HTTPRequest(#PB_HTTP_Get, URL, #Null$, #PB_HTTP_HeadersOnly)
	If httpReq
		httpStatus = Val(HTTPInfo(httpReq, #PB_HTTP_StatusCode))
		FinishHTTP(httpReq)
		If httpStatus = 200
			If UserAgent <> #Empty$
				ProcedureReturn ReceiveHTTPFile(URL, Filename, Flags, UserAgent)
			ElseIf Flags <> #PB_Ignore
				ProcedureReturn ReceiveHTTPFile(URL, Filename, Flags)
			Else
				ProcedureReturn ReceiveHTTPFile(URL, Filename)
			EndIf
		Else
			; HTTP request failed
			ProcedureReturn #Null
		EndIf
	Else
		; Http request failed
		ProcedureReturn #Null
	EndIf
EndProcedure
Maybe the helps to think about the problem ...