ASP get External Page Content

ASP Best Practices

Load external content through MSXML object


Date : 2006-03-31
The MSXML object has been called the "Glue that holds the internet together" and for good reason. Using MSXML is going to be your first step to consume web feeds in any format, incorporate dynamic content from one site into many or as an alternative to setting up a complicated and time consuming SOAP application. First we'll walk through the steps and then I'll give you the function that I use to encapsulate the MSXML GET operation. You will find the POST operation in a separate article.

First then, we turn off ASP error checking so we can handle any errors involved in getting the external page content.

Next we create the object. I've used the latest version that was available at the time of this article, but always check to see that you are using the latest version as there have been many. Please leave a comment here if a new version has come out so I can make any necessary adjustments to keep this code up to date.

Then we open the connection to the URL. I use Asynchronous connection because if there is a delay in returning the content we don't want to jam up our visitors waiting for the other website. This gives us the opportunity to determine how long we want to wait for this content.

Finally we send the request. Then we wait for a response, I've set it to 10 seconds which is pretty generous, most content you will want to gather with this method will only be worth waiting 3-5 seconds for. If your page already takes 3 seconds to load you're not going to want to make it take 13 seconds because of someone elses slow server.

Then we check for errors and for a properly returned page. Status will be 200 if the page was returned correctly.

Last we set our function value and we’re out. Feel free to use the code but please post a comment if you find any way to improve on it.

Code:
Function getURL(url)
  On Error Resume Next
  Set http = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
  http.open "GET", url, true
  http.send
  If http.readyState <> 4 then
    http.waitForResponse 10
  End If
  If Err.Number = 0  AND http.Status = 200 then
    getURL=http.responsetext
  else
    getURL=""
  end if   
  set http = nothing
End Function

Comments :

No comments yet
  • Search For Articles