Invoke-Webrequest 没有 POST 登录表单

Invoke-Webrequest does not POST login form

我正在尝试获取需要登录才能访问的本地网页上的一些信息。

我可以运行

$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  

我希望以下命令 return 我在登录后重定向到的页面的数据。

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"

不过,这只是重新return上一页,包括login_form对象。它与 $response 相同,尽管请求似乎完全不同。

我认为这个问题源于每个页面使用相同的 URL,具有不同的查询字符串参数(例如 script=rh&template=login&action=login)returning 不同的页面。但是,我确实以字符串或 Action 属性 的形式将这些字符串包含在我的 -URI 参数中。因此,如果我的 POST 请求完全有效,我不确定我会使用什么其他方法 return 正确的页面。

我完全被困在这里,没有任何错误可以解决。我不认为我对请求有任何错误,因为我花了很长时间调整参数(定义字段的不同语法等),这没有任何区别。

Chrome HAR 文件:Google Drive Download

我终于明白了。表单需要其他字段。有 JS 运行 重命名表单中的字段,因此当我获取登录对象时,我正在填充 "wrong" 字段,因为 Invoke-Webrequest 不会 运行 JS我 post 当然是表格。在使用 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