I'm writting a monitor program to see if our web server is ready to accept request by trying to resolve a URL of a known page. If the page is not found, then my program would send messages. Trying to use HTTPOpen() and never receive the 404 that I expected. Here is the test script.
tophandle=iBegin(0,"","")
connecthandle=iHostConnect(tophandle, ThisHost,@HTTP,"", "")
datahandle=iHttpInit(connecthandle, "GET", Object, "",0)
If datahandle==0
err=iGetLastError()
Message("Last Error",err)
iClose(tophandle)
Exit
EndIf
myheader=StrCat("user_process: My cool app", @CRLF, "app_version: 12.0")
rslt=iHttpOpen(datahandle, "" , 0, 0)
Message("HTTPGet", "Response Code = %rslt%")
If rslt=="ERROR" || rslt!=200
If rslt == "ERROR"
errstr = "WinInet Error"
rslt = iGetLastError()
Else
errstr = "HTTP Error"
EndIf
Message(errstr,rslt)
iClose(tophandle)
Exit
EndIf
xx=iReadData(datahandle, StrCat(ProgramPath, "HTTPGet.txt"))
iClose(datahandle)
iClose(connecthandle)
iClose(tophandle)
Message("All","Done")
Exit
When I intentionally mis-spell the URL, I still get a 200. and when I use iReadData, I get the following returned:
<script src='Login.jsgz'></script>
Is there a better way to do this?
Make sure that the server is in fact returning 404. Some websites report a "not found" error by returning a standard web page with a "200 OK" response code; this is known as a soft 404. Soft 404s are problematic for automated methods of discovering whether a link is broken.
Here is a code sample, that I put together that does return 404 from a server that actually returns a 404 error. :
;Load the Appropriate Extender
AddExtender("WWINT44I.DLL",0,"WWINT64I.DLL")
host = "www.barrons.com"
url = "/bie/articles/current/toc.htm"
tophandle=iBegin( 0, "", "" )
If tophandle==0
err=iGetLastError( )
Message( "iBegin Last Error", err )
GoSub CLEANUP
Exit
EndIf
connecthandle = iHostConnect( tophandle, host, @HTTP, "", "" )
err = iGetLastError( )
If connecthandle == 0
err = iGetLastError( )
Message( "iHostConnect Last Error", err )
GoSub CLEANUP
Exit
EndIf
datahandle = iHttpInit( connecthandle, "GET", url, "", 4 );Enable Keep Alive
If datahandle == 0
err = iGetLastError( )
Message( "iHttpInit Last Error", err )
GoSub CLEANUP
Exit
EndIf
rslt=iHttpOpen(datahandle, "" , 0, 0)
Pause('iHttpOpen',rslt)
Exit
:CLEANUP
If IsDefined( datahandle ) Then iClose( datahandle )
If IsDefined( connecthandle ) Then iClose( connecthandle )
If IsDefined( tophandle ) Then iClose( tophandle )
Return
Apparently you can use "Fetch as Google" (or other tools available on the web) to verify whether the URL is actually returning the correct code.
https://support.google.com/webmasters/answer/181708?hl=en