小编R D*_*R D的帖子

powershell http发布REST API基本身份验证

我使用curl使用REST API进行基本认证:

curl -X POST  -H 'Accept: application/json' -u user:password http://localhost/test/
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用powershell webRequest时,我得到403(许可被拒绝).当我在REST代码中禁用身份验证检查时,此脚本正常工作.

powershell在POST请求上传递凭据的最佳方式是什么,类似于curl,或者我可以做些什么来修复以下脚本.

非常感谢对此的一些指导.谢谢.

这是我的powershell脚本:

function Execute-HTTPPostCommand() {
    param(
        [string] $target = $null
    )

    $username = "user"
    $password = "pass"

    $webRequest = [System.Net.WebRequest]::Create($target)
    $webRequest.ContentType = "text/html"
    $PostStr = [System.Text.Encoding]::UTF8.GetBytes($Post)
    $webrequest.ContentLength = $PostStr.Length
    $webRequest.ServicePoint.Expect100Continue = $false
    $webRequest.Credentials = New-Object System.Net.NetworkCredential -ArgumentList $username, $password 

    $webRequest.PreAuthenticate = $true
    $webRequest.Method = "POST"

    $requestStream = $webRequest.GetRequestStream()
    $requestStream.Write($PostStr, 0,$PostStr.length)
    v$requestStream.Close()

    [System.Net.WebResponse] $resp = $webRequest.GetResponse();
    $rs = $resp.GetResponseStream();
    [System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs;
    [string] …
Run Code Online (Sandbox Code Playgroud)

rest powershell

20
推荐指数
2
解决办法
4万
查看次数

标签 统计

powershell ×1

rest ×1