* Invoke-WebRequestで200や404などHTTPステータスコードを参照する [#a33cc22d]
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コマンドを使う場合 [#w2cb17ec]
PS> curl.exe https://httpbin.org/status/404 -w '%{http_code}\n'
404
** 参考 [#ia98b05c]
https://stackoverflow.com/questions/42231486/capture-response-on-failed-401-invoke-webrequest