2017-09-13 120 views
0

我正在研究以下代碼。如何使用Powershell中的默認憑據運行Web請求

$HTTP_Request =[System.Net.WebRequest]::Create('http://google.com') 
$HTTP_Response = $HTTP_Request.GetResponse() 
$HTTP_Status = [int]$HTTP_Response.StatusCode 
echo $HTTP_Status 

但我想用我的默認憑據來運行它,因爲有它返回401幾個URL,也就是沒有授權客戶端和我需要用我的默認憑證運行。

誰能幫我關於一樣的,因爲我想存儲一些網址的狀態,除了那些被保護的代碼是工作的罰款。

回答

0

所以現在的問題是在使用默認憑證運行Web請求,這裏是我找到了解決方案:

爲PowerShell的2.0: -

$req = [system.Net.WebRequest]::Create($uri) 
$req.UseDefaultCredentials = $true 
try 
{ 
$res = $req.GetResponse() 
} 
catch [System.Net.WebException] 
{ 
$res = $_.Exception.Response 
} 
$int = [int]$res.StatusCode 
echo $int 

對於Powershell的3.0: -

try{ 
$res = Invoke-WebRequest $uri -UseDefaultCredentials 
} 
catch [System.Net.WebException] 
{ 
$res = $_.Exception.Response 
} 
$int = [int]$res.StatusCode 
echo $int 

兩個腳本都非常不錯,但是如果你想找到許多URL的代碼狀態,那麼你應該去的PowerShell 3.0,它在一個更好的方式處理網頁的請求。

相關問題