我正在尝试在本地网页上获取一些需要登录才能访问的信息。
我可以跑
$response = Invoke-WebRequest "http://web.com/admin/launch?script=rh&template=dashboard" -SessionVariable rb -UserAgent 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'
$response
包含正确的信息,包括我需要使用的名为login_form
的单个表单。
然后,我写道:
$response.Forms[0].Fields.user_id = 'admin'
$response.Forms[0].Fields.password= 'pass'
$response.Forms[0].Fields
Key Value
--- -----
d_user_id user_id
t_user_id string
c_user_id string
e_user_id true
user_id admin
password pass
我希望以下命令返回登录后重定向到的页面数据。
Invoke-WebRequest $('http://web.com/admin/' + $response.Forms[0].Action) -WebSession $rb -Body $response.Forms[0].Fields -Method POST -contentType "application/x-www-form-urlencoded"
但是,这只是重新返回上一页,包括login_form
对象。它与$response
相同,即使请求似乎完全不同。
我认为这个问题来自用于每个页面的相同URL,不同的查询字符串参数(例如script=rh&template=login&action=login
)返回不同的页面。但是,我确实在-URI
param中以字符串或Action
属性的形式包含这些字符串。因此,如果我的POST请求甚至可以正常工作,我不知道我会用什么方法来返回正确的页面。
我完全被困在这里,没有任何错误可以解决。我不认为我对Request有任何错误,因为我花了很长时间调整参数(定义字段的语法不同等),这没有任何区别。
Chrome HAR文件:Google Drive Download
答案 0 :(得分:0)
我终于明白了。表单还需要其他字段。 JS正在运行以重命名表单中的字段,因此当我获取登录对象时,我填充了“错误”字段,因为当我发布表单时,Invoke-Webrequest不会运行JS。在与Fiddler一起玩了很长时间的请求之后,我最终注意到成功登录是这些字段的结果,这些字段的命名方式略有不同。这就是它最终的样子:
$response = Invoke-WebRequest "http://web.com/admin/launch?script=rh&template=login" -SessionVariable x -UserAgent 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'
$response.Forms[0].Fields.Add('f_user_id','admin')
$response.Forms[0].Fields.Add('f_password','')
$null = Invoke-WebRequest $('http://web.com/admin/' + $response.Forms[0].Action) -WebSession $x -Body $response.Forms[0].Fields -Method POST -contentType "application/x-www-form-urlencoded"
Invoke-WebRequest 'http://web.com/admin/launch?script=rh&template=dashboard' -WebSession $x