PowerShell, Web Requests, and Proxies

前端 未结 6 1151
遇见更好的自我
遇见更好的自我 2020-12-12 17:36

When making a simple web request is there a way to tell the PowerShell environment to just use your Internet Explorer\'s proxy settings?

My proxy settings are contro

6条回答
  •  死守一世寂寞
    2020-12-12 17:49

    Somewhat better is the following, which handles auto-detected proxies as well:

    $proxy = [System.Net.WebRequest]::GetSystemWebProxy()
    $proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
    
    $wc = new-object system.net.WebClient
    $wc.proxy = $proxy
    $webpage = $wc.DownloadData($url)
    

    (edit) Further to the above, this definition appears to work fine for me, too:

    function Get-Webclient {
        $wc = New-Object Net.WebClient
        $wc.UseDefaultCredentials = $true
        $wc.Proxy.Credentials = $wc.Credentials
        $wc
    }
    

提交回复
热议问题