I have this simple VBScript that sends an HTTP POST request and reads the returning HTML response.
Function httpPOST(url, body, username, password )
Set Http = CreateObject("Msxml2.ServerXMLHTTP")
Http.Open "POST", url, False, username, password
Http.setRequestHeader _
"Content-Type", _
"application/x-www-form-urlencoded"
Http.send body
pagestatus = Http.status
if pagestatus<> "200" then
httpPOST="Error:"& pagestatus
else
'httpPOST = Http.ResponseBody
'httpPOST = Http.responseText
Set objXMLDoc = CreateObject("MSXML.DOMDocument")
objXMLDoc.async = False
objXMLDoc.validateOnParse = False
objXMLDoc.load(Http.ResponseBody)
Set objNode = objXMLDoc.selectSingleNode("/html/body/center/img")
httpPost = objNode.getAttribute("alt")
end if
End Function
The HTML response format is the following:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>---</title>
</head>
<body>
<center>
<img alt="You are now connected" src="pages/GEN/connected_gen.png">
</center>
</body>
</html>
The issue with this script is that it always returns Error: Object required: 'objNode'
I have tried so many variations of XML parsers, and finally gave up for every time I got the same error related to XML objects.
Your first problem is addressed here:
.loadexpects ‘A string containing a URL that specifies the location of the XML file’; so use .loadXml to check whetherHttp.ResponseBodycontains data that
MSXML?.DOMDocumentcan parse (your second problem).UPDATE:
Something that ‘works’ (and why):
output:
MSXML2.DOMDocument will
.loadXML(and parse) HTML code, provided it is ‘XML-valid’. Your HTML isn’t, because the img tag is not closed – the error message I got for your original code:How to proceed further depends on whether you are able/willing to change the HTML.
UPDATE II:
While you could do nasty things to .ResponseBody before you feed it to .loadXML – why not use a HTML tool to parse HTML:
output:
As the output shows, HTMLFILE accepts your ‘not-xml-closed’ img; the method to get what you really want should be sanitized, of course.