Skip to main content
 首页 » 编程设计

python之Powershell:Webclient 无法上传字符串

2025年05月04日22dyllove98

我的 https POST 请求在 UNIX 上使用 curl 和 python 使用以下命令工作:

p=subprocess.Popen(['curl','-s','-k','-X','POST', '-H','Content-Type: application/json','-d',message,<https link>],stdout=subprocess.PIPE) 

它可以向网络服务器发送任何字符串。现在,我想在 Windows 上使用 powershell 进行相同的操作。我做了以下事情:

$webclient = new-object system.net.webclient; 
$webclient.UploadString("https://host/message","string in json format"); 

然而,它失败了:

Exception calling "UploadString" with "2" argument(s): "The underlying connection was     closed: Could not establish trust relationship for the SSL/TLS secure channel." 
At line:1 char:24 
+ $webclient.UploadString <<<< ("https://host/message","strng in json format"); 
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException 
+ FullyQualifiedErrorId : DotNetMethodException 

我是否遗漏了什么?任何帮助将不胜感激。

提前致谢!

乔伊

请您参考如下方法:

您尝试过 Invoke-RestMethod 吗?

Invoke-RestMethod -Uri "https://host/message" -Method Post -ContentType "application/json" -Body '{"string in json format"}'