Robert
Robert

Reputation: 42575

Setting a proxy in PowerShell on for the PowerShell WebClient?

I am developing a PowerShell script that uses HTTP to access REST services. For debugging purposes I want to redirect all HTTP traffic created by that script through a local proxy (Fiddler).

What I don't want to is to set Fiddler as system wide proxy in IE/ Windows internet settings as this would redirect the traffic of my whole system through Fiddler (especially because Fiddler decrypts SSL/TLS traffic).

How do I set a proxy that affects only one WebClient instance or only the PowerShell?

Upvotes: 0

Views: 3053

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174445

Use the WebProxy class and instantiate it with the address to Fiddler (listens on port 8888 by default):

$FiddlerWP = New-Object System.Net.WebProxy "http://127.0.0.1:8888"

$WebClient = New-Object System.Net.WebClient
$WebClient.Proxy = $FiddlerWP

# This request will now get proxied through Fiddler
$WebClient.DownloadString("https://test.site.example")

Upvotes: 2

Related Questions