powershell - 我可以使用 Invoke-WebRequest 将二进制数据写入文件并仍然获得状态码吗?
问题描述
我需要使用 POST 请求下载文件,但是,由于 PowerShell 似乎无法处理管道和变量中的二进制数据(Out-File
不支持二进制数据),我需要-OutFile
使用Invoke-WebRequest
. 但是,这样做时,它不再输出状态代码,以便我可以在变量中捕获它:
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>
解决方案
-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
推荐阅读
- redux - 基于规则的 redux 可观察史诗中的多个动作
- html - 使用 flexbox,我可以有 1 个固定宽度的列和 1 个占用其余宽度的列吗?
- python - PyHook 只在某些时候有效,而不是始终如一
- linux - 使用 Linux 将预告片添加到 csv
- python - 使用 Spyder IDE 和 virtualenv。更改解释器但似乎没有更新
- exception - Yii2 - 捕获需要错误
- sql - Pyspark 自加入创建网络数据
- windows - 添加名称中带有空格的 Active Directory 组会导致 PowerShell 脚本出错
- javascript - Chrome 的 tabcapture api 没有在标签上录制所有音频
- python - python TypeError 不可散列的类型列表