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
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 |
|