PowerShell でHTTPステータスコードの取得

by kusakari 5. 1月 2010 17:28

PowerShellからHTTPステータスコードを取得してみます。

下記URLの「HTTPの応答時のステータスコードを取得する」を参考にさせて頂きました。
http://dobon.net/vb/dotnet/internet/httpstatuscode.html

 

HttpWebResponse の StatusCode を int でキャストするとコード値が取得できます。

 

■ サンプル

$url = "http://www.yahoo.co.jp/"
[System.Net.HttpWebRequest]$request = [System.Net.WebRequest]::Create($url)

try
{
    $response = $request.GetResponse()
    $response.Close()

    write ("{0}:`t{1}" -f [int]$response.StatusCode, $response.StatusCode)
}
catch [System.Net.WebException]
{
    $response = $_.Exception.Response
    [System.Net.HttpWebResponse]$err = $response
    $response.Close()

    write ("{0}:`t{1}" -f [int]$err.StatusCode, $err.StatusCode)
}

 

 

■ 結果
5617539

コメントを書く


(アイコンを表示しますか Gravatar )

  Country flag

biuquote
  • コメント
  • プレビュー
Loading



Calendar

<<  9月 2010  >>
月曜火曜水曜木曜金曜土曜日曜
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

RecentComments

Comment RSS