Using ASP and VBscript for HTTP GET

January 27, 2003

I normally don’t use ASP or VBScript but the need arose to fix some code. I modified some existing code to add some error handling. I am posting it because I could not find any good examples of this on the web so I thought someone might want to see it.

The following bit of code is gets data from the web and stores it in a variable for later use:


<%
on error resume next

Dim objXmlHttp
Dim strHTML
Set objXmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
objXmlHttp.setTimeouts 1000,1000,1000,1000
objXmlHttp.open "GET", "http://www.khakipants.org/", False
objXmlHttp.send

if Err.number = 0 and objXmlHttp.status = 200 then
  strHTML = objXmlHttp.responseText
else
  strHTML = ""
end if

Set objXmlHttp = Nothing
%>

Does anyone see any problems with the code?

Will this break under ANY situation?

Entry Filed under: programming. .

7 Comments Add your own

  • 1. fj  |  January 27, 2003 at 3:01 pm

    Does anyone see the code?

    Reply
  • 2. nathan jacobs  |  January 27, 2003 at 3:09 pm

    Fixed… thanks.

    Reply
  • 3. sil  |  January 28, 2003 at 4:25 am

    Yes. It’ll break if you’ve got the wrong version of the MS XML libraries installed, possibly, because MSXML2.ServerXMLHTTP might not exist. It’s not part of the base install on older MS boxes — I’m not sure what a stock Win2K install will give you, for example. You might want “on error goto 0″ after that code as well, to turn off “resume next” behaviour.

    Reply
  • 4. nathan jacobs  |  January 28, 2003 at 10:01 am

    As someone here at work pointed out there might also be a problem with my expectation of short-circuit logic evaluation.

    if Err.number = 0 then
      if objXmlHttp.status = 200 then
       strHTML = objXmlHttp.responseText
      else
        strHTML = “appropriate message”
      end if
    else
      strHTML = “appropriate message”
    end if

    Reply
  • 5. nathan jacobs  |  February 11, 2004 at 12:56 pm

    Looks like someone (http://www.thekdgroup.com/OnlineApp/Admin/leasing/xmltest.asp) is using my example…

    Reply
  • 6. Matt!  |  December 16, 2008 at 4:02 pm

    Farking fantastic! I just took your code, tweaked it a bit, and dropped it into the admin script I’m writing. Thank you for the time saving snippet.

    Reply
  • 7. Gavin  |  January 30, 2009 at 6:36 am

    Cool, just used your code in a VB Script. Only had to change one thing.

    I used the following code:

    Set objXmlHttp = CreateObject("MSXML2.ServerXMLHTTP")

    Reply

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe to the comments via RSS Feed


About me

Hello, I'm Nathan Jacobs and you are looking at my blog. I am a doctoral candidate in Computer Science at Washington University in St. Louis focusing on Computer Vision. My research is in algorithms to improve the ability of computer to reason about the natural world. I also really like to make attractive and informative visualizations of complex data.

I currently update my flickr site much more frequently than this blog.

RSS twitter

Category Cloud

computer vision friend information retrieval information technology knowledge management landscaping machine learning math mozilla noise personal programming research site admin social software teaching travel ultimate frisbee usability web standards

friend

links

papers

Top Posts

Flickr Photos

Smooth

View from Mill Club 405

View from Mill Club 405

Rainbow Sunrise

Why can't I turn left?

More Photos