PB5.72 - New HTTP feature to read headers not working

Just starting out? Need help? Post your questions and find answers here.
nalor
Enthusiast
Enthusiast
Posts: 121
Joined: Thu Apr 02, 2009 9:48 pm

PB5.72 - New HTTP feature to read headers not working

Post by nalor »

Hi!
Please help me - I can't figure out how to use the new feature to get the http headers.

Here's my example:

Code: Select all

EnableExplicit

Define sUrl.s="https://www.purebasic.com/download/PureBasic_Demo.zip"
Define iConn.i
Define iProgress.i

InitNetwork()

iConn=ReceiveHTTPMemory(sUrl, #PB_HTTP_Asynchronous)

If Not iConn
	Debug "Error"
	End
EndIf

Debug "Headers >"+HTTPInfo(iConn, #PB_HTTP_Headers)+"<"

Repeat
	iProgress = HTTPProgress(iConn)
	Select iProgress
		Case #PB_HTTP_Success
			Debug "iConn finished (size: )"
			End
		
		Case #PB_HTTP_Failed
			Debug "iConn failed"
			FinishHTTP(iConn)
			End
		
		Case #PB_HTTP_Aborted
			Debug "iConn aborted"
			FinishHTTP(iConn)
			End
		
		Default
		
			Debug "Headers >"+HTTPInfo(iConn, #PB_HTTP_Headers)+"<"
			Debug "Current iConn: " + iProgress
	
	EndSelect
	
	Delay(500) ; Don't stole the whole CPU
ForEver
But the command >HTTPInfo(iConn, #PB_HTTP_Headers)< never returns anything useful.

Where's my error?

Thanks for help :)
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: PB5.72 - New HTTP feature to read headers not working

Post by helpy »

Read PureBasic help and check the syntax of ReceiveHTTPMemory() ...
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
nalor
Enthusiast
Enthusiast
Posts: 121
Joined: Thu Apr 02, 2009 9:48 pm

Re: PB5.72 - New HTTP feature to read headers not working

Post by nalor »

But I don't want the data in my example - all I want is the headers ... to read the contentsize from it and calculate the progress during the async download.
(I know in my example the data is just buffered and finally lost because it's never read from the buffer - but I only care about 'HTTInfo' that isn't working as expected.)

Before 5.72 it wasn't possible with a single request to get the headers and download, but now it should be - just that I can't figure out how to use the new feature correctly.
infratec
Always Here
Always Here
Posts: 7622
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: PB5.72 - New HTTP feature to read headers not working

Post by infratec »

As you can read in the help of HTTPInfo(),
it is only valid for HTTPRequest() and HTTPRequestMemory()
and not for ReceiveHTTPMemory()

So you need

Code: Select all

EnableExplicit

Define sUrl.s="https://www.purebasic.com/download/PureBasic_Demo.zip"
Define iConn.i
Define iProgress.i

InitNetwork()

iconn = HTTPRequest(#PB_HTTP_Get, sUrl, "", #PB_HTTP_HeadersOnly)
If iconn
  Debug "Headers >"+HTTPInfo(iConn, #PB_HTTP_Headers)+"<"
  FinishHTTP(iconn)
EndIf

iConn=ReceiveHTTPMemory(sUrl, #PB_HTTP_Asynchronous)

If Not iConn
   Debug "Error"
   End
EndIf

Repeat
   iProgress = HTTPProgress(iConn)
   Select iProgress
      Case #PB_HTTP_Success
        Debug "iConn finished (size: )"
         End
      
      Case #PB_HTTP_Failed
         Debug "iConn failed"
         FinishHTTP(iConn)
         End
      
      Case #PB_HTTP_Aborted
         Debug "iConn aborted"
         FinishHTTP(iConn)
         End
      
      Default
         Debug "Current iConn: " + iProgress
   
   EndSelect
   
   Delay(500) ; Don't stole the whole CPU
ForEver
#PB_HTTP_HeadersOnly does only fetch the header and not the content.
Last edited by infratec on Wed May 20, 2020 7:03 pm, edited 1 time in total.
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: PB5.72 - New HTTP feature to read headers not working

Post by helpy »

helpy wrote:Read PureBasic help and check the syntax of ReceiveHTTPMemory() ...
:oops: :oops: ... oops... sorry... I have confused the two commands ReceiveHTTPMemory and HTTPRequestMemory.
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
nalor
Enthusiast
Enthusiast
Posts: 121
Joined: Thu Apr 02, 2009 9:48 pm

Re: PB5.72 - New HTTP feature to read headers not working

Post by nalor »

Thanks for the help! Didn't notice this small detail ...

Problem solved :)
Post Reply