View Full Version : trapping XMLHTTP.Open errors
luzmedia
07-23-2003, 01:34 PM
Can anyone please tell me how I can trap a "Page cannot be displayed" or "Page cannot be found" error when calling a URL with the Open method in XMLHTTP?
Here's what I want to be able to do:
--------------------------
Dim objXmlHttp, target_url
Set objXmlHttp = Server.CreateObject ("Msxml2.XMLHTTP")
target_url = "http://www.some-url.com"
If THIS_URL_RETURNS_A_PAGE_OF_HTML("target_url") Then
objXmlHttp.Open "GET", target_url, False
objXmlHttp.Send
do_some_work_on(objXmlHttp.ResponseText)
Else
Response.Write target_url & " is not available"
End If
-------------------------------
What I need is a definition for THIS_URL_RETURNS_A_PAGE_OF_HTML(url)
I'd be very grateful for any solutions.
Cheers
Charlie
Welcome here.
Look what i just ripped from
http://www.asptutorial.info/sscript/Accessremotepages.asp
So the request is always sent, and the content is stored in a variable. Then just check if it's empty.
----------------------------------------------------------------------------
In the example bellow we have add some code to avoid error messages when url is not available, so that we will get a alternative text (line 17)
GetremoteURL.asp
<%
' Intruduce the url you want to visit
GotothisURL = "http://www.asptutorial.info"
' Create the xml object
Set GetConnection = CreateObject("Microsoft.XMLHTTP")
' Conect to specified URL
GetConnection.Open "get", GotothisURL, False
on error resume next
GetConnection.Send
' ResponsePage is the response we will get when visiting GotothisURL
ResponsePage = GetConnection.responseText
' We will write
if ResponsePage="" then
Response.write("The page is not available")
else
Response.write(ResponsePage)
end if
Set GetConnection = Nothing
%>
-------------------------------------------------------------------------------
luzmedia
07-23-2003, 03:35 PM
Thanks raf - I knew there must be a simple solution!
But I realise now that I also need to be able to check that a URL is OK before I even call XMLHTTP - i.e. from basic ASP. I'm going to post this as another question but here it is in advance:
re: Trapping Response.Redirect error
How can I trap the error created when Response.Redirect tries to redirect to a non-existent - or unavailable - URL? I've tried On Error Resume Next but the script still falls over if the URL is no good.
Here's what I want to be able to do:
--------------------------
target_url = "http://www.some-url.com"
If THIS_URL_EXISTS_AND_IS_AVAILABLE_RIGHT_NOW("target_url") Then
Response.Redirect target_url
Else
Response.Write target_url & " is not available"
End If
-------------------------------
So what I need is a definition for THIS_URL_EXISTS_AND_IS_AVAILABLE_RIGHT_NOW(url)
Hope you can solve this one as quickly!
Thanks again
Charlie
glenngv
07-25-2003, 09:36 AM
Originally posted by raf
So the request is always sent, and the content is stored in a variable. Then just check if it's empty.
a better way is to check the HTTP Response code:
GetConnection.Send
if GetConnection.status=200 then 'OK
Response.write(GetConnection.responseText )
else
Response.write("The page is not available")
end if
you can check for more detailed error by checking other status codes like:
404 - file not found
500 - server error (the "Page cannot be displayed" page)
etc...
luzmedia
07-26-2003, 07:57 AM
Glenn - great solution - it's far better to be able to know what type of error it is as it's then possible to decide to try again later or to give up on that url.
Many thanks!
Cheers
Charlie:thumbsup:
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.