如何使用 Powershell 向 RFC 3161 时间戳服务器提交时间戳请求

How to Submit Timestamp Request to RFC 3161 Timestamp Server with Powershell

如何使用 PowerShell 的 Invoke-WebRequest 向 RFC 3161 时间戳服务器提交时间戳请求?

首先,我创建时间戳请求:

openssl ts -query -data message.txt -cert -sha256 -no_nonce -out ts_test_msg_sha256.tsq

然后我可以使用这个curl命令将它提交到时间戳服务器。这已确认有效。

curl -k -H "Content-Type: application/timestamp-query" -H "Host:timestamp.digicert.com" --data-binary @ts_test_msg_sha256.tsq http://timestamp.digicert.com > ts_test_msg_sha256.tsr

我正尝试在 PowerShell 中做同样的事情,但它不起作用。这是我试过的 PowerShell 命令:

Invoke-WebRequest -uri http://timestamp.digicert.com -Headers @{'Host' = 'timestamp.digicert.com'; 'Content-Type' = 'application/timestamp-query'} -body "ts_test_msg_sha256.tsq" -method POST > "ts_test_msg_sha256.tsr"

我得到这个错误:

Invoke-WebRequest : The remote server returned an error: (404) Not Found.

我做错了什么?据我了解,PowerShell 命令应该与 curl 命令完全相同。

编辑:我使用的最终命令是:

Invoke-WebRequest -uri http://timestamp.digicert.com -Headers @{'Host' = 'timestamp.digicert.com'; 'Content-Type' = 'application/timestamp-query'} -infile "ts_test_msg_sha256.tsq" -method POST -outfile ts_test_msg_sha256.tsr | Out-Null

TS 服务器returns 404 响应不正确的请求。

正确的要求是

$R = Invoke-WebRequest -uri http://timestamp.digicert.com/ -ContentType 'application/timestamp-query' -InFile "ts_test_msg_sha256.tsq" -method POST

请注意 -ContentType-InFile 参数。

响应保存在 $R 变量中。将原始响应主体写入二进制文件:

if ($R.StatusCode -eq 200) { 
    Set-Content ts_test_msg_sha256.tsr -Value $R.Content -AsByteStream; 
    echo "Done" 
} else { 
    echo "Request failed: $($R.StatusCode)" 
}