![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(SQL Server) Example: Http.DownloadAppend methodDemonstrates the DownloadAppend method.
-- 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 |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.