How to obtain numeric HTTP status codes in PowerShell

后端 未结 4 668
天命终不由人
天命终不由人 2021-01-30 17:35

I know of a few good ways to build web clients in PowerShell: the .NET classes System.Net.WebClient and System.Net.HttpWebRequest, or the COM object Msxml2.XMLHTTP. From what I

4条回答
  •  离开以前
    2021-01-30 18:12

    Using both x0n and joshua ewer's answers to come full circle with a code example, I hope that's not too bad form:

    $url = 'http://google.com'
    $req = [system.Net.WebRequest]::Create($url)
    
    try {
        $res = $req.GetResponse()
    } 
    catch [System.Net.WebException] {
        $res = $_.Exception.Response
    }
    
    $res.StatusCode
    #OK
    
    [int]$res.StatusCode
    #200
    

提交回复
热议问题