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
grab an image and spit it out
grab an image and save it for later
example HTTP headers


the basic (GET) fetch

this is an example of the simplest way to fetch a document
'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
 

the basic (GET) fetch with a twist

here's a simple fetch with some data added to the GET request (notice the URL)
'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
 

the basic fetch through a proxy server

this is a fetch that takes place by passing through a proxy server
NOTE: If you're having trouble getting URLFetch to work with a proxy server try assigning your proxy settings to the network connection on the server (e.g. using Internet Explorer under Tools->Internet Options, Connections tab). URLFetch will use these settings by default.
'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.amazon.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
 

the tricky (POST) fetch

try this example that illustrates a basic POST request
'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
 

a fetch with a munchie

this one shows you how to retrieve a cookie and send it back again
'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 ")
Response.Write("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
 

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!
'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 ")
Response.Write("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.URL = "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
 

grab an image and spit it out

this one grabs an image from a remote server and spits it out to the client

This one requires two files.

The first is grab_image.asp:

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

'designate this as a binary file
URLFetchObj.SetIsBinary( true )

'set the URL you want to retrieve (notice that it's an image)
URLFetchObj.SetURL("http://www.geocities.com/TimesSquare/Alley/6026/tpic7.jpg")

'fetch the contents of the URL into the variable sResponse
'NOTE: sResponse will now hold the string "binary" since we're doing a binary fetch.
sResponse = URLFetchObj.Fetch()

'grab the binary data that was retrieved as a result of the fetch.
binaryResult = URLFetchObj.GetBinary()

'set some HTTP headers so that this can be sent as an image.
Response.ContentType = "image/jpeg"
Response.Expires = 0

'write out the binary data to the client.
Response.BinaryWrite( binaryResult )

'clean up after yourself!
Set URLFetchObj = Nothing

Here's the second file, we'll call it display_image.htm, that makes use of grab_image.asp.

<html> <head></head> <body> <!-- Remember that test.asp spits out binary data containing an image, so we use it here like we would any other image. --> <img src="/test.asp"> </body> </html>
 

grab an image and save it for later

this one grabs an image from a remote server and saves it as a file
'create the object
Set URLFetchObj = Server.CreateObject("URLFetch.URLFetch")

'designate this as a binary file
URLFetchObj.SetIsBinary( true )

'set the URL you want to retrieve
URLFetchObj.SetURL("http://www.geocities.com/TimesSquare/Alley/6026/tpic7.jpg")

'fetch the contents of the URL into the variable sResponse
'we don't bother putting the result of the fetch into a variable since it will only
'contain the word "binary" because we're doing a binary fetch.
URLFetchObj.Fetch()

'just call the method and give it the path and name of the file you want to save.
URLFetchObj.SaveAsFile( "C:\Inetpub\wwwroot\my_idol.jpg" )

'clean up after yourself!
Set URLFetchObj = Nothing
 

example HTTP headers

this isn't a complete example, but rather some common HTTP headers to get and set

headers to set (that is, headers you'll want to set before making a request to a server)

'here's a complete set of headers as sent from a browser
URLFetchObj.SetHeader( "Accept-Language", "en-us" );
URLFetchObj.SetHeader( "Connection", "Keep-Alive" );
URLFetchObj.SetHeader( "Accept", "*/*" );
URLFetchObj.SetHeader( "Cache-Control", "no-cache" );
URLFetchObj.SetHeader( "Accept-Encoding", "gzip, deflate" );
URLFetchObj.SetHeader( "User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)" ); 
URLFetchObj.SetHeader( "Referer", "http://www.foo.com/myscript.asp" );

'this one will be sent in the case of a POST request
URLFetchObj.SetHeader( "Content-Type", "application/x-www-form-urlencoded" );

here's a good list of User-Agent strings that I found: http://www.highermind.org/design/user_agent/?Action=List_UA

headers to get (that is, headers you'll after you've made a request to a server)

'here's a complete set of headers as sent from a server
header = URLFetchObj.GetHeader( "Server" );
header = URLFetchObj.GetHeader( "Date" );
header = URLFetchObj.GetHeader( "Content-Length" );
header = URLFetchObj.GetHeader( "Content-Type" );
header = URLFetchObj.GetHeader( "Set-Cookie" );
header = URLFetchObj.GetHeader( "Cache-Control" );