我面临的问题是从POST请求中获取cookie,以便在以下GET请求中进行身份验证。
问题在于,尽管将cookie复制到下一个要进行身份验证的GET请求,但我仍然收到身份验证错误。我不确定我做错了什么。
$url = "www.example.com/post"
$postParams = @{user_id='username';user_password='password'}
$webrequest = Invoke-WebRequest -Uri $url -SessionVariable websession -Method Post -Body $postParams
$cookies = $websession.Cookies.GetCookies($url)
# Here, you can output all of $cookies, or you can go through them one by one.
$Nextsession=new-object microsoft.powershell.commands.webrequestsession
foreach ($cookie in $cookies) {
# You can get cookie specifics, or just use $cookie
# This gets each cookie's name and value
Write-Host "$($cookie.name) = $($cookie.value)"
$Nextcookie=new-object system.net.cookie
$Nextcookie.name = $cookie.Name
$Nextcookie.path = $cookie.Path
$Nextcookie.value = $cookie.Value
$Nextcookie.domain = $cookie.Domain
$Nextcookie.expires = $cookie.Expires
$Nextcookie.comment = $cookie.Comment
$Nextcookie.commentUri = $cookie.CommentUri
$Nextcookie.discard = $cookie.Discard
$Nextcookie.expired = $cookie.Expired
$Nextcookie.httpOnly = $cookie.HttpOnly
$Nextcookie.port = $cookie.Port
$Nextcookie.secure = $cookie.Secure
$Nextcookie.timeStamp = Get-Date
$Nextcookie.version = $cookie.Verison
$Nextsession.cookies.add($cookie)
}
$url = "www.example.com/get"
$Response = Invoke-RestMethod -Uri $url -Method Get -websession $Nextsession
Return $Response
答案 0 :(得分:0)
您创建了一个名为$NextCookie
的新Cookie,但您没有对其执行任何操作。我想您要在$NextCookie
语句中添加$Nextsession.cookies.add()
而不是$cookie
。
因此,我认为代码的第37行应该是$Nextsession.cookies.add($Nextcookie)
而不是$Nextsession.cookies.add($cookie)