Autoit:HTTP请求,如何登录到站点
您好,我正在尝试通过http请求登录en.forgeofempires.com,我真的不知道怎么做,但已经取得了一些好的进展 以下是来自chrome网络的信息Autoit:HTTP请求,如何登录到站点,http,request,autoit,Http,Request,Autoit,您好,我正在尝试通过http请求登录en.forgeofempires.com,我真的不知道怎么做,但已经取得了一些好的进展 以下是来自chrome网络的信息 Request Headers: :authority: en.forgeofempires.com :method: POST :path: /glps/login_check :scheme: https accept: application/json, text/plain, */* accept-encoding: gzip,
Request Headers:
:authority: en.forgeofempires.com
:method: POST
:path: /glps/login_check
:scheme: https
accept: application/json, text/plain, */*
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9,nb;q=0.8,sv;q=0.7
content-length: 85
content-type: application/x-www-form-urlencoded; charset=UTF-8
cookie: metricsUvId=b3101c98-4f26-4614-95a7-fb26978130f9; _fbp=fb.1.1564063487771.1575313619;
ig_conv_last_site=https://en13.forgeofempires.com/game/index; glps_exp_dc=ag1185a;
portal_tid=1595784514678-50397; device_view=full;
portal_ref_url=https://en0.forgeofempires.com/page/;
PHPSESSID=mh81candht9492apd1nb71fiatsmr6p28759fdrcdtelr7u9; XSRF-TOKEN=gdC1RBZsCkekiR6Z-
1EVetD3-
skzAvLg7zSB11YjBXc; portal_ref_session=1; portal_data=portal_tid=1595784514678-
50397&portal_ref_url=https://en0.forgeofempires.com/page/&portal_ref_session=1;
fs_cr=1596385201458
origin: https://en.forgeofempires.com
referer: https://en.forgeofempires.com/glps/iframe-login
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: same-origin
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/84.0.4147.105 Safari/537.36
x-requested-with: XMLHttpRequest
x-xsrf-token: gdC1RBZsCkekiR6Z-1EVetD3-skzAvLg7zSB11YjBXc
Form Data
login[userid]: user
login[password]: pass
login[remember_me]: false
view source:
login%5Buserid%5D=user&login%5Bpassword%5D=pass&login%5Bremember_me%5D=false
这是谷歌得到的回应
cache-control: max-age=0, must-revalidate, private
content-encoding: gzip
content-type: application/json
date: Sun, 02 Aug 2020 16:20:07 GMT
server: nginx
set-cookie: PHPSESSID=smq4rrp2gtp7an4t5vqsjd0npbbk7tumo8n04thkk8nbuer3; path=/; secure; HttpOnly;
SameSite=Lax
set-cookie: glps_remember_me=deleted; expires=Sat, 03-Aug-2019 16:20:06 GMT; Max-Age=0; path=/;
secure; httponly
status: 200
x-said: 549565
在autoit中,我可以通过以下代码发送请求:
#include "WinHttp.au3"
Opt("MustDeclareVars", 1)
; !!!Note that this example will fail because of invalid username and password!!!
; Authentication data
Global $sUsername = "UserName"
Global $sPassword = "Password"
; Address
Global $sAddress = "en.forgeofempires.com"
; Initialize and get session handle
Global $hOpen = _WinHttpOpen()
; Get connection handle
Global $hConnect = _WinHttpConnect($hOpen, $sAddress)
; Request
Global $hRequest = _WinHttpOpenRequest($hConnect, _
"POST", _ ; verb
"/glps/login_check", _ ; target
Default, _ ; version
"en.forgeofempires.com", _ ; referer
"*/*") ; accept
; Send it
_WinHttpSendRequest($hRequest, _
"Content-Type: application/x-www-form-urlencoded" & @CRLF, _
"login%5Buserid%5D=user&login%5Bpassword%5D=pass&login%5Bremember_me%5D=false")
; Wait for the response
_WinHttpReceiveResponse($hRequest)
; See what's returned
If _WinHttpQueryDataAvailable($hRequest) Then
Global $sHeader = _WinHttpQueryHeaders($hRequest)
ConsoleWrite($sHeader & @CRLF)
; Check if proper cookie is given
If StringInStr($sHeader, 'Set-Cookie: uchome_loginuser=' & $sUsername) Then
MsgBox(0, "", "Login success")
Else
MsgBox(0, "", "Login failed")
EndIf
Else
MsgBox(48, "Error", "Site is experiencing problems.")
EndIf
; Close open handles and exit
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)
这是我在控制台中得到的回应
HTTP/1.1 200 OK
Cache-Control: max-age=0, must-revalidate, private
Connection: keep-alive
Date: Sun, 02 Aug 2020 16:56:14 GMT
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8
Server: nginx
Set-Cookie: XSRF-TOKEN=8b33ZwZLqTnxwlK6wTPbUN_gKcNGTa1y1AxxiRk0gEk; path=/; secure
Set-Cookie: glps_exp_dc=deleted; expires=Sat, 03-Aug-2019 16:56:13 GMT; Max-Age=0; path=/; httponly
X-SaId: 549234
我在chrome和console中查看了不同的响应,这个x-xsrf-token可能是我的代码的问题,因为在chrome中,它是在请求头中发送的,但在autoiit中,我在响应头中得到了这个token???
不确定我哪里出了问题,但看起来服务器忽略了用户,并忽略了我发送的内容
让我成功登录到我的帐户的任何帮助都将被占用大量好的,下面是一个更新的答案,但仍然无法登录,但从服务器获得成功响应
#include "WinHttp.au3"
#include <File.au3>
; Authentication data
Global $sUsername = "user"
Global $sPassword = "pass"
; Open needed handles
$hOpen = _WinHttpOpen()
$hConnect = _WinHttpConnect($hOpen, "en.forgeofempires.com")
; Specify the reguest:
$hRequest = _WinHttpOpenRequest($hConnect)
; Send request
_WinHttpSendRequest($hRequest)
; Wait for the response
_WinHttpReceiveResponse($hRequest)
; Get cookies
If _WinHttpQueryDataAvailable($hRequest) Then
Global $sHeader = _WinHttpQueryHeaders($hRequest)
;ConsoleWrite("Response Headers" & @CRLF)
;ConsoleWrite($sHeader & @CRLF)
; Get cookies
$i = 0
FileDelete(@ScriptDir & "\System\Cookies.txt")
Local Const $sFilePath = @ScriptDir & "\System\Cookies.txt"
Local $hFileOpen = FileOpen($sFilePath, $FO_APPEND)
While 1
$sCookie = _WinHttpQueryHeaders($hRequest, $WINHTTP_QUERY_SET_COOKIE, Default, $i)
$i = @extended
If $i Then
;ConsoleWrite("cookie->: " & $sCookie & @crlf)
FileWrite($hFileOpen, "Set-Cookie: " & $sCookie & @CRLF)
Else
ExitLoop
EndIf
WEnd
FileClose($hFileOpen)
Else
MsgBox(48, "Error", "Site is experiencing problems.")
EndIf
FileOpen($sFilePath, 0)
;MsgBox(48, "Cookie Payload line 2", FileReadLine($sFilePath, 2))
;MsgBox(48, "Cookie Payload line 4", FileReadLine($sFilePath, 4))
; Request
Global $hRequest = _WinHttpOpenRequest($hConnect, _
"POST", _ ; verb
"/glps/login_check", _ ; target
Default, _ ; version
"https://en.forgeofempires.com/glps/iframe-login", _ ; referer
"*/*") ; accept
$sHeaders = "Content-Type: application/x-www-form-urlencoded" & @CRLF
; Send it
_WinHttpSendRequest($hRequest, _
$sHeaders, _ ;header(s)
"login%5Buserid%5D=" & $sUsername & "login%5Bpassword%5D=" & $sPassword &
"&login%5Bremember_me%5D=false")
; Wait for the response
_WinHttpReceiveResponse($hRequest)
; See what's returned
If _WinHttpQueryDataAvailable($hRequest) Then
Global $sHeader = _WinHttpQueryHeaders($hRequest)
ConsoleWrite($sHeader & @CRLF)
; Check if proper cookie is given
If StringInStr($sHeader, 'Set-Cookie: uchome_loginuser=' & $sUsername) Then
MsgBox(0, "", "Login success")
Else
MsgBox(0, "", "Login failed")
EndIf
Else
MsgBox(48, "Error", "Site is experiencing problems.")
EndIf
; Close handles
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)
#包括“WinHttp.au3”
#包括
; 认证数据
全局$sUsername=“用户”
全局$sPassword=“通过”
; 打开所需的手柄
$hOpen=\u WinHttpOpen()
$hConnect=\u WinHttpConnect($hOpen,“en.forgeofempires.com”)
; 指定reguest:
$hRequest=\u WinHttpOpenRequest($hConnect)
; 发送请求
_WinHttpSendRequest($hRequest)
; 等待回应
_WinHttpReceiveResponse($hRequest)
; 吃饼干
如果WinHttpQueryData可用($hRequest),则
全局$sHeader=\u WinHttpQueryHeaders($hRequest)
;控制台写入(“响应头”和@CRLF)
;控制台写入($sHeader和@CRLF)
; 吃饼干
$i=0
FileDelete(@ScriptDir&“\System\Cookies.txt”)
本地常量$sFilePath=@ScriptDir&“\System\Cookies.txt”
本地$hFileOpen=FileOpen($sFilePath,$FO_APPEND)
而1
$sCookie=\u WinHttpQueryHeaders($hRequest,$WinHttpQueryCookie\u SET\u COOKIE,默认值,$i)
$i=@扩展
如果我是$i,那么
;ConsoleWrite(“cookie->:”&$sCookie&@crlf)
文件写入($hFileOpen,“设置Cookie:&$sCookie&@CRLF)
其他的
ExitLoop
恩迪夫
温德
文件关闭($hFileOpen)
其他的
MsgBox(48,“错误”,“站点遇到问题”)
恩迪夫
文件打开($sFilePath,0)
;MsgBox(48,“Cookie有效负载行2”,FileReadLine($sFilePath,2))
;MsgBox(48,“Cookie有效负载行4”,FileReadLine($sFilePath,4))
; 要求
全局$hRequest=\u WinHttpOpenRequest($hConnect_
“POST”,动词
“/glps/login\u check”,\u;目标
默认版本
"https://en.forgeofempires.com/glps/iframe-login“,”参考
"*/*") ; 接受
$sHeaders=“内容类型:应用程序/x-www-form-urlencoded”&@CRLF
; 发送它
_WinHttpSendRequest($hRequest_
$sHeaders,页眉
“登录%5Buserid%5D=“&$sUsername&”登录%5Bpassword%5D=“&$sPassword&”
“&登录名%5b请记住\u me%5D=false”)
; 等待回应
_WinHttpReceiveResponse($hRequest)
; 看看有什么回报
如果WinHttpQueryData可用($hRequest),则
全局$sHeader=\u WinHttpQueryHeaders($hRequest)
控制台写入($sHeader和@CRLF)
; 检查是否提供了正确的cookie
如果StringInStr($sHeader,'Set Cookie:uchome_loginuser=''和$sUsername),那么
MsgBox(0,“,“登录成功”)
其他的
MsgBox(0,,“登录失败”)
恩迪夫
其他的
MsgBox(48,“错误”,“站点遇到问题”)
恩迪夫
; 闭合手柄
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)