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
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>





1.
fj | January 27, 2003 at 3:01 pm
Does anyone see the code?
2.
nathan jacobs | January 27, 2003 at 3:09 pm
Fixed… thanks.
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.
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
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…
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.
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")