Developing ColdFusion MX Applications with CFML
|
|
Interacting with Remote Servers
|
Using cfhttp to interact with the web
The cfhttp
tag, which lets you retrieve information from a remote server, is one of the more powerful tags in the CFML tag set. You can use one of two methods-Get or Post-to interact with a remote server using the cfhttp
tag:
- Using the Get method, you can only send information to the remote server in the URL. This method is often used for a one-way transaction in which
cfhttp
retrieves an object.
- Using the Post method, you can pass variables to a ColdFusion page or CGI program, which processes them and returns data to the calling page. The calling page then appears or further processes the data that was received. For example, when you use
cfhttp
to Post to another ColdFusion page, that page does not appear. It processes the request and returns the results to the original ColdFusion page, which then uses the information as appropriate.
Using the cfhttp Get method
You use Get to retrieve files, including text and binary files, from a specified server. The retrieved information is stored in a special variable, cfhttp.fileContent
. The following examples show several common Get operations.
To retrieve a file and store it in a variable:
- Create a ColdFusion page with the following content:
<html>
<head>
<title>Use Get Method</title>
</head>
<body>
<cfhttp
method="Get"
url="http://www.macromedia.com"
resolveurl="Yes">
<cfoutput>
#cfhttp.FileContent# <br>
</cfoutput>
</body>
</html>
- (Optional) Replace the value of the
url
attribute with another URL.
- Save the file as get_webpage.cfm in the myapps directory under your web_root and view it in the web browser.
The browser loads the web page specified in the url
attribute:
Reviewing the code
The following table describes the code and its function:
Code |
Description |
<cfhttp method="Get"
url="http://www.macromedia.com"
resolveurl="Yes">
|
Get the page specified in the URL and make the links absolute instead of relative so that they appear properly. |
<cfoutput>
#cfhttp.FileContent# <br>
</cfoutput>
|
Display the page, which is stored in the variable cfhttp.fileContent , in the browser. |
To get a web page and save it in a file:
- Create a ColdFusion page with the following content:
<html>
<head>
<title>Use Get Method</title>
</head>
<body>
<cfhttp
method = "Get"
url="http://www.macromedia.com/software"
path="c:\temp"
file="macr_software.htm">
</body>
</html>
- (Optional) Replace the value of the
url
attribute with another URL and change the filename.
- (Optional) Change the path from C:\temp to a path on your hard drive.
- Save the page as save_webpage.cfm in the myapps directory under your web_root directory.
- Go to the specified path and view the file that you specified in a text editor (using the values specified in step 1, this is C:\temp\macr_software.htm):
The saved file does not appear properly in your browser because the Get operation saves only the specified web page HTML. It does not save the frame, image, or other files that the page might include.
Reviewing the code
The following table describes the code and its function:
Code |
Description |
<cfhttp
method = "Get"
url="http://www.macromedia.com/software"
path="c:\temp"
file="macr_software.htm">
|
Get the page specified in the URL and save it in the file specified by the path and file attributes. When you use the path and file attributes, ColdFusion ignores any resolveurl attribute. As a result, frames and other included files cannot appear when you view the saved page. |
To get a binary file and save it:
- Create a ColdFusion page with the following content:
<cfhttp
method="Get"
url="http://www.macromedia.com/macromedia/accessibility/images/spotlight.jpg"
path="c:\temp"
file="My_SavedBinary.jpg">
<cfoutput>
#cfhttp.MimeType#
</cfoutput>
- (Optional) Replace the value of the
url
attribute with the URL of a binary file that you want to download.
- (Optional) Change the path from C:\temp to a path on your hard drive.
- Save the file as save_binary.cfm in the myapps directory under your web_root and view it in the web browser.
The MIME content type appears in your browser:
- (Optional) Verify that the binary file now exists at the location you specified in the
path
attribute.
Reviewing the code
The following table describes the code and its function:
Code |
Description |
<cfhttp
method="Get"
url="http://www.macromedia.com/macromedia/accessibility/images/spotlight.jpg"
path="c:\temp"
file="My_SavedBinary.jpg">
|
Get a binary file and save it in the path and file specified. |
<cfoutput>
#cfhttp.MimeType#
</cfoutput>
|
Display the MIME type of the file. |
Comments