logo.gif (7729 bytes)
metal.gif (356 bytes)

Examples for URLFetch

links to examples on this page:
the basic (GET) fetch
the basic (GET) fetch with a twist
the basic fetch through a proxy server
the tricky (POST) fetch
a fetch with a munchie
a fetch with a few munchies


the basic (GET) fetch

this is an example of the simplest way to fetch a document
'set the timeout just in case this thing takes too long
Server.ScriptTimeOut = 5000

'create the object
Set URLFetchObj = Server.CreateObject("URLFetch.URLFetch")

'set the URL you want to retrieve (URLFetch assumes a GET request if you don't give it any POST data)
URLFetchObj.SetURL("http://www.amazon.com/")

'fetch the contents of the URL into the variable sResponse
sResponse = URLFetchObj.Fetch()

'write out the contents of sResponse to the client
Response.Write(sResponse)

'clean up after yourself!
Set URLFetchObj = Nothing

'set the timeout back to what it normally is
Server.ScriptTimeOut = 90
 

the basic (GET) fetch with a twist

here's a simple fetch with some data added to the GET request (notice the URL)
'set the timeout just in case this thing takes too long
Server.ScriptTimeOut = 5000

'create the object
Set URLFetchObj = Server.CreateObject("URLFetch.URLFetch")

'set the URL you want to retrieve (this time we tack on some data to that GET request)
URLFetchObj.SetURL("http://search.yahoo.com/bin/search?p=pez")

'fetch the contents of the URL into the variable sResponse
sResponse = URLFetchObj.Fetch()

'write out the contents of sResponse to the client
Response.Write(sResponse)

'clean up after yourself!
Set URLFetchObj = Nothing

'set the timeout back to what it normally is
Server.ScriptTimeOut = 90
 

the basic fetch through a proxy server

this is a fetch that takes place by passing through a proxy server
'set the timeout just in case this thing takes too long
Server.ScriptTimeOut = 5000

'create the object
Set URLFetchObj = Server.CreateObject("URLFetch.URLFetch")

'set the URL you want to retrieve (this time we tack on some data to that GET request)
URLFetchObj.SetURL("http://www.e-kiwi.com/")

'set the host and port for the proxy server we need to pass through
URLFetchObj.Proxy = "myproxy.net:3268"

'indicate the username and password needed to authenticate to the proxy (not always necessary)
URLFetchObj.ProxyPassword = "foo:bar"

'fetch the contents of the URL into the variable sResponse
sResponse = URLFetchObj.Fetch()

'write out the contents of sResponse to the client
Response.Write(sResponse)

'clean up after yourself!
Set URLFetchObj = Nothing

'set the timeout back to what it normally is
Server.ScriptTimeOut = 90
 

the tricky (POST) fetch

try this example that illustrates a basic POST request
'set the timeout just in case this thing takes too long
Server.ScriptTimeOut = 5000

'create the object
Set URLFetchObj = Server.CreateObject("URLFetch.URLFetch")

'set the URL you want to retrieve (this time we're going to try a POST request)
URLFetchObj.SetURL("http://www.imdb.com/Find")

'add the data for the POST request (remember to separate parameters with the & symbol)
URLFetchObj.PostData = "select=Titles&for=delicatessen"

'fetch the contents of the URL into the variable sResponse
sResponse = URLFetchObj.Fetch()

'write out the contents of sResponse to the client
Response.Write(sResponse)

'clean up after yourself!
Set URLFetchObj = Nothing

'set the timeout back to what it normally is
Server.ScriptTimeOut = 90
 

a fetch with a munchie

this one shows you how to retrieve a cookie and send it back again
'set the timeout just in case this thing takes too long
Server.ScriptTimeOut = 5000

'create the object
Set URLFetchObj = Server.CreateObject("URLFetch.URLFetch")

'set the URL you want to retrieve (this site sets a cookie; we're going to grab it, print it out, then send it back)
URLFetchObj.SetURL("http://home.cnet.com/")

'fetch the contents of the URL into the variable sResponse
sResponse = URLFetchObj.Fetch()

'write out the contents of sResponse to the client
Response.Write(sResponse)
Response.Write("<hr>")

'Cookies are sent in the header of the HTTP response, URLFetch gives them all to you in an array
Headers = URLFetchObj.GetHeaders()

'loop through all of the HTTP headers; print them all out; when you find the cookie set it to be sent in the next fetch
Response.Write("<em>These are the headers that came in the HTTP response from that last fetch:</em><br>")
If NOT IsNull(Headers) Then
  For i = 1 to UBound(Headers, 1)
    If Headers(i, 0) = "Set-Cookie" Then
      Response.Write("Cookie: " & Headers(i, 0) & ": " & Headers(i, 1) & "<br>")
      Call URLFetchObj.SetHeader("Cookie", Headers(i, 1))
    Else
      Response.Write("Header: " & Headers(i, 0) & ": " & Headers(i, 1) & "<br>")
    End If
  Next
End If

'fetch the contents of the URL into the variable sResponse (we're retrieving from the same URL)
sResponse = URLFetchObj.Fetch()

'write out the contents of sResponse to the client
Response.Write("<hr>")
Response.Write(sResponse)

'clean up after yourself!
Set URLFetchObj = Nothing

'set the timeout back to what it normally is
Server.ScriptTimeOut = 90
 

a fetch with a few munchies

this example is just like the last one, except that it retrieves and sets multiple cookies
you can actually try this one out by signing up for an account at headhunter.net and substituting your own e-mail address and password in the code!
'set the timeout just in case this thing takes too long
Server.ScriptTimeOut = 5000

'create the object
Set URLFetchObj = Server.CreateObject("URLFetch.URLFetch")

'set the URL and POST data (here we log in to the site)
URLFetchObj.SetURL("http://back1.Headhunter.net/scripts/UserLogin.asp")
'use your own e-mail address and password in this next line of code
URLFetchObj.PostData = "Email=me@mymail.com&Passwd=KJDK8H&btnUASubmit=Submit"

'fetch the contents of the URL into the variable sResponse
sResponse = URLFetchObj.Fetch()

'write out the contents of sResponse to the client
Response.Write(sResponse)
Response.Write("<hr>")

'grab all of the headers
Headers = URLFetchObj.GetHeaders()

'here we're going to retrive multiple cookies and set them so that they'll all be sent back to the server with the next request
Response.Write("<em>These are the headers that came in the HTTP response from that last fetch:</em><br>")
If NOT IsNull(Headers) Then
  For i = 1 to UBound(Headers, 1)
    If Headers(i, 0) = "Set-Cookie" Then
      Response.Write("Cookie: " & Headers(i, 0) & ": " & Headers(i, 1) & "<br>")
      Call URLFetchObj.SetHeader("Cookie", Headers(i, 1))
    Else
      Response.Write("Header: " & Headers(i, 0) & ": " & Headers(i, 1) & "<br>")
    End If
  Next
End If
'this new URL is the one we use to view our resumes
URLFetchObj.SetURL("http://back1.Headhunter.net/scripts/resMan.asp")

'fetch the contents of the URL into the variable sResponse
sResponse = URLFetchObj.Fetch()

'write out the contents of sResponse to the client
Response.Write("<hr>")
Response.Write(sResponse)

'clean up after yourself!
Set URLFetchObj = Nothing

'set the timeout back to what it normally is
Server.ScriptTimeOut = 90