1

我有这个VBScript发送HTTP POST请求并读取返回HTML响应的简单方法。

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

HTML响应格式如下:

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

这个脚本的问题是它总是返回Error: Object required: 'objNode'

我尝试了很多XML解析器的变体,最后每次遇到与XML对象相关的相同错误时都放弃了。

4

1 回答 1

2

您的第一个问题在这里得到解决:.load需要“包含指定 XML 文件位置的 URL 的字符串”;所以使用 .loadXml 检查是否Http.ResponseBody 包含MSXML?.DOMDocument可以解析的数据(你的第二个问题)。

更新:

“有效”的东西(以及为什么):

  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

输出:

<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.loadXML(并解析)HTML 代码,前提是它是“XML 有效的”。您的 HTML 不是,因为 img 标记未关闭 - 我为您的原始代码收到的错误消息:

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

如何进一步进行取决于您是否能够/愿意更改 HTML。

更新二:

虽然您可以在将 .ResponseBody提供给 .loadXML之前对它做一些讨厌的事情 - 为什么不使用 HTML 工具来解析 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

输出:

<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

如输出所示,HTMLFILE 接受您的 'not-xml-closed' img; 当然,应该对获得您真正想要的东西的方法进行消毒。

于 2012-10-05T15:59:07.597 回答