Powershell Redirect URI



我正在尝试使用Powershell中的"Invoke-WebRequest"调用Web应用程序URL,当我调用"internal.com"URL时,它被重定向到另一个网页,其URL为".../xyz.aspx",该网页仅接受电子邮件作为输入并进行身份验证。

我正在使用以下代码(如托马斯建议的那样(并尝试了不同的排列和组合。

$firstRequest = Invoke-WebRequest -Uri 'https://XYZ.internal.com/' -SessionVariable mySession
$firstRequest.Forms[0].Fields["$txtEMAIL"] = "xyz@xyz.com"
$response = Invoke-WebRequest -Uri $firstRequest.BaseResponse.ResponseUri.AbsoluteUri -Body $firstRequest -WebSession $mySession

当我运行上面的代码时,我收到以下消息。

Invoke-WebRequest : Cannot send a content-body with this verb-type.
At line:9 char:18
+ ... ndRequest = Invoke-WebRequest -Uri ($baseUri + $firstRequest.Forms[0] ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [Invoke-WebRequest], ProtocolViolationException
+ FullyQualifiedErrorId : System.Net.ProtocolViolationException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

谢谢。

正如您在代码中看到的,您的第二个请求与您的第一个请求完全无关。您必须改用一个会话。而且你不应该依赖你认为会到来的"下一个"URI,而是使用表单将调用的URI:

# Do your first request to obtain the login form and start maintaining one session
$baseUri = 'https://XYZ.internal.com'
$firstRequest = Invoke-WebRequest -Uri $baseUri -SessionVariable mySession
# Fill out your login form
$firstRequest.Forms[0].Fields["$txtEMAIL"] = "xyz@xyz.com"
# Use the URI defined in the action of your form to send your request to while maintaining your session
$secondRequest = Invoke-WebRequest -Uri ($baseUri + $firstRequest.Forms[0].Action) -Body $firstRequest -WebSession $mySession

请注意,您在定义SessionVariable时没有$,但稍后会将其与$一起使用。

如果您的第二个请求尚未返回所需的网站,请相应地对第二个请求重复这些步骤(使用$secondRequest的表单/正文创建$thirdRequest等(。

最新更新