Skip to main content

Issuing HTTP Requests from a Net Express Application

  • February 15, 2013
  • 0 replies
  • 0 views

Problem:

It is possible for Net Express Windows applications to issue HTTP requests and interact with Web applications. There are a number of ways that an application can make use of this technology. For example, POST could be used to send data to a remote Web Server for processing locally at that site. An application could also "screen scrape" HTML pages and parse the information for use within the application. As long as the server supports http then an application should be able to interact with it.

Resolution:

There are a number of ways you can do this. In order of difficulty the available options are:-

-

TCP Sockets APIs. Windows has a Sockets API that could be called. This however would have a very high learning curve to learn both the Sockets API and also the HTTP protocol in order to interact with a Web Server.

-

Windows Internet API (WinInet). This is a higher level API and hides the lower level network protocol. It allows an application to interact with servers that support http , gopher and ftp. It is still based on API calls so there is again a rather steep learning curve. On its plus side this does have support for Dial Up networking and is supported on all Microsoft Win32 based operating systems (Win9x and WinNT/2000/XP/2003).

-

Windows HTTP Services (WinHTTP).  This API has a COM Object model for it that provides the most simple mechanism for issuing HTTP requests. In just a couple of COM method calls an application can start issuing requests and posting data. This API is also optimized for server based applications and is scalable so can be used in server side applications and within Windows Services. WinInet is not scalable and is designed for client side applications that do not need to scale. The downside of this mechanism is that it is Windows 2000 and above only (Win2000/XP/2003). For Win9x support Wininet must be used.

-

.Net HttpWebRequest objects. If the target is the .Net platform with Net Express for .Net then there are a number of classes in the System.Net namespace for issuing HTTP requests.

This article and the associated demos will deal with the WinHTTP and .Net mechanisms.

The mechanics of calling WinHTTP are just some simple COBOL constructs:-

       class-control.

           WinHTTP           is class "$OLE$WinHttp.WinHttpRequest.5.1"

These couple of lines define the COM Components that need to be called. The $OLE$ prefix tells the COBOL runtime that this is a COM Object and not a native COBOL object. The "WinHttp.WinHttpRequest.5.1" text is the Microsoft defined identifier for the component.

Next an instance of the COM component must be created. This is simply:-

           invoke WinHTTP "new" returning ws-winhttp

The simplest request type is a GET request. This is suitable for passing small amounts of data to a Web Server. For example to retrieve the Micro Focus web page you could do:-

           invoke ws-winhttp "Open" using z"GET"

                                          z"http://www.microfocus.com"

                                          ws-boolfalse

           invoke ws-winhttp "Send"

           invoke ws-winhttp "getResponseText" returning ws-text

These simple calls issue a GET request to the Web Server and receive the results into the PIC X(...) field ws-text.

If sending a large amount of data to the Web Server (eg a large XML file) then you need to use the POST method of sending data. The code required is similar to the GET request:-

      ***** Do the work

           invoke ws-winhttp "Open" using z"POST"

                                          open-string

                                          ws-boolfalse  *> syncron

                                returning ws-res

      ****** Supply the request string as a Variant to the method.

           initialize ws-len

           inspect req-string tallying ws-len for characters

                   before initial low-value

           invoke olevariant "new" returning req-string-var

           invoke req-string-var "setString" using by value ws-len

                                       by reference req-string

                   returning ws-res

           invoke ws-winhttp "Send" using req-string-var

                                returning ws-res

           display req-string.

           invoke ws-winhttp "getResponseText" returning ws-text

There is an extra parameter to the "Send" method and this is the actual data that is posted to the Web Server. The data being passed is wrapped up as a string in a variant parameter. A COM variant is a data type that can store different types of data. It holds both the underlying data type and the actual data.

Again the results from the POST request are returned into the ws-text data item.

If targeting the .Net platform then again some OO Syntax must be used:-

            set myHttp to cWebRequest::"Create"("http://www.microfocus.com") as cHttpWebRequest

            set myResponse to myHttp::"GetResponse"() as cWebResponse

            set myStreamReader to cStreamReader::"New"(myResponse::"GetResponseStream"())

            set mySiteContent to myStreamReader::"ReadToEnd"()

This highlights some new syntax from the new COBOL standard, myHttp::"GetResponse"(). This is called an InLine method call. In this case meyHttp is the object and GetResponse is the method that is to be called. These could of course be replaced by INVOKE verbs.

There are some demo projects that allow the trial of some of the techniques detailed in this article. The WinHTTP POST demo requires a CGI application to post the data to so there is a simple COBOL CGI Project that can be used. This project must be loaded and working in Net Express before the WinHTTP POST Demo can be run.

The supplied example projects are:-

CGITestApp.zip

- Test CGI for use with WinHTTP Post

COBOLDOTNETHttp.zip

- Test COBOL .Net Http example

WinHTTPGET.zip

- Example WinHTTP Get sample

WinHTTPPOST.zip

- Example WinHTTP POST sample

These files can be found under the Attachments list below.

In order to run the .Net sample Net Express with .Net must be installed.

The documentation on the various objects used here is available at:-

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wininet/wininet/portal.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnanchor/html/winhttp.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemNetHttpWebRequestClassTopic.asp

Attachments:

CGITestApp.zip

Old KB# 5229

0 replies

Be the first to reply!