-1

我目前使用浏览器登录并想获取该会话的当前 cookie。但是当我使用此代码时,它只会为该请求创建另一个会话 ID。不适用于我浏览器中当前登录的会话。

            $url = "http://example.website/" 
            $cookiejar = New-Object System.Net.CookieContainer 
            $webrequest = [System.Net.HTTPWebRequest]::Create($url); 
            $webrequest.CookieContainer = $cookiejar 
            $response = $webrequest.GetResponse() 
            $cookies = $cookiejar.GetCookies($url) 
            foreach ($cookie in $cookies) { 

                Write-Host "$($cookie.name) = $($cookie.value)" 
            }

我想在我的浏览器和脚本中输出类似的会话 id cookie。

4

1 回答 1

0

正如Lee_Dailey建议的那样,您可以使用 IE COM 对象接口,但是 cookie 的详细信息是有限的。详细信息以字符串形式返回,可以将其转换为哈希表以获取键、值对 - 但域或到期等扩展信息将不可用。

这仅适用于 Internet Explorer,我不确定信息的完整性,例如是否可以通过这种方式检索安全 cookie,因此您需要进行测试。

根据您的要求,这可能不够,也可能不够。

#URL we want to retrieve cookie information from
$URL = 'https://stackoverflow.com'

#Hashtable to store the cookie details
$CookieDetails = @{}

#Create a shell object
$Shell = New-Object -ComObject Shell.Application

#Find the web browser tab that starts with our URL
$IE = $Shell.Windows() | Where-Object { $_.Type -eq "HTML Document" -and $_.LocationURL -like "$URL*"}

#Split the cookie string and for each line parse into k,v pairs 
foreach($Line in ($IE.Document.cookie -split "; "))
{
    $Line = $Line -split "="
    $CookieDetails.($Line[0]) = $Line[1..-1] -join "="
}

#Output the hashtable result
$CookieDetails
于 2019-10-07T21:44:58.227 回答