Package | co.uk.mikestead.net |
Class | public class URLRequestBuilder |
URLRequestBuilder
class takes an instance of URLVariables
and wraps these variables in a URLRequest
with the appropriate HTTP encoding.
This class is needed to build URLRequest objects with encodings not automatically applied
when the URLRequest.data
property is set.
To determine the correct encoding to apply, each variable in the URLVariables
instance is examined. If a variable of type URLFileVariable
is found
then the encoding is set to multipart/form-data
, and the
URLRequest.method
set to POST. If no URLFileVariable
is found
then the URLRequest.data
property is set with the URLVariables
instance directly. To see the encoding used in this case refer to the documentation for the
URLRequest.data
property
// Construct variables (name-value pairs) to be sent to sever var variables:URLVariable = new URLVariables(); variables.userImage = new URLFileVariable(jpegEncodedData, "user_image.jpg"); variables.userPDF = new URLFileVariable(pdfEncodedData, "user_doc.pdf"); variables.userName = "Mike"; // Build the request which houses these variables var request:URLRequest = new URLRequestBuilder(variables).build(); request.url = "some.web.address.php"; // Create the loader and use it to send the request off to the server var loader:URLLoader = new URLLoader(); loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError); loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onError); loader.addEventListener(IOErrorEvent.IO_ERROR, onError); loader.addEventListener(Event.COMPLETE, onServerResponse); loader.load(request); function onServerResponse(event:Event):void { trace("Variables uploaded successfully"); } function onError(event:Event):void { trace("An error occurred while trying to upload data to the server: \n" + event); }
See also
Method | Defined by | ||
---|---|---|---|
URLRequestBuilder(variables:URLVariables)
Constructor.
| URLRequestBuilder | ||
build():URLRequest
Build a URLRequest instance with the correct encoding given the URLVariables
provided to the constructor.
| URLRequestBuilder |
URLRequestBuilder | () | constructor |
public function URLRequestBuilder(variables:URLVariables)
Constructor.
Parametersvariables:URLVariables — The URLVariables to encode within a URLRequest.
|
build | () | method |
public function build():URLRequest
Build a URLRequest instance with the correct encoding given the URLVariables provided to the constructor.
ReturnsURLRequest — URLRequest instance primed and ready for submission
|