你可以看到很多人在网站上使用鼠标和按键模拟在浏览器窗口或使用IE来实现自动化,但是对于一些应用程序来说,你不希望你的应用程序占用上百兆内存,并且使用负载的CPU来渲染网站等等。
所以问题是:
如何在没有浏览器但使用AutoHotkey COM的情况下使用WinHttpRequest登录网站/ the服务?
发布于 2015-04-05 15:29:29
我已经在AHK论坛上发布了这个信息,但是我认为这些信息是足够有用的,可以在Stackoverflow上存档。:)
工具与开始
首先,如果您想做诸如登录之类的事情,您可能应该学习一些HTML和HTTP协议的基础知识。费德勒和SetProxy(2,localhost:8888)将对您的调试和反向工程提供很大帮助。我还建议您的浏览器使用add on以快速实现清理你的饼干。
实例1(知识产权委员会论坛)
好了,现在让我们来看看一些例子。登录到autohotkey.com论坛会是什么样子?
为了反向工程that站点的登录,我简单地分析了autohotkey.com的浏览器HTTP请求(为此在浏览器中使用Fiddler或F12 ),通过一些尝试和错误,我能够将其最小化到最基本的水平。我们需要两个请求,登录需要一个请求头,以及3个POST数据参数。
以下是我们要做的事情:
示例1代码
;Prepare our WinHttpRequest object
HttpObj := ComObjCreate("WinHttp.WinHttpRequest.5.1")
;HttpObj.SetProxy(2,"localhost:8888") ;Send data through Fiddler
HttpObj.SetTimeouts(6000,6000,6000,6000) ;Set timeouts to 6 seconds
;HttpObj.Option(6) := False ;disable location-header rediects
;Set our URLs
loginSiteURL := "http://www.autohotkey.com/board/index.php?app=core&module=global§ion=login"
loginURL := "http://www.autohotkey.com/board/index.php?app=core&module=global§ion=login&do=process"
;Set our login data
username := "Brutosozialprodukt"
password := "xxxxxxxxxxxxxx"
rememberMe := "1"
;Step 1
HttpObj.Open("GET",loginSiteURL)
HttpObj.Send()
;Step 2
RegExMatch(HttpObj.ResponseText,"<input\stype='hidden'\sname='auth_key'\svalue='(\w+)'\s/>",match)
auth_key := match1
;Step 3
loginBody := "auth_key=" auth_key "&ips_username=" username "&ips_password=" password "&rememberMe=" rememberMe
;Step 4/5
HttpObj.Open("POST",loginURL)
HttpObj.SetRequestHeader("Content-Type","application/x-www-form-urlencoded")
HttpObj.Send(loginBody)
;Step 6
If (InStr(HttpObj.ResponseText,"<title>Sign In"))
MsgBox, The login failed!
Else
MsgBox, Login was successfull!如果正确地更改URL,这可能对大多数IPB论坛都有效。
例2 (phpbb论坛)
让我们再次登录到新的/其他AHK论坛(这将更容易)。
示例2代码
;Prepare our WinHttpRequest object
HttpObj := ComObjCreate("WinHttp.WinHttpRequest.5.1")
;HttpObj.SetProxy(2,"localhost:8888") ;Send data through Fiddler
HttpObj.SetTimeouts(6000,6000,6000,6000) ;Set timeouts to 6 seconds
;HttpObj.Option(6) := False ;disable location-header rediects
;Set our URLs
loginURL := "http://ahkscript.org/boards/ucp.php?mode=login"
;Set our login data
username := "Brutosozialprodukt"
password := "xxxxxxxxxxxxxx"
autologin := "on"
;Step 1
loginBody := "username=" username "&password=" password "&autologin=" autologin "&login=Login"
;Step 2/3
HttpObj.Open("POST",loginURL)
HttpObj.SetRequestHeader("Content-Type","application/x-www-form-urlencoded")
HttpObj.Send(loginBody)
;Step 4
If (InStr(HttpObj.ResponseText,"<title>Login"))
MsgBox, The login failed!
Else
MsgBox, Login was successfull!如果正确地更改URL,这可能对大多数phpbb论坛都有效。
https://stackoverflow.com/questions/29458900
复制相似问题