Contact Info

Crumbtrail

ActiveXperts.com » Network Component » How to Use Network Component » HTTP Client » ColdFusion

ColdFusion HTTP Get and HTTP Post Sample Source Code

Network Component provides an easy-to-use development interface to a variety of IP protocols. By using Network Component, you can very easily create or enhance applications with network features.

Network Component features the following: DNS, FTP, HTTP, HTTPs, ICMP Ping, IP-to-Country, MSN, NTP, RSH, SCP, SFTP, SNMP v1/v2c (Get, GetNext, Set), SNMP Traps, SNMP MIB, SSH, TCP, Telnet, TFTP, UDP, Telnet, Wake-On-LAN and more.

Network Component can be well integrated into any development platform that supports ActiveX objects.


The most important functions of the Http object are:


Step 1: Download and install the Network Component

Download Network Component 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 Network Component FTP 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:

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="AxNetwork.Http" type="com" name="objHttp" Action="Create"> 

Appendix: Full source code

<!--- creating the object ---> 
<cfobject class="AxNetwork.HttpEx" type="com" name="objHttpEx" Action="Create">

<cfoutput>

<html>

<head>
  <title>ActiveXperts Network Component HTTP Sample Using ColdFusion</title>
  <style>
  <!--
  body{
    text-align: center;
  }
  h2{
   font-family: verdana;
   color: navy;
   font-weight: bold;
  }
  form{
   margin: 0px;
  }
  .input{
   font-family: verdana;
   font-size: 9pt;
   color: navy;
   border: 1px solid navy;
   background-color: white;
   width: 325px;
  }
  .button{
   font-family: verdana;
   font-size: 9pt;   
   width: 325px;
  }
  .table{
   font-family: verdana;
   font-size: 9pt;
   color: navy;
   border: 1px solid navy;
   width: 500px;
  }
  div{
    width: 500; 
    height: 400px; 
    overflow: auto; 
    text-align: left; 
    font-size: xx-small; 
    font-family: verdana;
    border: 1px solid navy;
  }
  a{
    font-family: verdana;
    font-size: 8pt;
    color: red;
  }
  -->
 </style>

</cfoutput> 

<cfscript>
  strUsername = "";
  strWebsite = "www.activexperts.com";
  strCode = "";
  strResult = "";
  strLogfile = GetTempDirectory() & "http.log";
  strComponent = "Module [" & objHttpEx.Module & "]; Build [" & objHttpEx.Build & "]";
</cfscript>

<cfif IsDefined("submitbutton")>

	
  <!--- Filling in the username and the password to get the source code for a secured site --->
  <cfscript>
	 strUsername = "";
   if(IsDefined("URL.username")){
     objHttpEx.WebAccount = URL.username;
     strUsername = URL.username; 
   }

   if(IsDefined("URL.password")){
     objHttpEx.WebPassword = URL.password;
   }
   
   if(isDefined("URL.logfile")){
     strLogfile = URL.logfile;
   }
  </cfscript>
	 
  <!--- Connect the website --->
  <cfif objHttpEx.LastError eq 0 >
  <cfscript>
   strWebsite = "";
   if(IsDefined("URL.website")){	 
     objHttpEx.LogFile = strLogfile;
     strCode = objHttpEx.Get(URL.website);
     strConnect = "Connected";
     strWebsite = URL.website;
   }
  </cfscript>
  </cfif>
	
  <!--- get the sourcecode --->
  <cfif objHttpEx.LastError eq 0 >
   <cfscript>
     strCode = ReplaceEx(strCode, "<", "<", "all");
     strCode = ReplaceEx(strCode, ">", ">", "all");
     strCode = ReplaceEx(strCode, "#chr(10)#", "<br>", "all");		 		 
   </cfscript>
  </cfif>
	
  <!--- get the results --->
  <cfscript>
     strResult = objHttpEx.LastError & ": " & objHttpEx.GetErrorDescription(objHttpEx.LastError);
  </cfscript>
	
</cfif>

<cfoutput>

</head>

<body>

<form method=get>

<h2>ActiveXperts Network Component CF Sample - HTTP</h2>

<table class=table>
 <tr>
  <td>Component:</td>
  <td>#strComponent#</td>
 </tr>
 <tr>
  <td>URL:</td>
  <td><input class=input type=text name=website value="#strWebsite#"></td>
 </tr>
 <tr>
  <td>Web Account(optional):</td>
  <td><input class=input type=text name=username value="#strUsername#"></td>
 </tr>
 <tr>
  <td>Web Password(optional):</td>
  <td><input class=input type=password name=password value=></td>
 </tr>
 <tr>
  <td> </td>
  <td><input class=button type=submit name=submitbutton value="Visit web site now"></td>
 </tr> 
 <tr>
  <td>Logfile:</td>
  <td><input class=input type=text name="logfile" value="#strLogfile#"></td>
 </tr>
</table>

</form>

<br>

<div>
#strCode#
</div>

<br>

<table class=table>
 <tr>
  <td><b>Result: </b></td>
  <td>#strResult#</td>
 </tr>
</table>
<br>

<p>This sample is based on ActiveXperts Network Component, an <a target="blank" href="https://www.activexperts.com">ActiveXperts Software</a> product.</p>
<a href="index.cfm">Back to main page</a>

</body>
</html>

</cfoutput>

You can download the complete samples here. There are many other working Network Component scripts on our site and shipped with the product.