Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
matrixebiz
Roku Guru

Check for 404 error

Hello, is there a simple Brightscript 2-3 lines of code that I can use to check if a url is alive or not and if it gives a 404 error then return the 404 error so I can try another url? Thank you
0 Kudos
11 REPLIES 11
matrixebiz
Roku Guru

Re: Check for 404 error

Is this a hard question?  All I need to do is check and see if the url is good (valid) or bad (404) in my RSG VideoScene.brs file.
Can I use some of this Function?
Function check(thisurl) as string
request = CreateObject("roUrlTransfer")
port = CreateObject("roMessagePort")
request.SetMessagePort(port)
request.SetUrl(thisurl)
if (request.AsyncGetToString())
while (true)
msg = wait(0, port)
if (type(msg) = "roUrlEvent")
code = msg.GetResponseCode()
if (code = 200)
print"good"
return "good"

elseif (code =400 or code=404)
print("image not found")
return "bad"
else
print"error"; code
return "generic bad code"

endif
else if (event = invalid)
request.AsyncCancel()
endif
end while
endif
return invalid
End Function
0 Kudos
matrixebiz
Roku Guru

Re: Check for 404 error

Anyone have the correct simplified code for me?
0 Kudos
matrixebiz
Roku Guru

Re: Check for 404 error

Anyone?
0 Kudos
squirreltown
Roku Guru

Re: Check for 404 error

not tested

Function Ping()
 
UT = CreateObject( "roUrlTransfer" )
port = CreateObject("roMessagePort")
UT.SetPort(port)
UT.SetUrl("URL HERE")
UT.AsyncHead()
event = wait (0, port)
if type(event) = "roUrlEvent"
if event.GetResponseCode() = 200 
'do something'
else if event.GetResponseCode() = 404
'do something else'
end if
end if

End Function

Kinetics Screensavers
0 Kudos
matrixebiz
Roku Guru

Re: Check for 404 error

Hello, thanks for responding but I get this error;

BRIGHTSCRIPT: ERROR: roUrlTransfer: creating MAIN|TASK-only component failed on RENDER 
'Dot' Operator attempted with invalid BrightScript Component or interface reference. (runtime error &hec) in pkg:/components/VideoScene.brs(36)
036:  UT.SetPort(port)

This is a RSG channel - https://github.com/rokudev/videoplayer-channel
0 Kudos
squirreltown
Roku Guru

Re: Check for 404 error

The function works, putting it in the right place is up to you. I don't know RSG but it looks like it's in the wrong thread.
Kinetics Screensavers
0 Kudos
matrixebiz
Roku Guru

Re: Check for 404 error

Anyone know the Scene Graph coding? and where in the VideoScene.brs file I can put a URL 404 check function?
https://github.com/rokudev/videoplayer-channel
0 Kudos
rymawby
Binge Watcher

Re: Check for 404 error

Have you tried putting it in the UriHandler Task?
0 Kudos
matrixebiz
Roku Guru

Re: Check for 404 error

I think I did try that file (UriHandler.brs) but the actual url gets converted into a uri before it hits this file so that file confused me on where to put the url check.
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.