PowerShell, Web Requests, and Proxies

前端 未结 6 1158
遇见更好的自我
遇见更好的自我 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:59

    Untested:

    $user = $env:username
    $webproxy = (get-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet
    Settings').ProxyServer
    $pwd = Read-Host "Password?" -assecurestring
    
    $proxy = new-object System.Net.WebProxy
    $proxy.Address = $webproxy
    $account = new-object System.Net.NetworkCredential($user,[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd)), "")
    $proxy.credentials = $account
    
    $url = "http://stackoverflow.com"
    $wc = new-object system.net.WebClient
    $wc.proxy = $proxy
    $webpage = $wc.DownloadData($url)
    $string = [System.Text.Encoding]::ASCII.GetString($webpage)
    

    ...

提交回复
热议问题