Skip to main content

Problem:

Is there a way to download web pages via HTTP from a COBOL program under UNIX ?

Resolution:

This can be done via calls to the libcurl library.

Libcurl is a library for transfering data with URL syntax, supporting FTP, FTPS, HTTP, HTTPS, TFTP, GOPHER, TELNET, DICT, FILE and LDAP.

Here is an example of calling libcurl functions:

      *

      * Demo showing how to make an http request from a COBOL program

      *    using libcurl.

      * libcurl can be found at http://sourceforge.net/projects/curl/

      * libcurl documentation can be found at http://curl.haxx.se/libcurl/

      *

      * compile as follows:

      *    cob -x urltest.cbl -L/usr/lib -lcurl -L/usr/ssl/lib -lssl

      *        -lcrypto -ldl

      *

       Program-ID. urltest.

       Working-Storage Section.

       01  Curl-Handle                     pointer.

       01  Bad-Handle                      pointer.

       78  CURLOPT-NOTHING                 value     0.

       78  CURLOPT-FILE                    value 10001.

       78  CURLOPT-URL                     value 10002.

       78  CURLOPT-PORT                    value     3.

       78  CURLOPT-ERRORBUFFER             value 10010.

       78  CURLOPT-WRITEFUNCTION           value 20011.

       78  CURLOPT-CRLF                    value    27.

       78  CURLOPT-VERBOSE                 value    41.

       78  CURLOPT-HEADER                  value    42.

       78  CURLOPT-NOPROGRESS              value    43.

       78  CURLOPT-NOBODY                  value    44.

       78  CURLOPT-HEADER-YES              value     1.

       78  CURLOPT-HEADER-NO               value     0.

       01  Curl-URL                        pic x(256).

       01  Writefunction-Ptr               procedure-pointer.

       01  Curl-Error-Text                 pic x(256).

       01  Ws-Error                        pic x(40).

       01  Web-Data                        pic x(65536).

       01  Web-Data-Len                    pic x(04) comp-5 value 0.

       01  Web-Data-Max                    pic x(04) comp-5.

       01  Total-Size                      pic x(04) comp-5.

       01  Bytes-Read                      pic x(04) comp-5.

       Linkage Section.

       01  Data-In                         pic x(04) comp-5.

       01  Data-Elem-Size                  pic x(04) comp-5.

       01  Data-Elem-Num                   pic x(04) comp-5.

       01  Unused-Ptr                      pointer.

       Procedure Division.

       0000-Main.

           perform 1000-Init.

           perform 2000-Setup.

           perform 3000-Get-URL.

           perform 9999-Cleanup.

           display "Received " Web-Data-Len " bytes from " Curl-URL.

           display " ".

           display "First 80 characters:".

           display Web-Data(1:80).

           display " ".

           display "Last 80 characters:".

           display Web-Data(Web-Data-Len - 80:80).

           stop run.

       1000-Init.

           set Curl-Handle to null.

           set Bad-Handle  to null.

           call "curl_easy_init"

               returning Curl-Handle.

           if Curl-Handle = Bad-Handle

               move "Easy Init Error" to Ws-Error

               perform 9000-Error

           end-if.

       2000-Setup.

           call "curl_easy_setopt"

               using by value     Curl-Handle

                     by value     CURLOPT-ERRORBUFFER size 4

                     by reference Curl-Error-Text.

           if Return-Code not = 0

               move "Set Option ErrorBuffer Error" to Ws-Error

               perform 9000-Error

           end-if.

           call "curl_easy_setopt"

               using by value     Curl-Handle

                     by value     CURLOPT-HEADER    size 4

                     by value     CURLOPT-HEADER-NO size 4.

           if Return-Code not = 0

               move "Set Option Header Error" to Ws-Error

               perform 9000-Error

           end-if.

           set Writefunction-Ptr to entry "curl-callback".

           call "curl_easy_setopt"

               using by value     Curl-Handle

                     by value     CURLOPT-WRITEFUNCTION size 4

                     by value     Writefunction-Ptr.

           if Return-Code not = 0

               move "Set Option Writefunction Error" to Ws-Error

               perform 9000-Error

           end-if.

           move z"http://www.microfocus.com"   to Curl-URL.

           call "curl_easy_setopt"

               using by value     Curl-Handle

                     by value     CURLOPT-URL size 4

                     by reference Curl-URL.

           if Return-Code not = 0

               move "Set Option URL Error" to Ws-Error

               perform 9000-Error

           end-if.

       3000-Get-URL.

           call "curl_easy_perform"

               using by value     Curl-Handle.

           if Return-Code not = 0

               move "Perform Error" to Ws-Error

               perform 9000-Error

           end-if.

       9000-Error.

           display Ws-Error.

           display "Return-Code: " Return-Code.

           display "Error Text: " Curl-Error-Text.

           perform 9999-Cleanup.

           goback.

       9999-Cleanup.

           call "curl_easy_cleanup"

               using by value Curl-Handle.

       Program-Termination.

       entry "curl-callback"

           using by reference Data-In

                 by value     Data-Elem-Size

                 by value     Data-Elem-Num

                 by value     Unused-Ptr.

       E1000-Buffer-Data.

           compute Total-Size =   (Data-Elem-Size * Data-Elem-Num).

           compute Web-Data-Max = (length of Web-Data - Web-Data-Len).

           if Total-Size > Web-Data-Max

               move Web-Data-Max to Total-Size

           end-if.

           move Data-In(1:Total-Size)

                to Web-Data(Web-Data-Len 1:Total-Size).

           add Total-Size to Web-Data-Len.

           move Total-Size to Bytes-Read.

           goback returning Bytes-Read.

       Entry-Termination.

Old KB# 2135

Problem:

Is there a way to download web pages via HTTP from a COBOL program under UNIX ?

Resolution:

This can be done via calls to the libcurl library.

Libcurl is a library for transfering data with URL syntax, supporting FTP, FTPS, HTTP, HTTPS, TFTP, GOPHER, TELNET, DICT, FILE and LDAP.

Here is an example of calling libcurl functions:

      *

      * Demo showing how to make an http request from a COBOL program

      *    using libcurl.

      * libcurl can be found at http://sourceforge.net/projects/curl/

      * libcurl documentation can be found at http://curl.haxx.se/libcurl/

      *

      * compile as follows:

      *    cob -x urltest.cbl -L/usr/lib -lcurl -L/usr/ssl/lib -lssl

      *        -lcrypto -ldl

      *

       Program-ID. urltest.

       Working-Storage Section.

       01  Curl-Handle                     pointer.

       01  Bad-Handle                      pointer.

       78  CURLOPT-NOTHING                 value     0.

       78  CURLOPT-FILE                    value 10001.

       78  CURLOPT-URL                     value 10002.

       78  CURLOPT-PORT                    value     3.

       78  CURLOPT-ERRORBUFFER             value 10010.

       78  CURLOPT-WRITEFUNCTION           value 20011.

       78  CURLOPT-CRLF                    value    27.

       78  CURLOPT-VERBOSE                 value    41.

       78  CURLOPT-HEADER                  value    42.

       78  CURLOPT-NOPROGRESS              value    43.

       78  CURLOPT-NOBODY                  value    44.

       78  CURLOPT-HEADER-YES              value     1.

       78  CURLOPT-HEADER-NO               value     0.

       01  Curl-URL                        pic x(256).

       01  Writefunction-Ptr               procedure-pointer.

       01  Curl-Error-Text                 pic x(256).

       01  Ws-Error                        pic x(40).

       01  Web-Data                        pic x(65536).

       01  Web-Data-Len                    pic x(04) comp-5 value 0.

       01  Web-Data-Max                    pic x(04) comp-5.

       01  Total-Size                      pic x(04) comp-5.

       01  Bytes-Read                      pic x(04) comp-5.

       Linkage Section.

       01  Data-In                         pic x(04) comp-5.

       01  Data-Elem-Size                  pic x(04) comp-5.

       01  Data-Elem-Num                   pic x(04) comp-5.

       01  Unused-Ptr                      pointer.

       Procedure Division.

       0000-Main.

           perform 1000-Init.

           perform 2000-Setup.

           perform 3000-Get-URL.

           perform 9999-Cleanup.

           display "Received " Web-Data-Len " bytes from " Curl-URL.

           display " ".

           display "First 80 characters:".

           display Web-Data(1:80).

           display " ".

           display "Last 80 characters:".

           display Web-Data(Web-Data-Len - 80:80).

           stop run.

       1000-Init.

           set Curl-Handle to null.

           set Bad-Handle  to null.

           call "curl_easy_init"

               returning Curl-Handle.

           if Curl-Handle = Bad-Handle

               move "Easy Init Error" to Ws-Error

               perform 9000-Error

           end-if.

       2000-Setup.

           call "curl_easy_setopt"

               using by value     Curl-Handle

                     by value     CURLOPT-ERRORBUFFER size 4

                     by reference Curl-Error-Text.

           if Return-Code not = 0

               move "Set Option ErrorBuffer Error" to Ws-Error

               perform 9000-Error

           end-if.

           call "curl_easy_setopt"

               using by value     Curl-Handle

                     by value     CURLOPT-HEADER    size 4

                     by value     CURLOPT-HEADER-NO size 4.

           if Return-Code not = 0

               move "Set Option Header Error" to Ws-Error

               perform 9000-Error

           end-if.

           set Writefunction-Ptr to entry "curl-callback".

           call "curl_easy_setopt"

               using by value     Curl-Handle

                     by value     CURLOPT-WRITEFUNCTION size 4

                     by value     Writefunction-Ptr.

           if Return-Code not = 0

               move "Set Option Writefunction Error" to Ws-Error

               perform 9000-Error

           end-if.

           move z"http://www.microfocus.com"   to Curl-URL.

           call "curl_easy_setopt"

               using by value     Curl-Handle

                     by value     CURLOPT-URL size 4

                     by reference Curl-URL.

           if Return-Code not = 0

               move "Set Option URL Error" to Ws-Error

               perform 9000-Error

           end-if.

       3000-Get-URL.

           call "curl_easy_perform"

               using by value     Curl-Handle.

           if Return-Code not = 0

               move "Perform Error" to Ws-Error

               perform 9000-Error

           end-if.

       9000-Error.

           display Ws-Error.

           display "Return-Code: " Return-Code.

           display "Error Text: " Curl-Error-Text.

           perform 9999-Cleanup.

           goback.

       9999-Cleanup.

           call "curl_easy_cleanup"

               using by value Curl-Handle.

       Program-Termination.

       entry "curl-callback"

           using by reference Data-In

                 by value     Data-Elem-Size

                 by value     Data-Elem-Num

                 by value     Unused-Ptr.

       E1000-Buffer-Data.

           compute Total-Size =   (Data-Elem-Size * Data-Elem-Num).

           compute Web-Data-Max = (length of Web-Data - Web-Data-Len).

           if Total-Size > Web-Data-Max

               move Web-Data-Max to Total-Size

           end-if.

           move Data-In(1:Total-Size)

                to Web-Data(Web-Data-Len 1:Total-Size).

           add Total-Size to Web-Data-Len.

           move Total-Size to Bytes-Read.

           goback returning Bytes-Read.

       Entry-Termination.

Old KB# 2135

howto use this on windows and netexpress 3.0 and libcurl.dll ???