DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 20.23 Making HTTP Requests with Server-Side ActionScript

20.23.1 Problem

You want to make HTTP requests using Server-Side ActionScript (SSAS).

20.23.2 Solution

Use the CF.http( ) method.

20.23.3 Discussion

The Server-Side ActionScript CF.http( ) method enables you to make HTTP requests to any resource available on the Web. This is a convenient way to run server-side scripts on remote domains and return those values to Flash.

The CF.http( ) method accepts the following parameters:

url

The URL to the requested resource (e.g., "http://www.remotedomain.com/logicalPath/page.cfm") or the protocol and server address/domain name (e.g., "http://www.remotedomain.com/"). You may also need to specify values for the path and file parameters when you use the second format.

method

Either "post" or "get", corresponding to the HTTP method (POST or GET) used to send any parameters.

path

If you do not include the logical path and filename in the url parameter, then you should indicate the logical path with the path parameter. For example, if the resource is "http://www.remotedomain.com/logicalPath/page.cfm", and you use the value "http://www.remotedomain.com" for the url parameter, then you should set path to "/logicalPath".

file

The filename of the requested resource when you do not include it in the url parameter. For example, if the resource is "http://www.remotedomain.com/logicalPath/page.cfm", and you use the value "http://www.remotedomain.com/" for the url parameter, then you should set file to "path.cfm".

username

The username, if required.

password

The password, if required.

resolveurl

A value of either "yes" or "no". If "yes", then relative links and references within the remote document are converted to absolute references. The values of the url and path parameters are used for this purpose. For example, if the url parameter is "http://www.remotedomain.com", and the path parameter is "/logicalPath", then an HTML tag in the remote document such as <a href="pages/myPage.cfm"> is converted to <a href="http://www.remotedomain.com/logicalPath/pages/myPage.cfm"> in the returned value.

params

The params parameter contains the values (if any) that are sent to the resource, and it should be in the form of an indexed array of objects with the following properties:

name

The variable name.

value

The variable value.

type

The scope for the variable. The possible values for type are "URL", "FormField", "Cookie", and "CGI".

You can pass the arguments to CF.http( ) as either positional or named parameters. If you use positional parameters, the following is a list of possible parameter orders:

CF.http(url);
CF.http(method, url);
CF.http(method, url, username, password);
CF.http(method, url, params, username, password);

Notice that path, file, and resolveurl are not available when using positional parameters. If you want to use these parameters, you must use named parameters.

The CF.http( ) method returns an associative array with the following properties:

Header

The raw response header.

Charset

The character the requested resource uses.

Mimetype

The mime type of the requested resource.

Statuscode

The status code, such as "200 OK" or "500 Internal Server Error".

Responseheader

The response header as an associative array. Each response header key (such as Date, Server, etc.) is an element of the associative array.

Text

A Boolean value. The value is true if the requested resource contains text, and false otherwise.

FileContent

The contents of the requested resource.

Most of the time, you should return the result of a CF.http( ) to the Flash movie, and you can process the result as a normal ActionScript associative array within the client movie. However, if you want to process the result using SSAS, be aware that on the server, the object is actually an ASObject. Therefore, you must use the ASObject methods (ASObject subclasses java.util.HashMap) to process the result of a CF.http( ) method invocation within SSAS:

// Get the contents of http://www.person13.com and assign the result to httpResult.
var httpResult = CF.http("http://www.person13.com");

// Extract the value of the FileContent element using the get(  ) method.
var fContent = httpResult.get("FileContent");

20.23.4 See Also

Recipe 20.21

    [ Team LiB ] Previous Section Next Section