1

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.

2
  • Does it work when you try loading it from file and not from web? Are you sure html does not have any xmlns defined? Commented Oct 5, 2012 at 15:39
  • Yes I am getting the same error either if I try to read the xml locally or from the web. This html was copied as is from the view-source of the browser. Commented Oct 5, 2012 at 15:55

1 Answer 1

2

Your first problem is addressed here: .load expects 'A string containing a URL that specifies the location of the XML file'; so use .loadXml to check whether Http.ResponseBody contains data that MSXML?.DOMDocument can parse (your second problem).

UPDATE:

Something that 'works' (and why):

  Dim sHTML : sHTML = readAllFromFile("..\data\02.html")
  WScript.Echo sHTML
  Dim oXDoc : Set oXDoc = CreateObject("MSXML2.DOMDocument")
  oXDoc.async = False
  oXDoc.validateOnParse = False
  oXDoc.setProperty "SelectionLanguage", "XPath"
  If oXDoc.loadXML(sHTML) Then
     Dim ndImg : Set ndImg = oXDoc.selectSingleNode("/html/body/center/img")
     Dim httpPost : httpPost = ndImg.getAttribute("alt")
     WScript.Echo "ok", httpPost
  Else
     WScript.Echo "Error: " & trimWS(oXDoc.parseError.reason)
  End If

output:

<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>

ok You are now connected

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:

Error: End tag 'center' does not match the start tag 'img'.

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:

  Dim sHTML : sHTML = readAllFromFile("..\data\01.html")
  WScript.Echo sHTML
  Dim oHF : Set oHF = CreateObject("HTMLFILE")
  oHF.write sHTML
  Dim httpPost : httpPost = oHF.documentElement.childNodes(1).childNodes(0).childNodes(0).alt
  WScript.Echo "ok", httpPost

output:

<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>

ok You are now connected

As the output shows, HTMLFILE accepts your 'not-xml-closed' img; the method to get what you really want should be sanitized, of course.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your reply. As I said in my question I tried so many variations, and the link you posted was one of them. I think you are referring to Msxml2.DOMDocument not Msxml2.ServerXMLHTTP. It might end up to what you pointed as my second problem.
@update: Technically speaking the html is valid, the img tag is closed. It might be a parser issue. Do you know any other one that I can use, because I do not have control over html response.
@update ii: works just fine. this is what I ended up doing, thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.