How to download a file from a remote location using the HTTPService

Date:Archived
Product/Release:LANSA Integrator V10
Abstract:How to download a file from a remote location using the HTTPService
Submitted By:LANSA Technical Support

This example shows how to use LANSA Integrator to download a file from a remote location and save it to a specified directory on a PC using the HTTPService. This very simple example is written using LANSA Integrator and Visual LANSA V10.

The HTTPService is used to do a *GET of a .PDF document from the Australian Taxation Office Web site. Document size is 135kb. The PDF document that is being retrieved is available to anyone on the ATO Web site at this location and is used simply as an example for this demonstration. (http://www.ato.gov.au/content/downloads/nat1628.pdf)

This example uses a .pdf type file to download. Other file types that have been tested with this example are .gif and .html.

Once LANSA Integrator has received the response it will then save the document to the location specified on the local hard drive.

To run this example cut and paste the code into a Visual LANSA form, compile and execute.

Note: If your document is not downloaded, then enable tracing and check the trace information for detailed error messages. For more information on tracing, see this tip: Vital information to include when reporting LANSA Integrator problems. One reason for a possible error maybe that the ATO document no longer exists. If this is the case simply change the "URI" and the "HOST" code in the following line to something that you know exists, for example, to get Google's home page you would enter the following, URI = http://www.google.com.au Host = www.google.com.au 

Change Field(#S_JSMCMD) To('''SEND handler(ONVL) method(*GET) URI(http://www.ato.gov.au/content/downloads/n5425-03-2004.pdf) HOST(www.ato.gov.au)''')

Business value:

One use for this may be to programmatically download a document that may be in a continuous state of being updated. This will ensure that you always have the latest available version.

Details:

* 
* 
* COMPONENT: STD_FORM
* 
* 
Function Options(*DIRECT)
Begin_Com Role(*EXTENDS #PRIM_FORM) Clientheight(145) Clientwidth(638) Height(173) Left(323) Top(155) Width(646)
Define_Com Class(#PRIM_PHBN) Name(#BT_LOADSERV) Caption('Load Service') Displayposition(1) Left(368) Parent(#COM_OWNER) Tabposition(1) Top(104)
Define_Com Class(#PRIM_PHBN) Name(#PB_GETXML) Caption('Get File') Displayposition(2) Left(453) Parent(#COM_OWNER) Tabposition(2) Top(104)
Define_Com Class(#PRIM_PHBN) Name(#BTN_OPNJSM) Caption('Open JSM') Displayposition(3) Left(280) Parent(#COM_OWNER) Tabposition(3) Top(104)
Define_Com Class(#S_JSMMSG.Visual) Name(#S_JSMMSG) Displayposition(4) Height(19) Left(8) Parent(#COM_OWNER) Tabposition(4) Top(16) Width(609)
Define_Com Class(#S_JSMSTS.Visual) Name(#S_JSMSTS) Displayposition(5) Height(19) Left(8) Parent(#COM_OWNER) Tabposition(5) Top(48) Width(324)
Define_Com Class(#PRIM_PHBN) Name(#PHBN_1) Caption('Close Form') Displayposition(6) Left(539) Parent(#COM_OWNER) Tabposition(6) Top(104)


Evtroutine Handling(#com_owner.Initialize)
Set Com(#com_owner) Caption(*component_desc)

* Define the field to specify the server
Define Field(#JSMSVR) Type(*CHAR) Length(20)
Change Field(#JSMSVR) To('localhost:4560')
Endroutine

Evtroutine Handling(#BTN_OPNJSM.Click)

* Open the JSM
Use Builtin(JSM_OPEN) With_Args(#JSMSVR) To_Get(#S_JSMSTS #S_JSMMSG)
Endroutine

Evtroutine Handling(#BT_LOADSERV.Click)

* Load the HTTP service
Use Builtin(JSM_COMMAND) With_Args('SERVICE_LOAD SERVICE(HTTPCLIENT)') To_Get(#S_JSMSTS #S_JSMMSG)
Endroutine

Evtroutine Handling(#PB_GETXML.Click)
Change Field(#S_JSMCMD) To('''SEND handler(ONVL) method(*GET) URI(http://www.ato.com.au/content/downloads/n5425.pdf) HOST(www.ato.com.au)''')
Use Builtin(JSM_COMMAND) With_Args(#S_JSMCMD) To_Get(#S_JSMSTS #S_JSMMSG)
Change Field(#S_JSMCMD) To('RECEIVE handler(IFILE) TO(/LatestTaxUpdate.pdf)')
Use Builtin(JSM_COMMAND) With_Args(#S_JSMCMD) To_Get(#S_JSMSTS #S_JSMMSG)
Endroutine

Evtroutine Handling(#PHBN_1.Click)
Invoke Method(#com_owner.closeForm)
Endroutine
End_Com