Adam, that's beautiful. There seem to be a lot of different ways of doing this.
Here is an example that allows you to give a User ID and password for the site and the proxy, and uses the WinHTTP.WinHTTPRequest object. Of course, your server will need access to the web which is not normally advised for a production server, but that's life?
ALTER PROCEDURE dbo.spHTTPalive(
@URL VARCHAR(512),
@WebLogin VARCHAR(128) = NULL,
@WebPassword VARCHAR(128) = NULL,
@ProxyLogin VARCHAR(128) = NULL,
@ProxyPassword VARCHAR(128) = NULL)
AS
/*******************************************************************
Name : spHTTPAlive
Server : SQLserver 2000
Uses : WinHttp.WinHttpRequest.5.1
Description : does a simple GET to test the URL. Returns 0 if good
Parameters :
@URL : the url to use ( like http://www.simple-talk.com )
@WebLogin : (optional) The Username for the webserver
@WebPassword: (optional) The password for the webserver
@ProxyLogin : (optional) The Username for the proxyserver
@ProxyPassword : (optional) The password for the proxyserver
e.g. : Declare @Ret int
Exec @Ret=spHTTPAlive 'http://www.simple-talk.com'
select @ret
Date : 2006-07-08
Author : Phil Factor
History : helped a lot by an example by Bert De Haes
*******************************************************************/
SET nocount ON
DECLARE @ObjectToken INT,
-- the objecttoken for WinHttp.WinHttpRequest.5.1
@ReturnCode INT, -- the return code from sp_OA procedures
@Source VARCHAR(255), -- the source method of an error
@Description VARCHAR(512), -- the desciption of an error
@MethodName VARCHAR(512), -- The Method paramter passed
@Status INT
SET @ReturnCode = 0
-- create the 'WinHttp.WinHttpRequest.5.1' object
SET @MethodName = 'WinHttp.WinHttpRequest.5.1'
EXEC @ReturnCode = master.dbo.sp_oacreate @MethodName ,
@ObjectToken OUTPUT
IF @ReturnCode <> 0
GOTO error
-- open the url on the server
SET @MethodName = 'Open("GET" , "' + @URL + '")'
EXEC @ReturnCode = master.dbo.sp_oamethod @ObjectToken ,
@MethodName
IF @ReturnCode <> 0
GOTO error
IF @WebLogin IS NOT NULL
AND @WebPassword IS NOT NULL
BEGIN
-- Set the Credentials for the Webserver
SET @MethodName = 'SetCredentials("' + @WebLogin + '","' + @WebPassword + '",0)'
EXEC @ReturnCode = master.dbo.sp_oamethod @ObjectToken ,
@MethodName
IF @ReturnCode <> 0
GOTO error
END
IF @ProxyLogin IS NOT NULL
AND @ProxyPassword IS NOT NULL
BEGIN
-- Set the Credentials for the Proxy
SET @MethodName = 'SetCredentials("' + @ProxyLogin + '","' + @ProxyPassword + '",1)'
EXEC @ReturnCode = master.dbo.sp_oamethod @ObjectToken ,
@MethodName
IF @ReturnCode <> 0
GOTO error
END
SET @MethodName = 'Send'
EXEC @ReturnCode = sp_oamethod @ObjectToken ,
@MethodName
-- Get the HTTP_Status
IF @ReturnCode <> 0
GOTO error
SET @MethodName = 'Status'
EXEC @ReturnCode = sp_oamethod @ObjectToken ,
@MethodName ,
@status OUT
IF @ReturnCode <> 0
GOTO error
-- if we get here the normal way, don't do error
GOTO cleanup
ERROR:
IF @ObjectToken IS NOT NULL
BEGIN
EXEC sp_oageterrorinfo @ObjectToken ,
@Source OUT ,
@Description OUT
END
ELSE
BEGIN
SET @Source = '?'
SET @Description = '?'
END
SELECT @Source + ', ' + @Description + ', while doing the command ' + @MethodName
-- Destroy created object(s)
CLEANUP:
IF @ObjectToken IS NOT NULL
BEGIN
EXEC @ReturnCode = master.dbo.sp_oadestroy @ObjectToken
SET @ObjectToken = NULL
END
IF (@Status = 200)
RETURN (0)
ELSE
RETURN (1)