.net powershell版本2中的会话变量-net.httpWebRequest

.net powershell版本2中的会话变量-net.httpWebRequest,.net,powershell,cookies,powershell-2.0,.net,Powershell,Cookies,Powershell 2.0,我目前正在使用powershell自动化REST调用。我有一个REST API,我用下面的Invoke WebRequest调用我的powershell脚本 用于登录:- function Http-Web-Request([string]$method,[string]$Accept,[string]$contentType, [string]$path,[string]$post) { $url = "$global:restUri/$path" $CookieCon

我目前正在使用powershell自动化REST调用。我有一个REST API,我用下面的Invoke WebRequest调用我的powershell脚本

用于登录:-

function Http-Web-Request([string]$method,[string]$Accept,[string]$contentType, [string]$path,[string]$post)
{



    $url = "$global:restUri/$path"

    $CookieContainer = New-Object System.Net.CookieContainer

    $postData = $post

    $buffer = [text.encoding]::ascii.getbytes($postData)

    [System.Net.HttpWebRequest] $req = [System.Net.HttpWebRequest] [System.Net.WebRequest]::Create($url)
    $req.method = "$method"
    $req.Accept = "$Accept"
    $req.AllowAutoRedirect = $false
    $req.ContentType = "$contentType"
    $req.ContentLength = $buffer.length
    $req.CookieContainer = $CookieContainer
    $req.TimeOut = 50000
    $req.KeepAlive = $true
    $req.Headers.Add("Keep-Alive: 300");
    $reqst = $req.getRequestStream()
    $reqst.write($buffer, 0, $buffer.length)


        try
        {
            [System.Net.HttpWebResponse] $response = $req.GetResponse()  
            $sr = New-Object System.IO.StreamReader($response.GetResponseStream())

            $txt = $sr.ReadToEnd() 
                if ($response.ContentType.StartsWith("text/xml"))
                {
                    ## NOTE: comment out the next line if you don't want this function to print to the terminal
                    Format-XML($txt)
                }



            return $txt


        }

        catch [Net.WebException] 
        { 
            [System.Net.HttpWebResponse] $resp = [System.Net.HttpWebResponse] $_.Exception.Response  
            ## Return the error to the caller
            Throw $resp.StatusDescription
        }

}
Invoke WebRequest-Method Post-uri$loginUri-ContentType application/x-www-form-urlencoded-Body$loginBody-Headers@{“Accept”=“application/xml”}
-SessionVariable CookieSession-UseBasicParsing

在上面的URL中,类似于Server/_Login,在主体中,我的凭据作为

$loginBody = "username=$username&password=$password"
我从这个调用中获取cookie(JSESSIONID),然后将其解析到所有其他调用中。比如说

我的注销看起来像这样:-

function Http-Web-Request([string]$method,[string]$Accept,[string]$contentType, [string]$path,[string]$post)
{



    $url = "$global:restUri/$path"

    $CookieContainer = New-Object System.Net.CookieContainer

    $postData = $post

    $buffer = [text.encoding]::ascii.getbytes($postData)

    [System.Net.HttpWebRequest] $req = [System.Net.HttpWebRequest] [System.Net.WebRequest]::Create($url)
    $req.method = "$method"
    $req.Accept = "$Accept"
    $req.AllowAutoRedirect = $false
    $req.ContentType = "$contentType"
    $req.ContentLength = $buffer.length
    $req.CookieContainer = $CookieContainer
    $req.TimeOut = 50000
    $req.KeepAlive = $true
    $req.Headers.Add("Keep-Alive: 300");
    $reqst = $req.getRequestStream()
    $reqst.write($buffer, 0, $buffer.length)


        try
        {
            [System.Net.HttpWebResponse] $response = $req.GetResponse()  
            $sr = New-Object System.IO.StreamReader($response.GetResponseStream())

            $txt = $sr.ReadToEnd() 
                if ($response.ContentType.StartsWith("text/xml"))
                {
                    ## NOTE: comment out the next line if you don't want this function to print to the terminal
                    Format-XML($txt)
                }



            return $txt


        }

        catch [Net.WebException] 
        { 
            [System.Net.HttpWebResponse] $resp = [System.Net.HttpWebResponse] $_.Exception.Response  
            ## Return the error to the caller
            Throw $resp.StatusDescription
        }

}
Invoke WebRequest-Method Post-uri$logOutUri-ContentType application/xml-Headers@{“Accept”=“application/xml”}
-WebSession$SessionVariable-UseBasicParsing

其中urL是Server/_Logout,并使用-WebSession解析cookie

问题是,我必须使其与powershell版本2兼容,因此必须使用
[System.Net.HttpWebRequest]

因此,我需要一个函数来首先登录,它将返回sessioncokie,然后我必须解析所有其他调用的cookie

以下是我开始的内容,但不知道还有什么内容:-

function Http-Web-Request([string]$method,[string]$Accept,[string]$contentType, [string]$path,[string]$post)
{



    $url = "$global:restUri/$path"

    $CookieContainer = New-Object System.Net.CookieContainer

    $postData = $post

    $buffer = [text.encoding]::ascii.getbytes($postData)

    [System.Net.HttpWebRequest] $req = [System.Net.HttpWebRequest] [System.Net.WebRequest]::Create($url)
    $req.method = "$method"
    $req.Accept = "$Accept"
    $req.AllowAutoRedirect = $false
    $req.ContentType = "$contentType"
    $req.ContentLength = $buffer.length
    $req.CookieContainer = $CookieContainer
    $req.TimeOut = 50000
    $req.KeepAlive = $true
    $req.Headers.Add("Keep-Alive: 300");
    $reqst = $req.getRequestStream()
    $reqst.write($buffer, 0, $buffer.length)


        try
        {
            [System.Net.HttpWebResponse] $response = $req.GetResponse()  
            $sr = New-Object System.IO.StreamReader($response.GetResponseStream())

            $txt = $sr.ReadToEnd() 
                if ($response.ContentType.StartsWith("text/xml"))
                {
                    ## NOTE: comment out the next line if you don't want this function to print to the terminal
                    Format-XML($txt)
                }



            return $txt


        }

        catch [Net.WebException] 
        { 
            [System.Net.HttpWebResponse] $resp = [System.Net.HttpWebResponse] $_.Exception.Response  
            ## Return the error to the caller
            Throw $resp.StatusDescription
        }

}

所以在做了大量的调查之后,我找到了一个方法

我遇到的问题是,cookie容器在我通话之间丢失了。.net中的cookie容器存储在$CookieContainer

我所要做的就是在创建cookie容器时,我必须使其全球化

然后,在我的第一个调用(即登录)期间,将其分配为cookie容器

$req.CookieContainer = $CookieContainer
因此,在登录过程中,当相同的操作成功时,变量$cookiecontainer将与该值一起存储,并且以下对其余部分的所有调用都具有相同的cookie容器

$req.CookieContainer=$CookieContainer


您可以继续使用这个直到您结束会话。

这是一个很好的方法。对答案稍加修改,保持上下文不变。