所以我有這個代碼在生成xml后將檔案上傳到目的地。檢查服務器時,我發現該檔案包含正確的內容,但是從我的一個 catch 塊中我得到“發生無法解決的錯誤”。我無法判斷這是否是嚴重錯誤,因為我不知道錯誤是什么。該檔案至少已上傳,但我想找出此錯誤以便解決它。有沒有辦法知道這里拋出了什么錯誤?
try {
$wc = New-Object System.Net.WebClient
$rawResponse = $wc.UploadFile("someURIhere", "Post", $File)
$resp = System.Text.Encoding.ASCII.GetString($rawResponse)
Write-Host $resp
}
catch [System.Net.WebException] {
$Request = $_.Exception
Write-host "Exception caught: $Request"
$crapMessage = ($_.Exception.Message).ToString().Trim()
Write-Output $crapMessage
}
catch {
Write-Host "An error occurred that could not be resolved."
}
uj5u.com熱心網友回復:
因為你的第二個catch塊擊中,你知道這是不是一個WebException。
由于此無效行,您的腳本應該會產生錯誤:
$resp = System.Text.Encoding.ASCII.GetString($rawResponse)
你可能想寫的是:
$resp = [System.Text.Encoding]::ASCII.GetString($rawResponse)
此外,如果您使用它,則最安全,以確保您使用正確的編碼:
$resp = $wc.Encoding.GetString($rawResponse)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/324515.html
