如何在 Progress/OpenEdge ABL 中执行 HTTP GET 和 POST?

发布于 2024-08-18 01:56:31 字数 188 浏览 8 评论 0原文

Progress 文档在 SOAP 上花费了大量的精力,但我很难找到带有 Progress ABL 的简单 HTTP GET/POST 示例。

如何从 URL 获取和发布字符串?

URL 可以是 https:// 吗?

Progress 可以提供 HTTP Basic 或 HTTP Digest 身份验证吗?

The Progress docs spill plenty of ink on SOAP, but I'm having trouble finding the example for a simple HTTP GET/POST with Progress ABL.

How do I GET and POST strings to/from a URL?

Can the URL be https://?

Can Progress provide HTTP Basic or HTTP Digest authentication?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

伊面 2024-08-25 01:56:31

对于这个问题的未来旁观者:

Openedge 现在(我相信从 11.5.1 开始)已经内置了对调用基于 REST 的 Web 服务的支持。它们包含在提供的 .pl 存档中,默认情况下该存档不在您的 PROPATH 中,因此需要首先处理(或者可以将存档移动到“更好的位置”)。

propath 可以通过多种方式设置,初始化文件、注册表、编程方式等。这就是在 ABL 中完成的方式(如果这样做,则必须为每个新会话重复)。

PROPATH = PROPATH + ",c:\pathtoprogress\OpenEdge\gui\OpenEdge.Net.pl".

“tty”目录中还有一个版本,以及“src”目录中包含源代码的存档。

这是一个非常基本的示例:

USING OpenEdge.Net.HTTP.IHttpRequest.
USING OpenEdge.Net.HTTP.IHttpResponse.
USING OpenEdge.Net.HTTP.ClientBuilder.
USING OpenEdge.Net.HTTP.RequestBuilder. 

DEFINE VARIABLE oRequest  AS IHttpRequest NO-UNDO.
DEFINE VARIABLE oResponse AS IHttpResponse NO-UNDO.

oRequest = RequestBuilder:Get('http://stackoverflow.com/'):Request. 

oResponse = ClientBuilder:Build():Client:Execute(oRequest).

MESSAGE
    oResponse:StatusCode SKIP   
    oResponse:StatusReason SKIP
    VIEW-AS ALERT-BOX.

11.6 的文档可以在此处找到。

For future onlookers at this question:

Openedge now (since 11.5.1 I believe) has built in support for calling REST based webservices. These are enclosed in a provided .pl archive that is not in your PROPATH by default so that needs to be handled first (or the archive can be moved to a "better location").

The propath can be set in a number of ways, init files, registry, programatically etc. This is how it can be done in ABL (if done this way it must be repeated for each new session).

PROPATH = PROPATH + ",c:\pathtoprogress\OpenEdge\gui\OpenEdge.Net.pl".

There's also a version in "tty" directory, as well as an archive containing source code in the "src" directory.

Here's a very basic example:

USING OpenEdge.Net.HTTP.IHttpRequest.
USING OpenEdge.Net.HTTP.IHttpResponse.
USING OpenEdge.Net.HTTP.ClientBuilder.
USING OpenEdge.Net.HTTP.RequestBuilder. 

DEFINE VARIABLE oRequest  AS IHttpRequest NO-UNDO.
DEFINE VARIABLE oResponse AS IHttpResponse NO-UNDO.

oRequest = RequestBuilder:Get('http://stackoverflow.com/'):Request. 

oResponse = ClientBuilder:Build():Client:Execute(oRequest).

MESSAGE
    oResponse:StatusCode SKIP   
    oResponse:StatusReason SKIP
    VIEW-AS ALERT-BOX.

Documentation for 11.6 can be found here.

守护在此方 2024-08-25 01:56:31

Openedge 内置了用于处理 SOAP 服务的语句,但没有用于 GET/POST 的简单语句。然而,它确实具有一种读/写特定套接字的机制。因此,您可以使用它来构建 HTTP post 例程,或者就此而言,构建一个处理任何其他基于套接字的协议的例程。

有一个例程 - http.p - 将为您执行 GET 操作。如果没有别的事情的话,会让你看看套接字编程是如何工作的。您应该能够很容易地修改它来执行简单的 POST,但使用 SSL 或进入身份验证可能需要相当多的工作。在这种情况下,直接退出 CURL 可能会更容易。

http.p 曾经可以从 freeframework.org 获得,但我刚刚检查过,该域已过期,所以我发布了下面的代码。

/*-----------------------------------------------------------------------*
  File........: http.p
  Version.....: 1.1
  Description : Makes a "Get" request from an HTTP server
  Input Param : pHost = host name (without the "http://")
                pPort = port number (usually 80)
                pURL =  begin with the first slash after the domain name.

  Output Param: pResult = 0 or 1 (character)
                pResponse = http headers returned
                pContent = the document returned.
  Author......: S.E. Southwell, Mario Paranhos -  United Systems, Inc. (770) 449-9696
  Created.....: 12/13/2000
  Notes.......: Will not work with HTTPS.
  Usage:
        define var v-result as char no-undo.
        define var v-response as char no-undo.
        define var v-content as char no-undo.
        {&out} "Hello" skip.
        put stream webstream control null(0).
        run proc/http.p("www.whosplayin.com","80","/",output v-result,output v-response,output v-content).
        {&out} v-result "<hr>" skip
        html-encode(v-response) "<hr>" skip
        html-encode(v-content) "<hr>" skip
        .

  Last Modified: 10/20/01 - SES - Fixed to work in batch mode, or within a UDF.
--------------------------------------------------------------------------*/



&SCOPED-DEFINE HTTP-NEWLINE CHR(13) + CHR(10)
&SCOPED-DEFINE RESPONSE-TIMEOUT 45

DEFINE INPUT PARAMETER pHOST        AS CHAR NO-UNDO.
DEFINE INPUT PARAMETER pPORT        AS CHAR NO-UNDO.
DEFINE INPUT PARAMETER pURL         AS CHAR NO-UNDO.
DEFINE OUTPUT PARAMETER pRESULT     AS CHAR NO-UNDO.
DEFINE OUTPUT PARAMETER pRESPONSE   AS CHAR NO-UNDO.
DEFINE OUTPUT PARAMETER pContent    AS CHAR NO-UNDO.


DEFINE VARIABLE requestString       AS CHAR   NO-UNDO.
DEFINE VARIABLE vSocket             AS HANDLE NO-UNDO.   
DEFINE VARIABLE vBuffer             AS MEMPTR NO-UNDO.
DEFINE VARIABLE vloop               AS LOGICAL NO-UNDO.
DEFINE VARIABLE vPackets            AS INTEGER NO-UNDO.
DEFINE VARIABLE wStatus             AS LOGICAL NO-UNDO.

ASSIGN requestString = "GET " + pURL + " HTTP/1.0" + {&HTTP-NEWLINE} +
          "Accept: */*" + {&HTTP-NEWLINE} + 
          "Host: " + phost + {&HTTP-NEWLINE} + 
          /*"Connection: Keep-Alive" + {&HTTP-NEWLINE} + */
          {&HTTP-NEWLINE}.

/*OPEN THE SOCKET*/
CREATE SOCKET vSocket.
vSocket:SET-READ-RESPONSE-PROCEDURE ("readHandler",THIS-PROCEDURE).
ASSIGN wstatus = vSocket:CONNECT("-H " + phost + " -S " + pport) NO-ERROR.

/*Now make sure the socket is open*/
IF wstatus = NO THEN DO:
    pResult = "0:No Socket".
    DELETE OBJECT vSocket.
    RETURN.
END.

/*Got socket - Now make HTTP request*/

SET-SIZE(vBuffer) = LENGTH(requestString) + 1.
PUT-STRING(vBuffer,1) = requestString.
vSocket:WRITE(vBuffer, 1, LENGTH(requestString)).
SET-SIZE(vBuffer) = 0.

/*Wait for a response*/
ASSIGN vloop = TRUE.  /*Turns off automatically when request is done*/
DEFINE VAR vstarttime AS INTEGER.
ASSIGN vstarttime = etime.

WAITLOOP: DO WHILE vloop:
    PROCESS EVENTS.
    PAUSE 1.
    /* Build in timer in case sending is never set to NO 
       this will terminate the program after 60 seconds
       start-Etime will be reset by WriteData each time there
       is activity on the socket to allow for long transmissions */
    IF vstarttime + ({&RESPONSE-TIMEOUT} * 1000) < ETIME 
     THEN DO:
        MESSAGE "timed out at " + string(etime - vstarttime) + " msec".
        vSocket:DISCONNECT().
        ASSIGN pResult = "0:Failure".
        RETURN.
    END. /*No Response, or timed out*/
END.



/*At this point, pResponse should be populated with the result (up to 32K)*/


vSocket:DISCONNECT().

DELETE OBJECT vSocket.
/*All Done!*/
ASSIGN pResult = "1:Success".
ASSIGN 
 pContent = SUBSTRING(pResponse,INDEX(pResponse,{&HTTP-NEWLINE} + {&HTTP-NEWLINE}),-1)
 .
ASSIGN
 pResponse = SUBSTRING(pResponse,1,INDEX(pResponse,{&HTTP-NEWLINE} + {&HTTP-NEWLINE}))
.

RETURN.


/*Handle the response from the webserver*/
PROCEDURE readHandler:
    DEFINE VARIABLE bytesAvail  AS INTEGER  NO-UNDO.
    DEFINE VARIABLE b           AS MEMPTR   NO-UNDO.
    DEFINE VARIABLE lastBytes   AS INTEGER  NO-UNDO.

    IF vSocket:connected() THEN ASSIGN bytesAvail = vSocket:GET-BYTES-AVAILABLE().

    IF bytesAvail = 0 THEN DO: /*All Done*/
      ASSIGN vloop = FALSE.
      RETURN.
    END.


    /*OK, there's something on the wire... Read it in*/    
    SET-SIZE(b) = bytesAvail + 1.
    vSocket:READ(b, 1, bytesAvail, 1).
    ASSIGN pResponse = pResponse + GET-STRING(b,1).
    SET-SIZE(b) = 0.
END PROCEDURE. /*readHandler*/

Openedge has built in statements for handling SOAP services, but no simple statement for a GET/POST. What it does have, however, is a mechanism to read / write to specific sockets. So you could use this to build a HTTP post routine, or for that matter a routine to handle any other socket based protocol.

There is a routine - http.p - which will do a GET for you. Will let you see how the socket programming works if nothing else. You should be able to modify it quite easily to do a simple POST, but using SSL or getting into authentication might take quite a bit of work. You might be easier just dropping out to CURL in this case.

http.p used to be available from freeframework.org, but I've just checked and that domain has expired, so I've posted the code below.

/*-----------------------------------------------------------------------*
  File........: http.p
  Version.....: 1.1
  Description : Makes a "Get" request from an HTTP server
  Input Param : pHost = host name (without the "http://")
                pPort = port number (usually 80)
                pURL =  begin with the first slash after the domain name.

  Output Param: pResult = 0 or 1 (character)
                pResponse = http headers returned
                pContent = the document returned.
  Author......: S.E. Southwell, Mario Paranhos -  United Systems, Inc. (770) 449-9696
  Created.....: 12/13/2000
  Notes.......: Will not work with HTTPS.
  Usage:
        define var v-result as char no-undo.
        define var v-response as char no-undo.
        define var v-content as char no-undo.
        {&out} "Hello" skip.
        put stream webstream control null(0).
        run proc/http.p("www.whosplayin.com","80","/",output v-result,output v-response,output v-content).
        {&out} v-result "<hr>" skip
        html-encode(v-response) "<hr>" skip
        html-encode(v-content) "<hr>" skip
        .

  Last Modified: 10/20/01 - SES - Fixed to work in batch mode, or within a UDF.
--------------------------------------------------------------------------*/



&SCOPED-DEFINE HTTP-NEWLINE CHR(13) + CHR(10)
&SCOPED-DEFINE RESPONSE-TIMEOUT 45

DEFINE INPUT PARAMETER pHOST        AS CHAR NO-UNDO.
DEFINE INPUT PARAMETER pPORT        AS CHAR NO-UNDO.
DEFINE INPUT PARAMETER pURL         AS CHAR NO-UNDO.
DEFINE OUTPUT PARAMETER pRESULT     AS CHAR NO-UNDO.
DEFINE OUTPUT PARAMETER pRESPONSE   AS CHAR NO-UNDO.
DEFINE OUTPUT PARAMETER pContent    AS CHAR NO-UNDO.


DEFINE VARIABLE requestString       AS CHAR   NO-UNDO.
DEFINE VARIABLE vSocket             AS HANDLE NO-UNDO.   
DEFINE VARIABLE vBuffer             AS MEMPTR NO-UNDO.
DEFINE VARIABLE vloop               AS LOGICAL NO-UNDO.
DEFINE VARIABLE vPackets            AS INTEGER NO-UNDO.
DEFINE VARIABLE wStatus             AS LOGICAL NO-UNDO.

ASSIGN requestString = "GET " + pURL + " HTTP/1.0" + {&HTTP-NEWLINE} +
          "Accept: */*" + {&HTTP-NEWLINE} + 
          "Host: " + phost + {&HTTP-NEWLINE} + 
          /*"Connection: Keep-Alive" + {&HTTP-NEWLINE} + */
          {&HTTP-NEWLINE}.

/*OPEN THE SOCKET*/
CREATE SOCKET vSocket.
vSocket:SET-READ-RESPONSE-PROCEDURE ("readHandler",THIS-PROCEDURE).
ASSIGN wstatus = vSocket:CONNECT("-H " + phost + " -S " + pport) NO-ERROR.

/*Now make sure the socket is open*/
IF wstatus = NO THEN DO:
    pResult = "0:No Socket".
    DELETE OBJECT vSocket.
    RETURN.
END.

/*Got socket - Now make HTTP request*/

SET-SIZE(vBuffer) = LENGTH(requestString) + 1.
PUT-STRING(vBuffer,1) = requestString.
vSocket:WRITE(vBuffer, 1, LENGTH(requestString)).
SET-SIZE(vBuffer) = 0.

/*Wait for a response*/
ASSIGN vloop = TRUE.  /*Turns off automatically when request is done*/
DEFINE VAR vstarttime AS INTEGER.
ASSIGN vstarttime = etime.

WAITLOOP: DO WHILE vloop:
    PROCESS EVENTS.
    PAUSE 1.
    /* Build in timer in case sending is never set to NO 
       this will terminate the program after 60 seconds
       start-Etime will be reset by WriteData each time there
       is activity on the socket to allow for long transmissions */
    IF vstarttime + ({&RESPONSE-TIMEOUT} * 1000) < ETIME 
     THEN DO:
        MESSAGE "timed out at " + string(etime - vstarttime) + " msec".
        vSocket:DISCONNECT().
        ASSIGN pResult = "0:Failure".
        RETURN.
    END. /*No Response, or timed out*/
END.



/*At this point, pResponse should be populated with the result (up to 32K)*/


vSocket:DISCONNECT().

DELETE OBJECT vSocket.
/*All Done!*/
ASSIGN pResult = "1:Success".
ASSIGN 
 pContent = SUBSTRING(pResponse,INDEX(pResponse,{&HTTP-NEWLINE} + {&HTTP-NEWLINE}),-1)
 .
ASSIGN
 pResponse = SUBSTRING(pResponse,1,INDEX(pResponse,{&HTTP-NEWLINE} + {&HTTP-NEWLINE}))
.

RETURN.


/*Handle the response from the webserver*/
PROCEDURE readHandler:
    DEFINE VARIABLE bytesAvail  AS INTEGER  NO-UNDO.
    DEFINE VARIABLE b           AS MEMPTR   NO-UNDO.
    DEFINE VARIABLE lastBytes   AS INTEGER  NO-UNDO.

    IF vSocket:connected() THEN ASSIGN bytesAvail = vSocket:GET-BYTES-AVAILABLE().

    IF bytesAvail = 0 THEN DO: /*All Done*/
      ASSIGN vloop = FALSE.
      RETURN.
    END.


    /*OK, there's something on the wire... Read it in*/    
    SET-SIZE(b) = bytesAvail + 1.
    vSocket:READ(b, 1, bytesAvail, 1).
    ASSIGN pResponse = pResponse + GET-STRING(b,1).
    SET-SIZE(b) = 0.
END PROCEDURE. /*readHandler*/
缱倦旧时光 2024-08-25 01:56:31

Progress Kbase ID:20011:“使用 4GL 套接字通过 HTTP 访问网站的示例代码”也是一个很好的通用示例。

Progress Kbase ID: 20011: "Sample Code To Access a Web Site via HTTP with 4GL Sockets" is also a nice, generic example.

一杯敬自由 2024-08-25 01:56:31

我建议使用上面 Gordon Roberertson 的代码示例,因为 Progress KB 文章中的“WAIT-FOR”被替换为循环。因此,如果出现任何问题,程序将在超时后终止。

请注意,更改 requestString 中的任何内容都可能导致超时。但是,如果您需要登录网络服务器,则可以添加用户代理:

ASSIGN requestString = "GET " + Path + " HTTP/1.0" + {&HTTP-NEWLINE}
               + "Accept: */*" + {&HTTP-NEWLINE}
               + "User-Agent: " + "User Agent String" + {&HTTP-NEWLINE}
               + "Host: " + Host + {&HTTP-NEWLINE}
               + {&HTTP-NEWLINE}.

感谢 Gordon 提供的代码示例。

I recommend using the code example of Gordon Roberertson above because there is the "WAIT-FOR" of the Progress KB article replaced with a loop. So the prcoedure terminates after timeout period if anything goes wrong.

Please note that changing anything in requestString can cause timeouts. But adding a User-Agent is possible if you need for logging on your webserver:

ASSIGN requestString = "GET " + Path + " HTTP/1.0" + {&HTTP-NEWLINE}
               + "Accept: */*" + {&HTTP-NEWLINE}
               + "User-Agent: " + "User Agent String" + {&HTTP-NEWLINE}
               + "Host: " + Host + {&HTTP-NEWLINE}
               + {&HTTP-NEWLINE}.

Thanks to Gordon for his code example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文