Consultor Eletrônico



Kbase 22003: How To Post XML Documents to WebSpeed Transaction Server
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   30/04/2002
SUMMARY:

This Solution applies to WebSpeed 3.1x. It explains how to post an XML document from any client application.

To handle the XML document, any WebSpeed object could use the WEB-CONTEXT system handle, which provides access to information on the current connection to the Web Server. The IS-XML attribute returns whether an XML document was posted to the transaction server.
X-DOCUMENT contains the handle of any XML document posted to the transaction server.

EXPLANATION:

The client should make a socket connection to the Web Server, request the right WebSpeed object and transfer the XML document to the Web Server via socket connection.

SOLUTION:

The following example is a Progress 4GL client application, but similar concepts could be used for Java, C/C++ or any other client.

/* xmlsend.p */
DEFINE VARIABLE vcWebHost AS CHARACTER
INITIAL "localhost" NO-UNDO.
DEFINE VARIABLE vcWebPort AS CHARACTER
INITIAL "80" NO-UNDO.
DEFINE VARIABLE vcWSAgent AS CHARACTER
INITIAL "/scripts/cgiip91c.exe/WService=wsbroker1/xmlagent.p" NO-UNDO.
/* An easy test on the WebSpeed object side (xmlagent.p ) is */
/* to check whether the WEB-CONTEXT:IS-XML is TRUE and */
/* WEB-CONTEXT:X-DOCUMENT is a valid handle. */

DEFINE VARIABLE vhWebSocket AS HANDLE NO-UNDO.

CREATE SOCKET vhWebSocket.

vhWebSocket:CONNECT('-H ' + vcWebHost + ' -S ' + vcWebPort ) NO-ERROR.
IF vhWebSocket:CONNECTED() = FALSE THEN DO:
/* error connecting to the Web Server */
END.

vhWebSocket:SET-READ-RESPONSE-PROCEDURE('getWebServerResponce').
RUN PostRequest.
WAIT-FOR READ-RESPONSE OF vhWebSocket.
vhWebSocket:DISCONNECT() NO-ERROR.
DELETE OBJECT vhWebSocket.

/* Responsible for reading server response */
PROCEDURE getWebServerResponce:

DEFINE VARIABLE vcWebResp AS CHARACTER NO-UNDO.
DEFINE VARIABLE lSuccess AS LOGICAL.
DEFINE VARIABLE mResponse AS MEMPTR.

IF SELF:CONNECTED() = FALSE THEN RETURN.

lSuccess = TRUE.
DO WHILE lSuccess AND ERROR-STATUS:GET-MESSAGE(1) EQ '' :
SET-SIZE(mResponse) = 1.
SET-BYTE-ORDER(mResponse) = BIG-ENDIAN.
SELF:READ(mResponse,1,1,1).
vcWebResp = vcWebResp + GET-STRING(mResponse,1).
END.

/* just to see what is coming back */
OUTPUT TO "c:\temp\xmlsend.log" .

put unformatted "BEGIN: " STRING ( TIME , "hh:mm:ss" ) SKIP.
put unformatted vcWebResp SKIP.
put unformatted "END: " STRING ( TIME , "hh:mm:ss" ) SKIP.

output close.

END PROCEDURE.

/* make a request and post the xml document */
PROCEDURE PostRequest:
DEF VAR viXMLLength AS INTEGER.
DEF VAR vcRequest AS CHARACTER.
DEF VAR viRequest AS INTEGER.
DEF VAR viMsg AS INTEGER.
DEF VAR mRequest AS MEMPTR.
DEF VAR lSuccess AS LOGICAL.

/* just to be very simple, the XML document is stored in this var */
DEFINE VARIABLE vcXMLText AS CHARACTER NO-UNDO.
vcXMLText =
"<?xml version='1.0'?>"
+ "<COMPANY>"
+ "<NAME>PROGRESS SOFTWARE CORPORATION</NAME>"
+ "</COMPANY>" .

ASSIGN
viXMLLength = LENGTH ( vcXMLText )
vcRequest =
'POST ' + vcWSAgent
+ ' HTTP/1.0~n'
+ 'Content-Type: text/xml~n' /* post XML DOC */
+ 'Host: ' + vcWebHost + '~n'
+ 'Content-Length: ' + STRING(viXMLLength) + '~n'
+ 'Accept: text/html~n'
+ '~n'
viRequest = LENGTH ( vcRequest )
viMsg = viRequest + viXMLLength + 1
.

SET-SIZE(mRequest) = 0.
SET-SIZE(mRequest) = viMsg.
SET-BYTE-ORDER(mRequest) = BIG-ENDIAN.

PUT-STRING(mRequest,1) = vcRequest + vcXMLText.

vhWebSocket:WRITE(mRequest,1,viMsg) NO-ERROR.

IF ERROR-STATUS:ERROR THEN DO:
/* error writing to socket */
END.
END PROCEDURE.