Requesting web services using an HttpClient

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:

  • Authorize.net Payment Services
  • ClearCommerce Payment Services
  • Go Software RiTA
  • ValueLink Prepaid/Gift Card Payment Services
  • DHL, FedEx, United Parcel Services (UPS), and United States Post Office Shipping Services
  • CDYNE Web Based Services
  • PayPal Payments Pro

Getting ready

The first step in writing any web service client is to gather the following information about how the target web service operates:

  • The URL on the web for the service provider
  • Any connection parameters and/or HTTP/HTTPS request message header settings that must be passed as required by the service provider
  • The HTTP/HTTPS connection verb (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;

How to do it...

You can request a web service by following these steps:

  1. Create a connection string that includes the URL of the target web service and the type of request:
    String connectString =
    "http://www.some_web_service_url.com/serviceName";
    
  2. Within your Java program, create an instance of the HttpClient object with the URL/connection string passed to the constructor as shown here:
    HttpClient http = new HttpClient(connectString);
    
  3. Create the content of your request as dictated by the target web service provider. For example, some web services expect XML documents; others, simple string parameters. A web service that expects a string of name/value pairs could be coded as follows:
    http.setParameter("Param1", "X");
    http.setParameter("Param2", "Y");
    
  4. Send your request using the appropriate method on the 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
    }
    
  5. Handle any service response inline. Unlike the asynchronous nature of the PayPal IPN web service described earlier, 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.
  6. There is no need to release or close the connection as OFBiz manages the handoff of connections.

How it works...

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.

There's more...

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"));

See also

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:

http://hc.apache.org/httpclient-3.x/

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.250.11