Home / Blog / Proxy 101 / PowerShell Invoke-WebRequest with Proxy
Learn how integrating Invoke WebRequest with a proxy server to enhance requests' security, speed, and privacy.
PowerShell Invoke WebRequest cmdlet is used to send HTTP and HTTPS requests, to web services and APIs enabling developers to automate actions, like fetching web pages submitting forms, and working with REST APIs. In this article, we’ll explore how integrating Invoke WebRequest with a proxy server can enhance these requests’ security, speed, and privacy.
A proxy server acts as a middleman, between a user’s device and the internet. It manages all communications between the user and the server, hiding the user’s IP address to improve security and privacy. Moreover, it can improve performance by storing data controlling access, and monitoring traffic for problem solving.
For instance, when a user requests a web page, the proxy server first steps in. It forwards the request to the intended destination using its IP address. The website responds to the server, which then relays it back to the user.
Using the Invoke-WebRequest cmdlet is pretty straightforward. It is widely used for automating web-related tasks. Here are some simple examples of using Invoke-WebRequest:
Invoke-WebRequest
Invoke-WebRequest -Uri "https://example.com" -OutFile "example.html"
Invoke-WebRequest -Uri "https://example.com/file.zip" -OutFile "file.zip"
$FormFields = @{ "username" = "user" "password" = "pass" } Invoke-WebRequest -Uri "https://example.com/login" -Method Post -Body $FormFields
Invoke-WebRequest -Uri "https://api.example.com/data" -Headers @{ "Authorization" = "Bearer token" }
$response = Invoke-WebRequest -Uri "https://example.com" -SessionVariable "webSession" Invoke-WebRequest -Uri "https://example.com/nextpage" -WebSession $webSession
Adding a proxy server to your Invoke-WebRequest operations can greatly improve the security, speed, and management of your web requests. This guide will walk you through setting up proxies with Invoke-WebRequest.
PowerShell provides built-in parameters for configuring proxy settings directly within the Invoke-WebRequest cmdlet. This method is useful for setting proxy settings per request.
You can specify a proxy server using the -Proxy parameter. Here is how you can make a web request through a proxy server using Invoke-WebRequest.
-Proxy
Invoke-WebRequest -Uri "https://example.com" -Proxy "http://proxyserver:8080"
If the proxy server requires authentication to allow the web request to pass through, you can use -ProxyCredential parameter to specify the credentials required for the authentication.
-ProxyCredential
$ProxyCred = Get-Credential Invoke-WebRequest -Uri "https://example.com" -Proxy "http://proxyserver:8080" -ProxyCredential $ProxyCred
Using proxy preferences globally ensures that all web requests use the same proxy configuration. This approach is ideal for environments where all network traffic must pass through a proxy.
Here is how to set global proxy settings in your PowerShell profile:
[system.net.webrequest]::defaultwebproxy = new-object system.net.webproxy('http://proxyserver:8080') [system.net.webrequest]::defaultwebproxy.credentials = [system.net.credentialcache]::defaultnetworkcredentials
When you set up proxy settings, you can personalize how Invoke-WebRequest is used. For instance, you have the option to set up Invoke-WebRequest to skip addresses, establish proxies for different protocols, and switch proxies dynamically depending on specific conditions.
$proxy = New-Object System.Net.WebProxy('http://proxyserver:8080') $proxy.BypassList += "*.local" [System.Net.WebRequest]::DefaultWebProxy = $proxy
$Proxy1 = New-Object System.Net.WebProxy('http://proxy1:8080') $Proxy1.Credentials = Get-Credential $Proxy2 = New-Object System.Net.WebProxy('http://proxy2:8080') $Proxy2.Credentials = Get-Credential
$Proxy1 = New-Object System.Net.WebProxy('http://proxy1:8080') $Proxy1.Credentials = Get-Credential $Proxy2 = New-Object System.Net.WebProxy('http://proxy2:8080') $Proxy2.Credentials = Get-Credential # Use Proxy1 for the first request Invoke-WebRequest -Uri "https://example.com/api1" -Proxy $Proxy1.Address -ProxyCredential $Proxy1.Credentials # Use Proxy2 for the second request Invoke-WebRequest -Uri "https://example.com/api2" -Proxy $Proxy2.Address -ProxyCredential $Proxy2.Credentials
function Get-Proxy { param ( [string]$Url ) if ($Url -match "internal") { return New-Object System.Net.WebProxy('http://internal-proxy:8080') } else { return New-Object System.Net.WebProxy('http://external-proxy:8080') } } $Url1 = "https://internal.example.com" $Url2 = "https://external.example.com" # Use the dynamic proxy function for different URLs $Proxy1 = Get-Proxy -Url $Url1 Invoke-WebRequest -Uri $Url1 -Proxy $Proxy1.Address -ProxyCredential $Proxy1.Credentials $Proxy2 = Get-Proxy -Url $Url2 Invoke-WebRequest -Uri $Url2 -Proxy $Proxy2.Address -ProxyCredential $Proxy2.Credentials
While using Invoke-WebRequest with proxies provides more customization options, frustrating situations can occur where things don’t work as expected. Here are some common proxy-related issues you might encounter and their solutions.
# Check the proxy server address and connectivity Test-NetConnection -ComputerName "proxyserver" -Port 8080 # Correct the proxy address if necessary Invoke-WebRequest -Uri "https://example.com" -Proxy "http://proxyserver:8080"
Invoke-WebRequest -Uri "https://example.com" -Proxy "http://proxyserver:8080" -TimeoutSec 120
$proxy = New-Object System.Net.WebProxy('http://proxyserver:8080') $proxy.BypassList += "*.local" $proxy.BypassList += "intranet.company.com" [System.Net.WebRequest]::DefaultWebProxy = $proxy
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true } Invoke-WebRequest -Uri "https://example.com" -Proxy "http://proxyserver:8080"
In addition to the above, regularly reviewing proxy settings and proxy server logs is recommended to identify issues.
# Verify proxy settings [System.Net.WebRequest]::DefaultWebProxy # Review proxy server logs Get-Content -Path "C:\ProxyServer\logs\proxy.log"
Following best practices always gives you an advantage when creating the ideal solution. So, here are some best practices for using proxies with PowerShell scripts.
Security Best Practices
Monitoring and Logging Best Practices
# Log details of the web request function Log-WebRequest { param ( [string]$Uri, [string]$Proxy ) $logEntry = "Request to $Uri via $Proxy at $(Get-Date)" Add-Content -Path "C:\ProxyLogs\webrequests.log" -Value $logEntry } # Make a web request and log it $proxyAddress = "http://proxyserver:8080" $response = Invoke-WebRequest -Uri "https://example.com" -Proxy $proxyAddress Log-WebRequest -Uri "https://example.com" -Proxy $proxyAddress
Performance Best Practices
# Use multiple proxy servers for load balancing $proxyList = @("http://proxy1:8080", "http://proxy2:8080") foreach ($proxy in $proxyList) { try { Invoke-WebRequest -Uri "https://example.com" -Proxy $proxy -TimeoutSec 30 break } catch { Write-Output "Failed to connect through $proxy. Trying next proxy..." } }
Error Handling Best Practices
Using Invoke-WebRequest with proxies enhances security, performance, and control over web requests. By following best practices and implementing advanced configurations, you can effectively handle complex network environments and ensure reliable, efficient web interactions.
Looking for a proxy provider? Read our Best proxy providers review of the top proxy providers in the industry.
10 min read
Jonathan Schmidt
Wyatt Mercer
9 min read