Many web services are implemented using HTTP/HTTPS request methods and parameters passed as part of the request's message body. These requests for service mimic a user sitting at a browser submitting HTML forms and waiting for server response. Service providers read HTTP/HTTPS request header information and name/value request message parameter pairs, and deliver service through the HTTP/HTTPS response message.
Writing clients for these types of web services usually require a synchronous call to the service provider. From within your OFBiz client code, you initiate a call to a service provider and then wait until a response (or timeout) is received. Unlike the PayPal Payments Standard service described earlier, the OFBiz client program does not redirect HTTP/HTTPS request messages to another URL.
There are a number of examples within the out-of-the-box OFBiz project of service providers that use the HTTP/HTTPS request message body to exchange information with OFBiz clients. They include, but are not limited to:
The first step in writing any web service client is to gather the following information about how the target web service operates:
get, post
, or other)Within a Java program, to send and receive web service messages as a client and use the built-in HTTP client utility provided by OFBiz, make sure you have the following Java packages imported in your program:
import org.ofbiz.base.util.HttpClient; import org.ofbiz.base.util.HttpClientException;
You can request a web service by following these steps:
String connectString = "http://www.some_web_service_url.com/serviceName";
HttpClient
object with the URL/connection string passed to the constructor as shown here:HttpClient http = new HttpClient(connectString);
http.setParameter("Param1", "X"); http.setParameter("Param2", "Y");
HttpClient
object. For "get" requests, use the get
method. For "post" requests, use the post
method as shown here:try { response = http.post(); } catch (HttpClientException e) { // Process error conditions here }
HttpClient
based web services process return calls inline with the initial web service call. Under the covers, the HttpClient
utility handles all the network connection set up and lower-level message transmissions.When using the HttpClient
to access web services remote to OFBiz, you send the consumer-side call synchronously; that is, you wait for the return from the remote web service call within your program. The OFBiz integration of the HttpClient
utility manages the details necessary to open the network connection, maintain direct request and response message exchanges, and close the connection upon completion of processing.
The OFBiz implementation of the HttpClient
object provides several convenience constructors, which may be useful depending on your processing needs. These include:
// To create a new client object and connect using a URL object // instead of a String URL url = "https://www.some_host.com/"; HttpClient http = new HttpClient(url); // To create a new client object using a Java Map containing // request parameters HttpClient http = new HttpClient(url, UtilMisc.toMap("param1", "X", "param2", "Y"); //To create a new client object with a parameter map and // header settings HttpClient http = new HttpClient(connectString, UtilMisc.toMap("param1", "X"), UtilMisc.toMap("User-Agent, "Mozilla/4.0"));
OFBiz provides an integration of the Apache HttpClient
software package: the Jakarta Commons HTTP Client that is accessed by creating a new HttpClient
object. Any method you can call on, the original Apache HttpClient
object is available in the OFBiz implementation. This includes full support for HTTPS (SSL) clients. For more information, please see the Jakarta Commons HTTP Client web page:
3.149.250.11