Month: July 2015

Pinging and Posting from PeopleCode

I had a need to Ping a server to see if the server could get to it.  I also tried to post to it.  This code could be helpful for others, so I want to share it.  A post should normally go through the Integration Broker, but I first developed it at a time when I had a product that was supposed to go on servers with diverse versions of Integration Broker.

The Ping code doesn’t seem very reliable for some reason.  Something on the Java side doesn’t always work.  Still it might be helpful.

   Local JavaObject &url;
   Local JavaObject &conn;
   Local JavaObject &r;
   Local any &line;
   Local string &output;
   Local JavaObject &inet;
   Local string &address;

   MessageBox(0, "", 0, 0, "hostname: " | GetJavaClass("java.net.InetAddress").getLocalHost().getHostName());

   &address = "localhost";
   &inet = GetJavaClass("java.net.InetAddress").getByName(&address);
   MessageBox(0, "", 0, 0, &address | "(" | &inet.getHostAddress() | ") reachable: " | &inet.isReachable(5000));
   &address = "www.google.com";
   &inet = GetJavaClass("java.net.InetAddress").getByName(&address);
   MessageBox(0, "", 0, 0, &address | "(" | &inet.getHostAddress() | ") reachable: " | &inet.isReachable(5000));

   &url = CreateJavaObject("java.net.URL", "http://www.google.com");
   &conn = &url.openConnection();
   &conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
   &conn.setRequestProperty("accept", "text/xml/html");
   &conn.setRequestProperty("accept-charset", "utf-8, iso_8859-1");
   &conn.setRequestProperty("userid", "????");
   &conn.setRequestProperty("pwd", "?????");
   &conn.setDoOutput( True);

   &output = "";
   &r = CreateJavaObject("java.io.BufferedReader", CreateJavaObject("java.io.InputStreamReader", &conn.getInputStream()));
   &line = &r.readLine();
   While &line <> Null
      &output = &output | Char(10) | Char(13) | &output;
      &line = &r.readLine();
   End-While;
   &r.close();

   MessageBox(0, "", 0, 0, &output);

Please make sure to adjust the URLs and hostnames to what you need.

Resources