How to Use CGI for Program-to-Program Communications

Article ID: 17890

A few days ago on the iSeries Network forums, someone asked for a quick way to have PC programs request information from an iSeries server. He stated that he did not want to use iSeries Access and wanted to be able to support hundreds of PCs at a time.

My suggestion was to write a CGI script. Although a CGI script is normally used to build Web pages for display in a browser, it can also be used in an application to simply pass data from program to program.

To demonstrate this technique, I've created a CGI script written in ILE RPG called CGIDEMO. CGIDEMO reads a user-id from a PC program, looks up the last sign-on date for that user, and returns it to the PC program.

The following Java program demonstrates the PC side of the conversation:

import java.net.*;
import java.io.*;

public class GetLastSignon {
   public static void main(String[] args) throws Exception {
 /* CHANGE THE URL BELOW TO THE CORRECT ONE FOR YOUR SYSTEM! */
       URL iseries = 
new URL("http://as400.example.com./cgi-bin/CGIDEMO.PGM?userid=YOURNAME");
       BufferedReader in = new BufferedReader(
                               new InputStreamReader(
                               iseries.openStream()));

       String signon = in.readLine();
       in.close();

       System.out.println(signon);
   }
}

You can download the Java code above, as well as the RPG CGI script and its dependencies, from http://www.iseriesnetwork.com/noderesources/code/clubtechcode/CgiP2P.zip

ProVIP Sponsors

ProVIP Sponsors