POST A HTTP REQUEST using 4GL.

Posted by Admin on 15-Oct-2008 13:32

I am using progress 10.1b on my PC, running windows XP.

I am following the code example in P125506 to post a HTTP Request. The code sample is at bottom of message.

I basically connect to my localhost on my port, and I post to the URL on my network.

Do I need some sort of server running to facilitate all this communication?

The response I get when I post is NOT from the URL I thought I was posting to.

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&param2=value&param3=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&paramName=value&paramName=value */

vcRequest = 'POST ' + postUrl + ' HTTP/1.0rn' +

'Content-Type: text/xmlrn' +

'Content-Length:' + string(LENGTH(postData)) + 'rn' +

'rn' +

postData + 'rn'.

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..

All Replies

Posted by Matt Baker on 15-Oct-2008 15:18

Your example connection parameters are localhost:8080. The port 8080 is normally used for proxy requests for HTTP. Port 80 is normally used for http. If you plan on requesting data using this example you are going to need a web server such as Apache or IIS.

Posted by Admin on 15-Oct-2008 16:52

OK,

There were several issues that had to be worked through.

I had to correct my host and port to get my program to work.

Thank you all!!!

This thread is closed