Invoke-WebRequestで200や404などHTTPステータスコードを参照する

 PS> $resp = try { curl https://httpbin.org/status/404 } catch { $_.Exception.Response }
 PS> $resp
 IsMutuallyAuthenticated : False
 Cookies                 : {}
 Headers                 : {Connection, Access-Control-Allow-Origin, Access-Control-Allow-Credentials, Content-Length...}
 SupportsHeaders         : True
 ContentLength           : 0
 ContentEncoding         :
 ContentType             : text/html; charset=utf-8
 CharacterSet            : utf-8
 Server                  : gunicorn/19.9.0
 LastModified            : 2018/09/13 8:24:50
 StatusCode              : NotFound
 StatusDescription       : NOT FOUND
 ProtocolVersion         : 1.1
 ResponseUri             : https://httpbin.org/status/404
 Method                  : GET
 IsFromCache             : False
  • 404などは例外が発生するので、catchで補足する。

cURLコマンドを使う場合

 PS> curl.exe https://httpbin.org/status/404 -w '%{http_code}\n'
 404

参考

https://stackoverflow.com/questions/42231486/capture-response-on-failed-401-invoke-webrequest


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS

Last-modified: 2018-09-13 (木) 08:47:17