Published: Tuesday, July 18, 2000
Speed up your ASP pages: Turn Them into HTML with ASPTear!
By Evagoras Charalambous
Let's face it: HTML is faster than ASP. Yes, we are hearing promises of compiled ASP+ code that will run a lot
faster than today's ASP, but will it be faster than good old HTML? (To learn more about ASP+ be sure to check
out our ASP+ Article Index.) As developers we often use ASP to connect
to a database and display certain results. Whether we do it all with ASP, or with components it doesn't matter.
The question on our minds is "how long does it take to process?". A good example of this is a messageboard
where for every 10 visits you get only one post. It makes more sense to change it from an ASP page which
requests a visit to the database every time to a plain HTML page and update the HTML page only when there is
a new post. In this article I will show you a way to improve some of your ASP pages by transforming them into
pure HTML using the free component ASPTear. (To learn more about ASPTear be sure to read the previous 4Guys article:
Grabbing Information from Other Servers.)
I will break down this example into 3 parts. In the first one I will show you how to use ASPTear to get the
contents of another page, in the second part how to write the contents to an HTML file using the
FileSystemObject, and in the third part I will put everything together using transactions to make
sure they all get executed. (To learn more about the FileSystemObject be sure to take a moment and
read the FileSystemObject F.A.Q..)
Part 1: Using ASPTear
The free component can be downloaded from http://www.alphasierrapapa.com/iisdev/components/asptear/
where you can also find instructions on how to register it on your server and use it. I will summarize the use
of it here though. To instantiate the component you write:
Set xObj = Server.CreateObject("SOFTWING.ASPTear")
strRetVal = xObj.Retrieve(StrUrl, nRequestType, _
strQueryString|strPostData, strUsername, strPassword)
|
This is a very simple component. It supports only one method: Retrieve. The rest here means:
| Parameter | Meaning |
StrRetVal | The HTML code returned back to us in the form of a string |
StrUrl | The complete URL of the page you want to get |
NRequestType | 2 for GET, 1 for POST |
strQueryString|strPostData | any information you need to post to a page to aid it in the processing, just like posting data from a form or using the URL to add a querystring. |
strUsername/strPassword | Log in to secured sites with username/password |
The simplest form of this component could be something like this:
strRetVal = xObj.Retrieve("http://www.fakeaddress.com/default.asp",2,"","","")
|
or a more complicated one could be something like:
strPostData = "Name=" & Server.URLEncode("Christoph Wille") & _
"&goto=" & Server.URLEncode("http://www.alphasierrapapa.com/")
strRetVal = xObj.Retrieve("http://www.alphasierrapapa.com/",1, _
strPostData,"Evagoras","Charalambous")
|
Ok let's get to work! We'll be creating a procedure that will run on the server and will do all this for us.
I call this procedure CreateHTMLPage for obvious reasons and it takes 2 parameters:
getURL, the page to get the contents of, and
postFile, the HTML file that it will output the results to.
In Part 2 we will look at writing the output of the ASP page to an
HTML file. This section concludes with the part of the CreateHTMLPage subroutine that
uses ASPTear to obtain the output of the ASP page...
Read Part 2
Sub CreateHTMLPage(getURL,postFile)
'###################################################
'# change the variables below to match your site #
'###################################################
'replace "getSite" variable to the address of your site
'to get the complete URL of the active file you want to run
Dim getPage
getPage = "http://www.yoursite.com" & getURL
'replace "postSite" to the physical address of your site
'to make a composite physical address of the "HTML" file
'you want to write to
Dim postPage
postPage = "C:\Inetpub\wwwroot\mysite" & postFile
'##################################################
'# end changes #
'##################################################
'variables for "ASPTear" component
Const Request_POST = 1
Const Request_GET = 2
'initiate component "ASPTear.dll"
Dim TearObj
Set TearObj = CreateObject("SOFTWING.ASPTear")
Response.ContentType = "text/html"
'Note on above line: you can actually skip it if your asp file
'that you are calling has a similar declaration in its HEAD part,
'like:
'<meta http-equiv="Content-Type"
content="text/html;
charset=iso-8859-1">
' to collect the output of the ASP page as a string
Dim strRetrieval
strRetrieval = TearObj.Retrieve(getPage, Request_GET, _
"", "", "")
'in case of error getting the page, output error
'this is a direct copy from the documentation of ASPTear
If Err.Number <> 0 Then
Response.Write "<b>
If Err.Number >= 400 Then
Response.Write "Server returned error: " & Err.Number
Else
Response.Write "Component/WinInet error: " & Err.Description
End If
Response.Write "<b>
Response.End
End If
Set TearObj = Nothing
|