SQL Server
SQL Server
Example: Http.DownloadAppend method
Demonstrates the DownloadAppend method.Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
SELECT @success = 0
DECLARE @targetPath nvarchar(4000)
SELECT @targetPath = 'c:/temp/qa_output/download.txt'
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @http, 'KeepResponseBody', 1
-- Assume the target file in the local filesystem does not yet exist.
EXEC sp_OAMethod @http, 'DownloadAppend', @success OUT, 'https://chilkatsoft.com/testData/helloWorld.txt', @targetPath
DECLARE @statusCode int
EXEC sp_OAGetProperty @http, 'LastStatus', @statusCode OUT
IF @statusCode = 0
BEGIN
-- Unable to either send the request or get the response.
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
RETURN
END
-- Should be 200.
PRINT 'Response status code: ' + @statusCode
-- Examine the contents of the file.
DECLARE @sb int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sb OUT
EXEC sp_OAMethod @sb, 'LoadFile', @success OUT, @targetPath, 'utf-8'
EXEC sp_OAMethod @sb, 'GetAsString', @sTmp0 OUT
PRINT @sTmp0
-- Output:
-- Response status code: 200
-- Hello World!
-- Download another text file and append to the target file.
EXEC sp_OAMethod @http, 'DownloadAppend', @success OUT, 'https://chilkatsoft.com/testData/this_is_a_test.txt', @targetPath
EXEC sp_OAGetProperty @http, 'LastStatus', @statusCode OUT
IF @statusCode = 0
BEGIN
-- Unable to either send the request or get the response.
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @sb
RETURN
END
-- Should be 200.
PRINT 'Response status code: ' + @statusCode
-- Examine the contents of the file.
EXEC sp_OAMethod @sb, 'LoadFile', @success OUT, @targetPath, 'utf-8'
EXEC sp_OAMethod @sb, 'GetAsString', @sTmp0 OUT
PRINT @sTmp0
-- Output:
-- Response status code: 200
-- Hello World!This is a Test.
-- Delete the local target file.
DECLARE @fac int
EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @fac OUT
EXEC sp_OAMethod @fac, 'FileDelete', @success OUT, @targetPath
-- Try to download a file that does not exist:
EXEC sp_OAMethod @http, 'DownloadAppend', @success OUT, 'https://chilkatsoft.com/testData/does_not_exist.txt', @targetPath
EXEC sp_OAGetProperty @http, 'LastStatus', @statusCode OUT
IF @statusCode = 0
BEGIN
-- Unable to either send the request or get the response.
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
END
ELSE
BEGIN
-- We got a response, and we already know it wasn't a 200 success response.
-- It should be a 404 not found.
PRINT 'Response status code: ' + @statusCode
-- Examine the response body.
PRINT 'Response body:'
EXEC sp_OAGetProperty @http, 'LastResponseBody', @sTmp0 OUT
PRINT @sTmp0
END
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @sb
EXEC @hr = sp_OADestroy @fac
END
GO