To demonstrate the ability to use the AspTear component in any Automation enviroment, the HTTP headers example is mainly implemented in an Excel worksheet (SessionCookieSample.xls). It is supported by a small ASP file that is teared (session-cookie.asp).
The example shows how to parse headers you get after retrieval, as well as how to set headers before retrieving another page.
' Written by: Christoph Wille
' Last updated: 5/22/2001
Option Explicit
Const Request_POST = 1
Const Request_GET = 2
Const Request_HEAD = 3
' change this URL to reflect your test environment
Const cstrServerURL = "http://localhost/AspTear/Session-Cookie.asp"
' This sub test the .AddCookie functionality
' First request sets the cookie, second request sends it. To prove it's working, a Session
' variable is used and printed
Sub SendHeaders()
On Error GoTo Err_SendHeaders
Dim xobj As Object, strRetval As String, strRetVal2 As String
Dim strHeaders As String, nFoundAt As Integer, nEndOfCookie As Integer, strCookie As String
Dim arrCookiePair As Variant
Set xobj = CreateObject("SOFTWING.ASPtear")
' disable caching, force a page reload every time
xobj.ForceReload = True
' URL, action, payload, username, password
strRetval = xobj.Retrieve(cstrServerURL, Request_GET, "", "", "")
' now get the headers we found and search for the Set-Cookie header with the ASPSESSIONID cookie
strHeaders = xobj.Headers
nFoundAt = InStr(strHeaders, "Set-Cookie: ASP")
If (nFoundAt > 0) Then
nEndOfCookie = InStr(nFoundAt, strHeaders, ";")
If (nEndOfCookie > 0) Then
' get only the cookie data; forget about path, etc. you shouldn't be that careless
strCookie = Mid(strHeaders, nFoundAt + 12, nEndOfCookie - nFoundAt - 12)
End If
End If
' now start our second request; the Session variable is now set and we can retrieve it.
' first thing to do is to set the Session cookie so we get what we want.
If (nFoundAt > 0 And nEndOfCookie > 0) Then
arrCookiePair = Split(strCookie, "=")
xobj.AddCookie arrCookiePair(0), arrCookiePair(1)
End If
' retrieve the page now
strRetVal2 = xobj.Retrieve(cstrServerURL, Request_GET, "", "", "")
' display what we got for the first call and now for the second one
MsgBox "1st Call: """ & Left(strRetval, Len(strRetval) - 1) & """" & _
vbCrLf & "2nd Call: """ & Left(strRetVal2, Len(strRetVal2) - 1) & """"
Exit Sub
Err_SendHeaders:
If Err.Number >= 400 Then
MsgBox "Server returned error: " & Err.Number
Else
MsgBox "Component/WinInet error: " & Err.Description
End If
End Sub
<% @LANGUAGE=VBSCRIPT %>
<%
' Written by: Christoph Wille
' Last updated: 5/22/2001
Option Explicit
Response.Expires = 0
' Shown on second call only
Response.Write "--- " & Session("TestVarValue") & " ---" & vbCRLF
' Set the session variable. Only a user returning with the same ASPSESSIONID cookie
' will be able to see this value
Session("TestVarValue") = "my value is stored here"
%>