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 illustrate a few common Get operations.

To retrieve a file and store it in a variable:

  1. Open a new file in ColdFusion Studio.
  2. Modify the file so that it appears as follows:
    <cfhttp method="Get"
    
      url="http://www.ci.newton.ma.us/main.htm"
    
      resolveurl="Yes">
    
    <cfoutput>
    
      #cfhttp.FileContent# <br>
    
    </cfoutput>
    
    
  3. (Optional) Replace the url attribute value with the URL of a file you want to get.
  4. Save the file as getwebpage.cfm in myapps under your Web root directory and view it in your browser.

Reviewing the code

The following table describes the code and its function:
Code
Description

<cfhttp method="Get" 

   url="http://www.ci.newton.ma.us/

  main.htm/ 

   resolveurl="Yes"> 

Get the page specified in the URL and make the links absolute instead of relative so that they display 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:

  1. Open a new file in ColdFusion Studio.
  2. Modify the file so that it appears as follows:
    <cfhttp
    
      method = "Get"
    
      url="http://www.ci.newton.ma.us/main.htm"
    
      path="c:\temp"
    
      file="newtonmain.htm">
    
    
  3. (Optional) Replace the url attribute value with the URL of a file you want to save and change the filename.
  4. (Optional) Change the path from C:\temp to a path on your hard drive.
  5. Save the file as savewebpage.cfm and view it in a text editor. The file does not display properly in your browser because the Get operation saves only the specified Web page. 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.ci.newton.ma.us

    /main.htm" 

  path="c:\temp" 

  file="newtonmain.htm"> 

Get the page specified in the URL and save it in the file specified in path and file.
When you use the path and file attributes, ColdFusion ignores any resolveurl attribute. As a result, frames and other included files cannot display when you view the saved page.

To get a binary file and save it:

  1. Open a new file in ColdFusion Studio.
  2. Modify the file so that it appears as follows:
    <cfhttp
    
        method="Get"
    
        url="http://localhost/myapps/testfile.zip"
    
        path="c:\temp"
    
        file="MyTestFile.zip">
    
    <cfoutput>
    
      #cfhttp.MimeType#
    
    </cfoutput>
    
    
  3. Change the URL to point to a binary file you want to download.
  4. Change the path to point to a path on your hard drive.
  5. Save the file as savebinary.cfm in myapps under your Web root directory and view it in your browser.

Reviewing the code

The following table describes the code and its function:
Code
Description

<cfhttp 

  method="Get" 

  url="http://localhost/myapps/testfile.zip" 

  path="c:\temp" 

  file="MyTestFile.zip"> 

Get a binary file and save it in the path and file specified.

<cfoutput>

  #cfhttp.MimeType#

</cfoutput> 

Display the MIME type of the file.



Banner.Novgorod.Ru