Click here to Skip to main content
Click here to Skip to main content

Calling scripts within scripts

By , 29 Jan 2000
 
  • Download source files - 27.5 Kb
  • Overview

    When writing an ASP script, it is sometimes useful to temporarily transfer execution to another script, and then continue the original script with the result of the called script.

    This is what the AspRetrieve component does. You can think of it like calling a procedure.

    The AspRetrieve component acts similar to the new function Server.Execute in IIS 5.0. In addition, you can pass parameters, either by GET or by POST.

    The functionality of the AspRetrieve component in brief:

    • call any URL from within an ASP script
    • pass parameters to the URL either in the URL itself (i.e. by GET) or by POST
    • retrieve the results form the called URL as a string

    Because I only needed limited functionality, only textual results are correctly returned from the called URL. i.e. you can call a HTML page, or an ASP script, but you cannot call a GIF or JPEG image, since the latter two return binary data.

    If you would like to extend the functionality, I would love to have this new version.

    Details

    The AspRetrieve component consists of two parts:

    • A MFC/ATL component that uses the Wininet functions to call an URL
    • Some ASP functions that aid in accessing the MFC component

    In addition I included some examples

    Installation

    • Compile the MFC/ATL part of the component, using Visual C++ (I used the german version of Visual C++ 6, SP3)
    • Copy the component to whatever directory you want it, ensuring the account under which IIS runs (IUSR_XXX) has the correct permissions to access the component
    • Register the compiled component using regsvr32 (which is in your windows' system directory). Just type regsvr32 aspretrieve.dll at the command prompt
    • Copy the ASP function file retrieve.inc to whatever directory you want
    • Include retrieve.inc in your projects and begin using it

    Usage

    When everything is installed correctly, just include the retrieve.inc file in your ASP scripts and call the two functions Retrieve(url) and RetrievePost(url,params). You can either use relative URLs or absolute URLs for calling.

    A practical use of the component is at those places, where you would need a lot of Response.Write statements to produce HTML output. Those functions can now be separated into extra ASP files, feeded with parameters and called via the AspRetrieve component.

    Example

    Imagine you have a function that creates a table by code. 

    Without the AspRetrieve component, you would write something like this to implement the function:

    sub makeTable( byval cols, byval rows )
    	dim c,r
    
    	Response.Write "<table border=""0"" width=""100%"">"
    
    	' create rows.
    	for r=1 to rows
    		Response.Write "<tr>"
    
    		' create cols.
    		for c=1 to cols
    			Response.Write "<td width=""100%""></td>"
    		next
    
    		Response.Write "</tr>"
    	next
    
    	Response.Write "</table>"
    end sub

    You call the function like this:

    <% makeTable 10, 20 %>

    When using the AspRetrieve, you implement the function in a separate ASP script like this:

    <table border="1" width="100%">
    	<%
    	' create rows.
    	dim c,r
    	for r=1 to Request("rows")
    	%>
    
    		<tr>
    
    			<%
    			' create cols.
    			for c=1 to Request("cols")
    			%>
    				<td width="100%"></td>
    			<%
    			next
    			%>
    
    		</tr>
    	<%
    	next
    	%>
    </table>

    You could then write a wrapper to simplify calling:

    sub makeTable( byval cols, byval rows )
    	Response.Write Retrieve( "maketable.asp?cols="&cols&"&rows="&rows )
    end sub

    And finally you would call the wrapper in your code

    <% makeTable 10, 20 %>

    Conclusion

    The advantages of using AspRetrieve are that you can divide a complex application into individual modules. You can create a library of ASP scripts that you can call as needed. 

    In addition by cutting down the number of times you need to use Response.Write, you simplify your code. Having code in separate files, you can even use a graphical layout tool to modify your files, which is impossible if you construct your page with Response.Write.

    Another benefit is that you no longer need to include all files "at compile-time" using the <!--#include--> directive, but can "include" these files dynamically at runtime using the Retrieve function! (a feature that e.g. JSP has by default).

    Please feel free to ask any questions you have by e-mail: keim@zeta-software.de.

    License

    This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    About the Author

    Uwe Keim
    Chief Technology Officer Zeta Producer Desktop CMS
    Germany Germany
    Member
    Uwe does programming since 1989 with experiences in Assembler, C++, MFC and lots of web- and database stuff and now uses ASP.NET and C# extensively, too. He has also teached programming to students at the local university.
     
    In his free time, he does climbing, running and mountain biking. Recently he became a father of a cute boy.
     
    Some cool, free software from us:
     
    Free Test Management Software - Intuitive, competitive, Test Plans. Download now!  
    Homepage erstellen - Intuitive, very easy to use. Download now!  
    Send large Files online for free by Email
    Some random fun stuff in German

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board.
    Search this forum  
        Spacing  Noise  Layout  Per page   
    General"Print & Go" script with Uwe's helpmemberJuliansongs6 May '04 - 23:34 
    Hi friends!
     
    Thanx to Uwe Keim I created a nice little ASP 3.0 script that loops thru a certain web directory, finds all [.htm*] files, reads them out, puts them in a new HTML file within the same directory and opens the browser's print dialog box. After printing it automatically closes the window.
     
    It benefits Online Magazine articles etc. that reside in several DIFFERENT html files. With this script you can print them all out with one click. I call the function "Print & Go"
     
    <html>
       <head>
          <title>Online Magazine "Print & Go" Edition</title>
     
             <!-- PRINT LAYOUT FORMATTING FOR IE4+ -->
             <style language="StyleSheet" type="text/css">
             <!--
             //forces a page break after every article using <h3></h3> as the trigger
             h3 { page-break-before:always }
             //forces at least 3 textlines to remain attached to an orphaned line after a page break
             p   { widows:3 }
             //-->
             </style>
     
       </head>
     
       <!-- Opens the print dialog box and closes this window (IE4+) after print or cancel -->
       <!-- In Netscape window.close() does not work if called right after window.print() -->
       <body onLoad="window.print(); window.close()">
     
    <% 'locates this file and puts the path into the pathInfo variable
       pathInfo   = Server.MapPath(Request.ServerVariables("SCRIPT_NAME"))
     
       'locates the last backslash in the pathInfo variable
       slashPos   = InStrRev(pathInfo, "\")
     
       'truncates the variable pathInfo and puts the remaining directory path into the variable folderStr
       folderStr = Left(pathInfo, slashPos - 1)
     
       'creates 3 of the four object variables
       Set FSO = CreateObject("Scripting.FileSystemObject")
       Set FOL = FSO.GetFolder(folderStr)
       Set FIL = FOL.Files
     
       'iterates thru the FIL Object to find all files within the variable folderStr
       For Each file In FIL
     
             'creates the 4th object variable to open all found files
             Set TXT = FSO.OpenTextFile(folderStr & "\" & file.Name, 1)
     
             'excludes all non htm(l) files
             If Right(file.Name, 4) = ".htm" _
             Or Right(file.Name, 5) = ".html" Then
     
                'reads all found htm(l) files line by line
                Do Until TXT.AtEndOfStream
     
                      articles = TXT.ReadLine
                      Response.Write(articles)
     
                Loop
     
                'trigger for the above StyleSheet item [h3] to force a page break in print
                Response.Write("<h3></h3>")
     
             End If
     
       Next
     
       'optional disclaimer in a directory 1 level down
       Set TXT = FSO.OpenTextFile(Server.MapPath("\Dir1Down\Disclaimer.htm"), 1)
     
       'reads the optional disclaimer
       Do Until TXT.AtEndOfStream
     
             disclaimer = TXT.ReadLine
             Response.Write(disclaimer)
     
       Loop
     
       'destroys all 4 object variables
       Set TXT = Nothing
       Set FIL = Nothing
       Set FOL = Nothing
       Set FSO = Nothing %>
     
       </body>
    </html>
     
    You will have to adapt only a few settings to make this code work for you. If you have any questions please write me. I'll be happy to help you!
    QuestionHave you guys tried this?memberChristianCalderon15 Feb '04 - 16:32 
    Using the MSXML2.ServerXMLHTTP object you can do something like this (an extract from a code I sometime use)
     
    Set objHttpRequest = CreateObject("MSXML2.ServerXMLHTTP")

    With objHttpRequest
    .setTimeouts lngResolveTimeout, lngConnectTimeout, lngSendTimeout, lngReceiveTimeout
    .Open "GET", url
    .send
    Select Case .Status
    Case 200
    ' handle this
    Case 404 'Not found
    ' and this
    Case Else
    ' AND THIS!!
    End Select
    strMyResponse = .responseText
    End With

    Set objHttpRequest = Nothing
     

     
    Christian Calderon

    AnswerRe: Have you guys tried this?sitebuilderUwe Keim15 Feb '04 - 19:49 
    Cool idea! Thanks for sharing!
     
    --
    - Free Windows-based CMS: www.zeta-software.de/enu/producer/freeware/download.html
    - See me: www.magerquark.de
    - MSN Messenger: uwe_keim@hotmail.com
     

    Generalaspretrieve.dll in asp .netmemberhaninaya2 Dec '03 - 3:38 
    Confused | :confused: Hi
     
    Pls, does anyone know if the aspretrieve.dll can be used in asp .net too?
     
    Thx
     
    Hanane
    QuestionHow to through proxy?susstonyjun15 Oct '02 - 22:30 
    Confused | :confused:
    I get message:
    "You are not permitted to access the remote system.
     
    If this is an error, then you should contact your local firewall administrator. (The operation completed successfully.)"

    QuestionHow to get AspRetrieve?sussAnonymous19 Sep '02 - 23:48 
    How to get AspRetrieve?
    AnswerRe: How to get AspRetrieve?sitebuilderUwe Keim19 Sep '02 - 23:58 
    Download and compile WTF | :WTF:
     
    --
    See me: www.magerquark.de
    GeneralRe: How to get AspRetrieve?sussAnonymous20 Sep '02 - 0:06 
    thanx,i get it ,let me try it now.
    GeneralHey ,Uwe.I use your component,some error occured.sussAnonymous20 Sep '02 - 0:29 
    'example.asp
    <!--#include file="retrieve.inc"-->
     
    <html>
    <head>
    <title>AspRetrieve example</title>
    </head>
    <body>
    <%
    c = Retrieve("http://www.target.net/cgi-bin/check?num=" & 13907540094)
    %>

    <%=c%>

    </body>
    </html>
    --------------------------
    error is : Component-error: operation timeout
     
    ----------------------
     
    and url
    http://www.target.net/cgi-bin/check?num=" & 13907540094
     
    should return: 0 0
     
    why?

    GeneralConfirmation of Previous MessagesussGravity6 Aug '02 - 19:42 
    I have confirmed that this application crashes when placed under heavy traffic. Great concept, but in the end doesn't go all the way... Does anybody know a solution that does similiar (like Server.Execute but accepts GET and/or POST) and is a little bit more robust?
    GeneralRe: Confirmation of Previous MessagesitebuilderUwe Keim19 Sep '02 - 23:59 
    Try to use Perl-functions of the Perl www-library (e.g. on www.cpan.org).
     
    --
    See me: www.magerquark.de
    QuestionIs WinInet really suitable for this ??memberChristian Tratz20 Jun '01 - 4:10 
    Hi,
    you mentioned that you are using WinInet based classes/functions to retrieve the URLs.
    I doubt that this is a good approach, as it basically crashes a your web server under heavy load.
     
    See also the MS knowledge base:
     
    Q238425
    INFO: WinInet Not Supported for Use in Services
     
    Regards
    Chris
     

    QuestionCan someone send me a compiled dllmemberAlan Warchuck8 Mar '01 - 10:21 
    Getting an error message I can't read. Not in english
     
    can someone send me a compiled dllEek! | :eek:
     
    Regards,
     
    Alan
    Warchuck@thelastdomain.com
    QuestionNew session?memberGuy25 Jan '01 - 0:01 
    Confused | :confused:
    Does using this component start a new session between the
    client and IIS? If so, it precludes accurate session counting.
    It also explains why the former reader's OnPageStart was called twice.
    Questionbug?memberAndre Milton18 Jan '01 - 20:52 
    I sometimes get the following error when calling Retrieve on multiple files at once:
     
    Server object error 'ASP 0193 : 80020009'
     
    OnStartPage Failed
     
    /admin/retrieve.inc, line 29
     
    An error occurred in the OnStartPage method of an external object.
     

    My global.asa file is empty but with an empty OnStartPage, I still get the same problem. Any thoughts?
     
    Confused | :confused:
     
    Freakboy
    AnswerRe: bug?memberUwe Keim18 Jan '01 - 20:54 
    Hmmmm. I use the component very seldom for myself, so I never got a similar message.
     
    Uwe Keim
    GeneralRe: bug?memberAndre Milton18 Jan '01 - 21:02 
    Wow.. thanks for the speedy reply. Its strange because if I call a response.end before the end of the function, it works fine... Anyways. Thanks for the help and its a really wicked but simple piece of code you've written. I've saved alot of time by using it. Smile | :)

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Permalink | Advertise | Privacy | Mobile
    Web02 | 2.6.130523.1 | Last Updated 30 Jan 2000
    Article Copyright 2000 by Uwe Keim
    Everything else Copyright © CodeProject, 1999-2013
    Terms of Use
    Layout: fixed | fluid