HTTP Get, HTTP Post software component for ColdFusion for Windows

ActiveSocket is a Network Communication component for Windows Developers. It runs on any 32 bit and 64 bit Windows Platform, incl. Windows 7, Windows 2008, Windows 2003, Windows 2000, Windows Vista and Windows XP.
It features many IP protocols, incl.: SSH (Secure Shell), RSH (Remote Shell), HTTP(s), FTP, ICMP Ping, NTP, SNMP v1/v2c (Get,GetNext,Set), SNMP MIB translation, SNMP Trap Sender, SNMP Trap Receiver, Telnet, DNS, TCP, UDP, IP-to-Country, Wake-On-LAN and more. Samples are included many popular development platforms, incl. Visual C# .NET, Visual Basic .NET, ASP .NET, Visual Basic, Visual C/C++, ASP, Java, Javascript, PHP, Borland Delphi, Borland C++ Builder, ColdFusion.

HTTP Get and HTTP Post can be well integrated into ColdFusion environments. This document describes how the ActiveSocket Http object can be integrated into ColdFusion projects.

The most important functions of the Http object are:

  • Connect - connect to the (remote) HTTP web server on port 80 or any alternate port; optionally, use proxy credentials to use a proxy server; optionally, specify a web account and password for password protected web sites
  • Disconnect - to diconnect after a connect call;
  • ReadData - read all data from a web page;
  • WriteData - write data to a web page.

Step 1: Download and install ActiveSocket

Download ActiveSocket from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.

Step 2: Create a new ColdFusion document

Create a new blank webdocument with the ".cfm" extention. First of all we are going to build the form whitch commands and properties of the device can be filled in. Then we are going to make a source code that connects to the device.

Step 3: Implementation

This sample displays files in a folder. We're using the ActiveSocket FTP toolkit to do this. To establish a connection to an FTP server, we only need to know the ftp-address, the username and the password. The software picks a default port to establish the connection.

Establishing the connection and listing the files can be done in three steps:

  • Collect the ftp-server-address, the username and the password from the user
  • Establish the connection
  • List the files

Collecting the information can be done using a form. We've made our form look like this:

(Click on the picture to enlarge)

When we've got this information, we're ready to establish a connection to a server. First of all we need an object witch creates a connection between ColdFusion and the ActiveXperts software. To do that, use the following code:

 <cfobject class="ActiveXperts.FtpServer" type="com" name="objFtp" Action="Create"> 

Once the object is created, we're able to use the toolkit. We need to establish a connection, so we're going to use the ActiveSocket method "connect()". Use the following code:

  objFtp.Connect(strHost,strUsername,strPassword);

Now we need to list the files. The ActiveSocket toolkit is able to get the first file in the folder using the "FindFirstFile()" method and then the next file using the "FindNextFile()" method. So we're going to list the first file in the folder first, then the next file (the second file) and than the next file (the third file) and so on. Use the following code:

   objFiles = objFtp.FindFirstFile();
			
   do{
     writeoutput ("<tr>");
     writeoutput ("<td width=100>");				
     if(objFiles.IsDirectory()){
        writeoutput("Directory:");
     }
     else{
        writeoutput("File:");				
     }
     writeoutput ("</td>");
     writeoutput ("<td>");
     writeoutput(objFiles.Name & "<br>");				
     writeoutput ("</td>");
     writeoutput ("</tr>");																
     objFiles = objFtp.FindNextFile();				
   }
   while(objFtp.LastError eq 0);

Perhaps you would like to show the content of another folder. Paste the following code, before the listing code.

  objFtp.ChangeDir(strDirectory);

We're displaying the results of the process using the "LastError" method of the ActiveSocket toolkit. The method "LastError" displays a number. It's helpfull to know "0" means success. To get the error description use the "GetErrorDescription()" method. In our code we've used these methodes this way:

  strResult = objFtp.LastError & " : " & objFtp.GetErrorDescription(objFtp.LastError);

It can be very helpfull to check for errors before executing some code. Applying this can be done easily:

 if(objFtp.LastError eq "0"){
    Some code
 }

In this file only snippets of the code are showed. You can download the etire code on our ftp-server: ftp://ftp.activexperts-labs.com/samples/network-component/