Page 1 of 1

IsHttpFile()

Posted: Tue Mar 18, 2014 8:47 pm
by infratec
Hi,

since we need to check if our images on the webserver are available I wrote a procedure which does not download them :mrgreen:

Code: Select all

Procedure.i IsHttpFile(URL$)
  
  Protected.i Result, Connection, Size, Pos, Status
  Protected Head$
  
  
  Connection = OpenNetworkConnection(GetURLPart(URL$, #PB_URL_Site), 80)
  If Connection
    
    Head$ = "HEAD /" + GetURLPart(URL$, #PB_URL_Path) + " HTTP/1.1" + #CRLF$
    Head$ + "Host: " + GetURLPart(URL$, #PB_URL_Site) + #CRLF$
    Head$ + #CRLF$
    
    ;Debug Head$
    
    SendNetworkString(Connection, Head$)
    
    TimeOutCounter = 100
    Repeat
      If NetworkClientEvent(Connection) = #PB_NetworkEvent_Data
        
        Buffer$ = Space(1024)
        Size = ReceiveNetworkData(Connection, @Buffer$, Len(Buffer$))
        If Size
          Buffer$ = PeekS(@Buffer$, Size, #PB_UTF8)
          ;Debug Buffer$
          Pos = FindString(Buffer$, " ")
          If Pos
            Status = Val(Mid(Buffer$, Pos + 1, 3))
            If Status > 199 And Status < 300
              Result = #True
            EndIf
          EndIf
        EndIf
        
        Break
      EndIf
      Delay(10)
      TimeOutCounter - 1
    Until TimeOutCounter = 0
    
    CloseNetworkConnection(Connection)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure
Example:

Code: Select all

Debug IsHttpFile("http://www.purebasic.fr/english/index.php")
Debug IsHttpFile("http://www.purebasic.fr/english/index.ppp")
Bernd

Re: IsHttpFile()

Posted: Wed Mar 19, 2014 4:07 am
by J. Baker
Don't forget InitNetwork(). ;)

Re: IsHttpFile()

Posted: Wed Mar 19, 2014 4:17 am
by Shield
Shortened it "a bit". :)

Code: Select all

EnableExplicit

InitNetwork()

Procedure.i IsHttpFile(url.s)
	Protected header.s
	Protected status.i
	
	header = GetHTTPHeader(url)
	status = Val(StringField(header, 2, " "))
	
	If status < 200 Or status >= 300
		ProcedureReturn #False
	EndIf
	
	ProcedureReturn #True
EndProcedure


Debug IsHttpFile("http://www.purebasic.fr/english/index.php")
Debug IsHttpFile("http://www.purebasic.fr/english/index.ppp")

Re: IsHttpFile()

Posted: Wed Mar 19, 2014 7:42 am
by infratec
:oops: :oops: :oops:

What a m... :!:

I should read the complete help file every month :mrgreen:

Bernd