[英]VBScript HTTP - how to read DOM element value and have server reply correctly
I'm having lots of trouble with a VBScript that I'm putting together that, when executed, automatically logs into a webpage. 我在使用VBScript时遇到了很多麻烦,这些问题在执行时会自动登录到网页。 The problem I'm facing at the moment is that my POST doesn't mimic correctly the same as the browser does.
我目前面临的问题是我的POST与浏览器无法正确模仿。 Any help is greatly appreciated!
任何帮助是极大的赞赏!
EDIT1: I found a sloppy way to read the DOM element value, but even then there is something wrong with my POST because it differs from that of the browser. EDIT1:我发现读取DOM元素值的方法很草率,但是即使那样,我的POST还是有问题,因为它与浏览器有所不同。
EDIT2: Helen's answer steered me in the right direction, now it works perfectly. EDIT2: Helen的答案将我引向正确的方向,现在它可以正常运行了。
This is the code: 这是代码:
Dim URL
URL = "http://192.168.0.10/login.php"
Dim http : Set http = CreateObject("Msxml2.ServerXMLHTTP.6.0")
http.Open "GET", URL, FALSE
http.Send
Dim html : Set html = CreateObject("HTMLFILE")
html.write http.responseText
' Getting the DOM element's value by it's name
Dim rnd : rnd = html.getElementsByName("rnd").[0].value
'WScript.Echo rnd
sRequest = "username=admin&password=admin&rnd=" & rnd
'Msgbox sRequest, 0, "Final sRequest"
SetCookie = http.getResponseHeader("Set-Cookie")
'Bellow I trim an unnecessary part of the cookie (; path=/;) to keep only the AuthID
SetCookie = Left(SetCookie, Len(SetCookie) - 9)
'Msgbox SetCookie, 0, "Cookie Value"
Set http = nothing
http.Open "POST", URL, False
http.setRequestHeader "Host", "192.168.0.10"
http.setRequestHeader "Content-Length", Len(sRequest)
http.setRequestHeader "Cache-Control", "max-age=0"
http.setRequestHeader "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
http.setRequestHeader "Origin", "http://192.168.0.10"
http.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36"
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
http.setRequestHeader "Referer", "http://192.168.0.10/login.php"
http.setRequestHeader "Accept-Encoding", "gzip,deflate,sdch"
http.setRequestHeader "Accept-Language", "en-US;q=0.6,en;q=0.4"
http.setRequestHeader "Cookie", SetCookie
'Msgbox SetCookie, 0, "Cookie Value"
http.Send sRequest
'Msgbox sRequest, 0, "Final sRequest"
'HTTPGet = http.responseText
Set http = nothing
Thanks again Helen! 再次感谢海伦! )
)
Is this random DOM element on the http://192.168.0.10/login.php
page? 这是
http://192.168.0.10/login.php
页面上的随机DOM元素吗? You could GET
the page source and load it into an HTMLFILE
object. 您可以
GET
页面源并将其加载到HTMLFILE
对象中。 Then you'll be able to extract the needed element, eg using getElementById
: 然后,您将能够提取所需的元素,例如使用
getElementById
:
http.Open "GET", "httpp://192.168.0.10/login.php", False
http.Send
Dim html : Set html = CreateObject("HTMLFILE")
html.write http.responseText
Dim rnd : rnd = html.getElementsByName("rnd").[0].value ' Replace with your element query
WScript.Echo rnd
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.