StackOverflow 中有一个漂亮的功能可以解决这个问题。这是链接:执行请求
这应该可以帮助您:
Function Execute-Request()
{
Param(
[Parameter(Mandatory=$True)]
[string]$Url,
[Parameter(Mandatory=$False)]
[System.Net.ICredentials]$Credentials,
[Parameter(Mandatory=$False)]
[bool]$UseDefaultCredentials = $True,
[Parameter(Mandatory=$False)]
[Microsoft.PowerShell.Commands.WebRequestMethod]$Method = [Microsoft.PowerShell.Commands.WebRequestMethod]::Get,
[Parameter(Mandatory=$False)]
[Hashtable]$Header,
[Parameter(Mandatory=$False)]
[string]$ContentType
)
$client = New-Object System.Net.WebClient
if($Credentials) {
$client.Credentials = $Credentials
}
elseif($UseDefaultCredentials){
$client.Credentials = [System.Net.CredentialCache]::DefaultCredentials
}
if($ContentType) {
$client.Headers.Add("Content-Type", $ContentType)
}
if($Header) {
$Header.Keys | % { $client.Headers.Add($_, $Header.Item($_)) }
}
$data = $client.DownloadString($Url)
$client.Dispose()
return $data
}
用法:
Execute-Request -Url "https://URL/ticket" -UseDefaultCredentials $true
Execute-Request -Url "https://URL/ticket" -Credentials $credentials -Header @{"Accept" = "application/json"} -ContentType "application/json"