OpenWGA 7.2 - TMLScript reference

WGA
Method :

createHttpClient()

On object WGA
Usage Creates an object to do custom HTTP requests
Description

The returned object is an instance of the Jakarta Commons HTTP Client, a Java class providing extensive functionality to perform HTTP requests. If a proxy is configured for the system the client object will already be configured to use it.

See Accessing Java and the WGAPI on how to use Java objects in TMLScript.

Return value A new Jakarta Commons HTTP Client object
Examples

A little example executing a basic HTTP GET method via Http Client.

var client = WGA.createHttpClient();
var getMethod = new Packages.org.apache.commons.httpclient.methods.GetMethod("http://www.openwga.com");

try {
  var statusCode = client.executeMethod(getMethod);

  if (statusCode != 200) {
    log.error("Method failed: " + getMethod.getStatusLine());
    return;
  }

  var responseBody = getMethod.getResponseBodyAsString();
  return responseBody;
}
finally {
  getMethod.releaseConnection();
}

The HTTP Client object is designed to be used inside Java, involving many objects to be created via constructor and constants spread among various classes, so the usage in TMLScript may not be very straight-forward.

Generally a request is executed by instantiating a method class,  here "GetMethod", giving it a URL in the constructor and passing it on to the method "executeMethod" of the HTTP client. The return value is the HTTP status returned for the request. The body of the response may get returned as string, which is not recommended with large responses, or as Java stream.

Always call "releaseConnection" on the method object once you are finished working with it, so all network resources can be freed early.