Flex's Remoting/RPC: HTTP Service

http://www.flexafterdark.com/docs/Flex-HttpService

HTTP Service

Flex's HTTPService is used to make HTTP requests and handle the results. When you call the HTTPService object's send() method, it makes an HTTP request to the specified URL, and an HTTP response is returned (asynchronously).

The HTTPService is part of Flex's Remoting/RPC library.

MXML HTTPService

   <mx:HTTPService id="httpInMXML" url="http://www.flexafterdark.com"
      method="GET" resultFormat="text"
      result="resultHandler(event)" fault="faultHandler(event)"
   />

When working with a <mx:HTTPService> component, it is an instance ofmx.rpc.html.mxml.HTTPService. The MXML version of HTTPService is an extension with some facilities for making MXML property definition and event handling more convenient.

ActionScript HTTPService

The below example demonstrates using an HTTPService and handling the result/fault with registered event listeners.

   var http:HTTPService = new HTTPService();
   
   // register event handlers (resultHandler and faultHandler functions)
   http.addEventListener( ResultEvent.RESULT, resultHandler );
   http.addEventListener( FaultEvent.FAULT, faultHandler );
   
   // specify the url to request, the method and result format
   http.url = "http://www.flexafterdark.com";
   http.method = "GET";
   http.resultFormat = "text";
   
   // send the request
   http.send();

Making an HTTP request in ActionScript:

  1. Create an instance of mx.rpc.http.HttpService
  2. Set the HTTP url
  3. Set the result format
  4. Send a request
  5. Handle the result or fault
    1. via pre-registered event listeners
    2. or via responders

The below example demonstrates using an HTTPService and handling the result/fault with a token and responder.

   var http:HTTPService = new HTTPService();
   
   // specify the url to request, the method and result format
   http.url = "http://www.flexafterdark.com";
   http.method = "GET";
   http.resultFormat = "text";
   
   // call the HTTP Service's send() to invoke the request, a token is returned
   var token:AsyncToken = http.send( params );
   
   // setup responder (resultHandler and faultHandler functions) and add to token
   var responder:AsyncResponder = new AsyncResponder( resultHandler, faultHandler );
   token.addResponder( responder );

The HTTPService constructor accepts a rootURL parameter. Do not confuse the rootURL constructor argument with the url property that will be used for a send() request. Even if you set a rootURL in the constructor you'll still need to set the url property. The rootURL is used only if you set the url property to a relative URL.

Passing Parameters

The HTTPService's send() function accepts an optional parameters object.

   // create parameters object
   var params:Object = { abc: "123", def: "456" };
   
   // call the HTTP Service's send() with a parameters object
   http.send( params );

Methods and Results

Not all HTTP methods are supported, but the most common (GET and POST) are.

HTTP Methods:

  • GET - fetches the contents of a URL
  • POST - submits data to be processed (i.e. used to make form submissions)
  • HEAD - (see HTTPService documentation for more information)
  • OPTIONS - (see HTTPService documentation for more information)
  • TRACE - (see HTTPService documentation for more information)
  • DELETE - (see HTTPService documentation for more information)

When you do not go through the server-based proxy service, you can use only HTTP GET or POST methods.

The result format determines how you want to deserialize the result returned by the HTTP call.

Result Formats:

  • object - default value (see HTTPService documentation for more information)
  • text - use for text or for HTML (if the HTML is not strict XHTML)
  • e4x - use for XML and XHTML
  • xml - use for XML and XHTML
  • array - (see HTTPService documentation for more information)
  • flashvars - (see HTTPService documentation for more information)

Related Links:

posted @ 2011-12-13 14:02  Stranger  阅读(183)  评论(0编辑  收藏  举报