我正在尝试执行:
$postParams = @{user='myUser';password='myPass'};
Invoke-WebRequest -Uri https://MYURL -Method POST -Body $postParams;
而且我总是被 403 禁止,在 ubuntu 或 git 终端中这就像一个魅力(使用 -k 或 --insecure):
curl -k -X POST "https://MYURL" -H "accept: */*" -H "Content-Type: application/json" -d "{ \"user\" : \"myUser\", \"password\" : \"myPass\"}"
或
curl -X POST "https://MYURL" -H "accept: */*" -H "Content-Type: application/json" -d "{ \"user\" : \"myUser\", \"password\" : \"myPass\"}" --insecure
经过一番研究,我尝试了两种解决方案:
解决方案#1:
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true };
$postParams = @{user='myUser';password='myPass'};
Invoke-WebRequest -Uri https://MYURL -Method POST -Body $postParams;
仍然被 403 禁止,但不起作用。
解决方案#2:
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true };
$postParams = @{user='myUser';password='myPass'};
Invoke-WebRequest -Uri https://MYURL -Method POST -Body $postParams;
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy;
$postParams = @{user='myUser';password='myPass'};
Invoke-WebRequest -Uri https://MYURL -Method POST -Body $postParams;
也没有用,仍然被 403 禁止。
任何关于我可能做错了什么的线索?因为它在带有 -k
或 --insecure
的 ubuntu 和 git 终端中工作正常。