|Published (Last):||1 September 2011|
|PDF File Size:||1.21 Mb|
|ePub File Size:||19.80 Mb|
|Price:||Free* [*Free Regsitration Required]|
Since then, it has become a part of our everyday lives, whether we think about it or not. Usually, we make use of this protocol without giving it a second thought, like when we surf the Web.
Sometimes, though, we have to actively use HTTP to solve problems. ColdFusion has built-in tags for HTTP support and, as Web developers, it is to our advantage to know how to use these tags efficiently.
If we dumb it down to the lowest level, HTTP is a way for clients to request information from a Web server, and for Web servers to provide responses to those requests.
The method used specifies how the data is sent. If you use the "POST" method, they are sent behind the scenes. We should also touch briefly on MIME types. This standard was originally intended to define the types of files that are exchanged via email, but has since been extended to the World Wide Web. Knowing about MIME types will allow us to return information in various formats and it will be a key part of our demonstration code below.
In fact, entire books have been written on the topic. First, you want to specify the URL of the blog with the url attribute, of course. This way, if the server is slow, it will timeout, and not hold up our application unnecessarily. The cfhttp tag returns a structure to us called cfhttp and it contains several variables that may be of use to us.
First, we have the FileContent variable. This holds all the data returned to us by the URL we called. ResponseHeader is a struct that contains several variables dealing with the response received from the URL. If we had attempted to access a binary file, such as a. You can perform a simple text dump of the cfhttp struct like this:. This works on ColdFusion 5 and up, and it will show you exactly what comes back when you use this particular tag. You can also have ColdFusion automatically save the file to the server when it gets the information back from the URL, by passing it both the path and the file attributes like this:.
This will save the file directly to the server so that you can use it later. It might look something like this:. Everyone wins! Another attribute worth mentioning with regards to cfhttp is the ResolveUrl attribute.
If you used cfhttp to call a site that had relative paths in its image sources, links or style sheets, you might want to set the ResolveUrl attribute to "yes". This makes sure that all the links still point to the correct path or URL when you get the file content back.
This allows you to specify a parameter to send to the URL in the url attribute of the cfhttp tag. What output is generated when you run this? Well, first the script requests itself using cfhttpparam to send some request parameters.
When it is requested with those parameters, the script lists the parameters that it was sent. This output gets put into the cfhttp. FileContent variable back in the script that performed the request.
It is then displayed along with the cfhttp. As you can see, cfhttp and cfhttpparam provide you a great deal of flexibility over your applications, and they can help you manipulate your ColdFusion scripts to behave like real people surfing a Website. Any information for which you rely on that server will not be available. Also, use good judgment when using these tags. Be aware that you must specify the absolute URL. The next thing we should discuss is the use of the cfheader tag.
This can be used to manually set HTTP response headers along with the contents of a page. HTTP headers are sent with every request. You could choose to set the status code to page not found if you wanted, or even internal server error. If there was, you would set the status code to successful and display the content. Note that you must set up the handler as a URL handler, not a File handler. The File handler will dump out your ColdFusion code in the browser as if it were a text file.
To illustrate this tag a little better, I put together this little bit of code. In the receive. Now, run the call. So, it worked! This tag allows you to specify the MIME type that is returned by the file.
If you output anything before the cfcontent tag, it will be ignored, just as with cfheader. Now create a new HTML document and insert this image tag in it:. This assumes you have named your file with the cfcontent tag in it returnimage. You can return practically any file type you want.
First, we need a database table to store each image. This will be a very simple table that will hold an image identifier, the absolute path to the image, and a descriptive image name.
When building a real image server you may want to include the ID of the user who uploaded it, the date and time it was uploaded, the page the image was displayed on, and any other information you may want regarding the image. You might also track the image size so that you can measure how much disk space and bandwidth each user is utilizing. This table will store the full path to the image, a brief description of the image so we can know what it is when looking at it through an administrative interface or in the database table itself , and a unique ID.
I also want to count every time that an image is accessed. This is essential for uploading a file. In the upload. That code will look like this:. The file name will be the date and time in a single, long string. The destination will be the images directory underneath the directory that the current file is in the CGI.
Now we can upload the file to this location:. Note that ColdFusion generates its own temporary filename under which to store the file until you decide what to do with it.
As a result, if you output the value of Form. But, in the cffile struct, we get the actual file name and the file extension. Make sure that you limit the types of files that can be uploaded, using the "accept" attribute as above, and specifying GIF and JPEG images. You could also specify bitmaps and PNGs as well. Now, we'll put this in the database. Here, we simply insert the full path to the image, along with a note that this was uploaded by an anonymous user.
When we uploaded the file above, it didn't have an extension, so now we need to rename it to match what was in the database. Here's how we'll do that:. You'll need to change the URL based on the way you've set up your directory structure.
The next step is to create the returnimage. This is a pretty big chunk of code, so take a look first, then we'll talk about it:. The first thing we do is check to make sure there's an image ID available.
If not, we could return a default "Graphic Not Available" type of graphic. If we don't, an infamous red X will appear there. We check to make sure the query returned a result, and we put a row in the ImageTracking table to indicate that the image was accessed.
You could create a separate "FileExtension" field in the Images table and eliminate this bit of work by storing the file extension or even the content type in there. Once we have the content type figured out, we send our cfheader , then push our image along using cfcontent.
Make sure that the deletefile attribute is set to "no", so that the file isn't removed after it's used. If there are no images matching that ID in the database, we could return our "Image Not Available" graphic, or just let it display a red X.
Be sure to specify in the cfheader value attribute that this is inline data. This means that it will be displayed in the Web page, not downloaded as an attachment. Hint: If you want to track downloads of a file, you could use this method and set the value attribute to "attachment".
Also, note that the value should be the file name only, not the full path. There you have it: a very basic start on an image server. There are many, many things that you can do with the tags and techniques we've discussed in this article. Your assignment now is to take the above application and build it into something more comprehensive.
Add a way for users to name images, a report to show how often the images are accessed, and the ability to store more data about each image upload and each time the file is accessed. There are plenty of things you can do! In this article, we've covered a lot of ground and I'm pretty much exhausted, believe me! We've discussed using the cfhttp tag to retrieve an RSS feed, using the cfhttpparam tag to send form, URL, and cookie variables, as well as files, to a remote Website, and how to return file types other than text and HTML, using ColdFusion.
The more applications you develop, the more ways you will find to apply these tags in your code. Until next time, have fun in the wide world of ColdFusion.
Putting cfhttp to Use
You use Get to retrieve files, including text and binary files, from a specified server. The following examples show several common Get operations. Get the page specified in the URL and make the links absolute instead of relative so that they appear properly. 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. Using cfhttp to interact with the web Search.
Practical Uses of HTTP in ColdFusion
The first example demonstrates a simple GET operation. Listing This example fetches the index page from www. Figure
Subscribe to RSS
Generates an HTTP request and handles the response from the server. Internet protocol tags. Note: You can specify this tag's attributes in an attributeCollection attribute whose value is a structure. Specify the structure name in the attributeCollection attribute and use the tag's attribute names as structure keys. Three attributes added - authType, domain, and workstation. ColdFusion MX 7.