Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using ServerXMLHTTP to redirect 1

Status
Not open for further replies.

pwills

Technical User
Sep 14, 2002
54
GB
I'd like to test for availability of site1. If available, redirect to the site, otherwise redirect to site2. I'm not an ASP expert and do not know how to create the object and handle timeouts.
 
Well, you should not experience time outs, you should get server and page not found errors. What you can do is create your XMLHTTP object and check the status code it returns. If it doesn't return code 200 then you will want to check the next server:
Code:
<%
Response.Buffer = True
Dim objXMLHTTP, URL

' Create an xmlhttp object:
Set objXMLHTTP = Server.CreateObject(&quot;Microsoft.XMLHTTP&quot;)

URL = &quot;[URL unfurl="true"]http://www.server1.com/pagename.asp&quot;[/URL]

' Opens the connection to the remote server.
objXMLHTTP.Open &quot;GET&quot;, URL, False

' Actually Sends the request and returns the data:
objXMLHTTP.Send

'Check the return status code
If objXMLHTTP.status <> 200 Then	'If page did not come back ok
	'Change the URL
	URL = &quot;[URL unfurl="true"]http://www.server2.com/pagename.asp&quot;[/URL]

	' Opens the connection to the remote server.
	objXMLHTTP.Open &quot;GET&quot;, URL, False

	' Actually Sends the request and returns the data:
	objXMLHTTP.Send

	If objXMLHTTP.status <> 200 Then
		Response.Write &quot;No Server Available.&quot;
	Else							'Else Server 2 is available
		Response.Redirect URL			'redirect
	End If
Else								'Else server 1 is available
	Response.Redirect URL				'redirect
End If
	
%>

Hope that helps,
-Tarwn &quot;Customer Support is an art not a service&quot; - marketing saying
&quot;So are most other forms of torture&quot; - programmers response
(The Wiz Biz - Rick Cook)
 
trouble is, if I put a URL for server 1 which I know is unavailable I get an error 500 and it doesn't redirect to server 2.
 
This works for me. I tried changing the first server to a known server and changing both to garbage addresses:
Code:
<%
Response.Buffer = True
Dim objXMLHTTP, URL

' Create an xmlhttp object:
Set objXMLHTTP = Server.CreateObject(&quot;Microsoft.XMLHTTP&quot;)

URL = &quot;[URL unfurl="true"]http://www.dfrdfdd.com/pagename.asp&quot;[/URL]

' Opens the connection to the remote server.
objXMLHTTP.Open &quot;GET&quot;, URL, False

On Error Resume Next
	' Actually Sends the request and returns the data:
	objXMLHTTP.Send

'Check the return status code
If objXMLHTTP.status <> 200 Then	'If page did not come back ok
	'Change the URL
	URL = &quot;[URL unfurl="true"]http://www.tek-tips.com&quot;[/URL]

	' Opens the connection to the remote server.
	objXMLHTTP.Open &quot;GET&quot;, URL, False

	' Actually Sends the request and returns the data:
	objXMLHTTP.Send

	If objXMLHTTP.status <> 200 Then
		Response.Write &quot;No Server Available.&quot;
	Else							'Else Server 2 is available
		Response.Write &quot;<b>Server 2 is available</b>&quot;
		Response.Write objXMLHTTP.ResponseText
	End If
Else								'Else server 1 is available
	Response.Write &quot;<b>Server 1 is available</b>&quot;
	Response.Write objXMLHTTP.ResponseText
End If
	
%>
-Tarwn &quot;Customer Support is an art not a service&quot; - marketing saying
&quot;So are most other forms of torture&quot; - programmers response
(The Wiz Biz - Rick Cook)
 
Ok it works now, not sure why! Thank you very much.
Do you happen to know whether it works with all browsers?
 
..and also, if I call it with n parameters, is it be possible to pass these through?

E.g. mysite/redirect.asp?a=1&b=2&c=3

will go to

server1?a=1&b=2&c=3
 
That should work, as it is part of the URL and will be processed by the server yyour attempting to request the information from. Also, this should be browser independant as long as you redirect to the page instead of just printing it out like I did above. Since all of the work will be done on the server it won't matter what the client is running.
-Tarwn &quot;Customer Support is an art not a service&quot; - marketing saying
&quot;So are most other forms of torture&quot; - programmers response
(The Wiz Biz - Rick Cook)
 
Using the redirect vesion, annoyingly it loses the parameters:
e.g. when I call mysite/redirect.asp?a=1&b=2&c=3 just server1/index.asp is loading

Do you know if I could use Server.Transfer or is there a function which can grab the whole parameter string?

You are v. helpful, thank you.
 
I realise I cannot use Server.transfer across different machines....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top