Kbase P125506: How to Post a HTTP Request using 4GL
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  3/8/2011 |
|
Status: Unverified
GOAL:
How to Post a HTTP Request using 4GL Sockets
GOAL:
How to send parameters to a WebSpeed Program using 4GL sockets
GOAL:
How to build a simple HTTP client using 4GL sockets
FACT(s) (Environment):
Progress 9.x
OpenEdge 10.x
All Supported Operating Systems
FIX:
The following sample does a request to a Webserver URL and waits for its reply. It uses a POST method, which allows lots of data to be sent. A full discussion of HTTP is beyond the solution, so it will be useful to get some HTTP documentation to extend this sample and make it more powerful.
However, basically what this sample does is:
1 - Connects to the Server, using a hostname and port. Change it to be applicable for the desired server
2 - Sends a piece of data, consisting of the URL that will be dealing the request, and the data in the format param=value¶m=value¶m=value. The sample uses a webspeed URL, but could it be anything in the other side as long as it is HTTP. Sonic http listeners are good example of real needs for usage of this technique.
3 - Gets the answer in the procedure getResponse. Customize it to read, parse and work with the results.
In this case, only basic headers are sent. If the target application requires headers like cookies, different content types, the sample has to be extended to support those headers.
DEFINE VARIABLE vcHost AS CHARACTER INITIAL "localhost" NO-UNDO.
DEFINE VARIABLE vcPort AS CHARACTER INITIAL "8080" NO-UNDO.
DEFINE VARIABLE vhSocket AS HANDLE NO-UNDO.
CREATE SOCKET vhSocket.
vhSocket:CONNECT('-H ' + vcHost + ' -S ' + vcPort) NO-ERROR.
IF vhSocket:CONNECTED() = FALSE THEN
DO:
MESSAGE "Connection failure" VIEW-AS ALERT-BOX.
MESSAGE ERROR-STATUS:GET-MESSAGE(1) VIEW-AS ALERT-BOX.
RETURN.
END.
ELSE
MESSAGE "Connect" VIEW-AS ALERT-BOX.
vhSocket:SET-READ-RESPONSE-PROCEDURE('getResponse').
/* supposes there is an webspeed app called yourapp.w that receives param1, param2, param3 */
RUN PostRequest (INPUT '/scripts/cgiip.exe/WService=wsbroker1/yourApp.w', 'param1=value¶m2=value¶m3=value').
WAIT-FOR READ-RESPONSE OF vhSocket.
vhSocket:DISCONNECT() NO-ERROR.
DELETE OBJECT vhSocket.
QUIT.
PROCEDURE getResponse:
DEFINE VARIABLE vcWebResp AS CHARACTER NO-UNDO.
DEFINE VARIABLE lSucess AS LOGICAL NO-UNDO.
DEFINE VARIABLE mResponse AS MEMPTR NO-UNDO.
IF vhSocket:CONNECTED() = FALSE THEN do:
MESSAGE 'Not Connected' VIEW-AS ALERT-BOX.
RETURN.
END.
lSucess = TRUE.
DO WHILE vhSocket:GET-BYTES-AVAILABLE() > 0:
SET-SIZE(mResponse) = vhSocket:GET-BYTES-AVAILABLE() + 1.
SET-BYTE-ORDER(mResponse) = BIG-ENDIAN.
vhSocket:READ(mResponse,1,1,vhSocket:GET-BYTES-AVAILABLE()).
vcWebResp = vcWebResp + GET-STRING(mResponse,1).
END.
/* BR> *
*
*PUT HERE THE CODE TO MANIPULATE THE ANSWER
*/
END.
PROCEDURE PostRequest:
DEFINE VARIABLE vcRequest AS CHARACTER.
DEFINE VARIABLE mRequest AS MEMPTR.
DEFINE INPUT PARAMETER postUrl AS CHAR. /* URL that will send the data. It must be all the path after the server. IE: /scripts/cgiip.exe/WService=wsbroker1/myApp.htm */
DEFINE INPUT PARAMETER postData AS CHAR. /* Parameters to be sent in the format paramName=value¶mName=value¶mName=value */
vcRequest = 'POST ' + postUrl + ' HTTP/1.0~r~n' +
'Content-Type: application/x-www-form-urlencoded~r~n' +
'Content-Length:' + string(LENGTH(postData)) + '~r~n' +
'~r~n' +
postData + '~r~n'.
MESSAGE vcREquest VIEW-AS ALERT-BOX.
SET-SIZE(mRequest) = 0.
SET-SIZE(mRequest) = LENGTH(vcRequest) + 1.
SET-BYTE-ORDER(mRequest) = BIG-ENDIAN.
PUT-STRING(mRequest,1) = vcRequest .
vhSocket:WRITE(mRequest, 1, LENGTH(vcRequest)).
END PROCEDURE..