powershell - 我可以使用 Invoke-WebRequest 将二进制数据写入文件并仍然获取状态代码吗?

标签 powershell download

我需要使用 POST 请求下载文件,但是,由于 PowerShell 似乎无法处理管道和变量中的二进制数据(Out-File 不支持二进制数据),我需要使用 Invoke-WebRequest-OutFile 参数。但是,在执行此操作时,它不再输出状态代码,因此我可以将其捕获到变量中:

PS K:\hmpf> $Filepath = "E:\shmimpf\g\yyv.fgr"
PS K:\hmpf> $URL = "https://wwwh.ipswich.org/tycobrahe/magazine.php?burgerflipcount=76"
PS K:\hmpf> (Invoke-WebRequest -Uri $URL -OutFile $Filepath -Method Post).StatusCode
PS K:\hmpf> (Invoke-WebRequest -Uri $URL -Method Post).StatusCode
200
PS K:\hmpf>

最佳答案

来自documentation for Invoke-WebRequest :

-PassThru

    Indicates that the cmdlet returns the results, in addition to writing them to a file. 
    This parameter is valid only when the OutFile parameter is also used in the command.

所以要结合-OutFile获取响应结果:

(Invoke-WebRequest -Uri $URL -OutFile $Filepath -Method Post -PassThru).StatusCode

关于powershell - 我可以使用 Invoke-WebRequest 将二进制数据写入文件并仍然获取状态代码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61233166/

相关文章:

c++ - 当前步骤 : Building Tasks. json 文件

azure - 如何使用 powershell 为具有共享 key 的 Azure 文件共享构建授权 header

android - 如何从url下载pdf并在android应用程序本身中查看下载的pdf?

HTML 下载页面而不是内容

python - 从 Google Drive Colab 下载数据

powershell - 如何将函数转换为脚本方法?

powershell - 用管道替换 foreach

Powershell 替代 SetSPN

javascript - 从网站/浏览器拖放文件下载到本地文件系统

php - 如何着手保护文件免受未经授权的下载