How to test for 404 page not found?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

How to test for 404 page not found?

28 Jul 2021, 20:01

Basically there's this site that goes by categories and I only want to launch the site if the page is valid. is this possible?

Code: Select all


Run, https://abc.com/categoryA/SearchTerm
if 404
Run, https://abc.com/categoryB/SearchTerm
if 404
Run, https://abc.com/categoryC/SearchTerm
if 404, do not launch browser at all 
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: How to test for 404 page not found?

28 Jul 2021, 20:39

This isn't foolproof because if a page contains 404 for some other reason, it will still not run it, such as this thread (the third case below):

Code: Select all

OpenIfNot404("https://abc.com/categoryA/SearchTerm")
OpenIfNot404("https://www.autohotkey.com/boards")
OpenIfNot404("https://www.autohotkey.com/boards/viewtopic.php?f=76&t=93166")
return

OpenIfNot404(url) {
	hObject:=ComObjCreate("WinHttp.WinHttpRequest.5.1")
	hObject.Open("GET",url)
	hObject.Send()
	if !InStr(hObject.ResponseText, "404")
		Run chrome.exe %url%
}
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to test for 404 page not found?

28 Jul 2021, 22:31

@boiler
Try this one:

Code: Select all

OpenIfNot404("https://www.autohotkey.com/boards/viewtopic.php?f=76&t=93166&p=412515#p412515")
return

OpenIfNot404(url) {
   hObject:=ComObjCreate("WinHttp.WinHttpRequest.5.1")
   hObject.Open("GET",url)
   hObject.Send()
   if !InStr(hObject.ResponseText, "404")
      Run chrome.exe %url%
}
I'd suggest this code:

Code: Select all

OpenIfNot404("https://abc.com/categoryA/SearchTerm")
OpenIfNot404("https://www.autohotkey.com/boards")
OpenIfNot404("https://www.autohotkey.com/boards/viewtopic.php?f=76&t=93166")
return

OpenIfNot404(url) {
   Whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
   Whr.Open("GET", url)
   Whr.Send()
   MsgBox, % Whr.status
}
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: How to test for 404 page not found?

28 Jul 2021, 23:06

Yes, I think you and I are showing the same drawback of my approach. Thanks for showing the better approach.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 215 guests