在Powershell中使用Proxy for WebRequest

时间:2013-01-18 10:26:56

标签: web-services powershell proxy webrequest connectivity

我在脚本PowerShell中使用WebRequest来检查url是否有效并检测webservice是否可用。

我的脚本WebRequest

$request = [System.Net.WebRequest]::Create($WebServiceSSRSRDL)
$request.Method = 'HEAD'

$request.Credentials = [System.Net.CredentialCache]::DefaultCredentials
if ($request.Proxy -ne $null) 
{
    $request.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
}

$response = $request.GetResponse()
$httpStatus = $response.StatusCode
$urlIsValid = ($httpStatus -eq 'OK')
$tryError = $null
$response.Close()

但我收到错误远程服务器返回错误:(500)内部服务器错误。

如果我使用WebClient,我得到的不是错误,一切都好。

我的脚本WebClient

$webclient = New-Object Net.WebClient
$webclient.Credentials = [System.Net.CredentialCache]::DefaultCredentials
if($webclient.Proxy -ne $null)     
{
   $webclient.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
}

$webclient.DownloadString($WebServiceSSRSRDL) | Out-Null

我想在我的脚本中使用WebRequest。有关它的任何建议吗?

0 个答案:

没有答案