WebService class



Availability

Flash Media Server 2

Description

You can use the WebService class to create and access a WSDL/SOAP web service. Several classes comprise the Flash Media Interactive Server web services feature: WebService class, SOAPFault class, SOAPCall class, and Log class.

Note: The WebService class is not able to retrieve complex data or an array returned by a web service. Also, the WebService class does not support security features.

The following steps outline the process of creating and accessing a web service.

Create and access a web service:

  1. Load the WebServices class:

    load("webservices/WebServices.asc");
  2. Prepare the WSDL location:

    var wsdlURI = "http://www.flash-db.com/services/ws/companyInfo.wsdl";
  3. Instantiate the web service object by using the WSDL location:

    stockService = new WebService(wsdlURI);
  4. (Optional) Handle the WSDL parsing and web service instantiation event through the WebService.onLoad() handler:

    // Handle the WSDL loading event. 
    stockService.onLoad = function(wsdl){ 
        wsdlField.text = wsdl; 
    }
  5. (Optional) If the WSDL doesn’t load, handle the fault:

    // If the WSDL fails to load, the onFault event is fired. 
    stockService.onFault = function(fault){ 
        wsdlField.text = fault.faultstring; 
    }
  6. (Optional) Set the SOAP headers:

    // If headers are required, they are added as follows: 
    var myHeader = new XML(headerSource); 
    stockService.addHeader(myHeader);
  7. Invoke a web service operation:

    // Method invocations return an asynchronous callback. 
    callback = stockService.doCompanyInfo("anyuser", "anypassword", "ADBE"); 
    // NOTE: callback is undefined if the service itself is not created 
    // (and service.onFault is also invoked).
  8. Handle either the output or the error fault returned from the invocation:

    // Handle a successful result. 
    callback.onResult = function(result){ 
        // Receive the SOAP output, which in this case  
        // is deserialized as a struct (ActionScript object). 
        for (var i in result){ 
                 trace(i +" : " +result[i]); 
        } 
    } 
    // Handle an error result. 
    callback.onFault = function(fault){ 
        // Catch the SOAP fault and handle it  
        // according to this application's requirements. 
        for (var i in fault){ 
            trace(i +" : " +fault[i]); 
        } 
    }

Event handler summary

Event handler

Description

WebService.onFault()

Invoked when an error occurs during WSDL parsing.

WebService.onLoad()

Invoked when the web service has successfully loaded and parsed its WSDL file.

WebService constructor

new WebService(wsdlURI)

Creates a new WebService object. You must use the constructor to create a WebService object before you call any of the WebService class methods.

Availability

Flash Media Server 2

Parameters

wsdlURI
A string specifying the URI of a WSDL.

Returns

A WebService object.

Example

The following example prepares the WSDL location and passes it to the WebService constructor to create a new WebService object, stockService:

load("webservices/WebServices.asc"); 
var wsdlURI = "http://www.flash-db.com/services/ws/companyInfo.wsdl"; 
stockService = new WebService(wsdlURI);

WebService.onFault()

myWS.onFault(fault){}

Invoked when an error occurs during WSDL parsing. The web services features convert parsing and network problems into SOAP faults for simple handling.

Availability

Flash Media Server 2

Parameters

fault
An object version of an XML SOAP fault (see SOAPFault class).

Example

The following example displays the fault code in a text field if the WSDL fails to load and the onFault() event fires:

// Load the WebServices class: 
load("webservices/WebServices.asc"); 
 
// Prepare the WSDL location: 
var wsdlURI = "http://www.flash-db.com/services/ws/companyInfo.wsdl"; 
 
// Instantiate the web service object by using the WSDL location:  
stockService = new WebService(wsdlURI); 
 
// Handle the WSDL parsing and web service instantiation event: 
stockService.onLoad = function(wsdl){ 
    wsdlField.text = wsdl; 
} 
 
// If the WSDL fails to load, the onFault event is fired: 
stockService.onFault = function(fault){ 
    wsdlField.text = fault.faultstring; 
}

WebService.onLoad()

myWS.onLoad(wsdldocument)

Invoked when the web service has successfully loaded and parsed its WSDL file. Operations can be invoked in an application before this event occurs; when this happens, they are queued internally and are not actually transmitted until the WSDL has loaded.

Availability

Flash Media Server 2

Parameters

wsdldocument
A WSDL XML document.

Example

In the following example, the onLoad event is used to handle the WSDL parsing:

// Load the WebServices class: 
load("webservices/WebServices.asc"); 
 
// Prepare the WSDL location: 
var wsdlURI = "http://www.flash-db.com/services/ws/companyInfo.wsdl"; 
 
// Instantiate the web service object by using the WSDL location:  
stockService = new WebService(wsdlURI); 
 
// Handle the WSDL parsing and web service instantiation event: 
stockService.onLoad = function(wsdl){ 
    wsdlField.text = wsdl; 
}