🎯 一款可定制、具备反检测功能的云浏览器,由自主研发的 Chromium驱动,专为网页爬虫AI 代理设计。👉立即试用
返回博客

如何在 PowerShell 中使用代理与 Invoke-WebRequest 结合使用

Olivia Patel
Olivia Patel

Senior Cybersecurity Analyst

17-Dec-2025
快速浏览

掌握 PowerShell 代理配置,以支持安全、匿名的网络请求。通过住宅代理路由流量,以绕过地区限制并避免 IP 阻塞。

关键要点

  • Invoke-WebRequest 的 -Proxy 参数允许通过代理服务器路由流量
  • PowerShell 7.x 支持 HTTPS 和 SOCKS 代理;早期版本仅支持 HTTP
  • ProxyCredential 参数处理需要登录凭据的代理身份验证
  • 住宅代理提供合法的 ISP 分配的 IP,能够击败基于 IP 的阻塞
  • 会话对象在多个请求之间自动保持代理设置

理解 Invoke-WebRequest 代理

Invoke-WebRequest 是 PowerShell 用于发起 HTTP 和 HTTPS 请求的原生命令。-Proxy 参数通过指定的代理服务器路由请求,而不是直接连接到目标网站。此功能使脚本能够:

  • 从特定位置访问地区限制内容
  • 在多个 IP 之间分发请求以避免速率限制
  • 隐藏脚本来源于目标网站
  • 通过企业代理测试网络连接性

代理参数接受格式为:<PROTOCOL>://<HOST>:<PORT>

基本 HTTP 代理配置

最简单的代理设置指定 HTTP 地址和端口:

powershell Copy
$proxyUrl = "http://47.252.29.28:11222"
$response = Invoke-WebRequest -Uri "https://httpbin.io/ip" -Proxy $proxyUrl
Write-Output $response.Content

此命令通过指定的代理服务器路由请求。目标网站接收来自代理 IP 地址的请求,而不是您计算机的 IP。

代理身份验证

许多代理提供商要求身份验证以防止未经授权的使用。-ProxyCredential 参数传递用户名和密码:

powershell Copy
$proxyUrl = "http://proxy.example.com:8080"
$proxyCreds = Get-Credential
$response = Invoke-WebRequest -Uri "https://httpbin.io/ip" `
    -Proxy $proxyUrl `
    -ProxyCredential $proxyCreds

Get-Credential 会打开一个交互提示,要求输入凭据。对于自动化脚本,可以编程创建凭据:

powershell Copy
$username = "proxy_user"
$password = "proxy_password"
$secPassword = ConvertTo-SecureString $password -AsPlainText -Force
$proxyCreds = New-Object System.Management.Automation.PSCredential `
    -ArgumentList $username, $secPassword

$response = Invoke-WebRequest -Uri "https://httpbin.io/ip" `
    -Proxy "http://proxy.example.com:8080" `
    -ProxyCredential $proxyCreds

嵌入凭据的代理 URL

另外,可以直接在代理 URL 中嵌入凭据:

powershell Copy
$proxyUrl = "http://username:password@proxy.example.com:8080"
$response = Invoke-WebRequest -Uri "https://httpbin.io/ip" -Proxy $proxyUrl

这种方法对凭据管理需求较低的脚本来说非常方便。

HTTPS 和 SOCKS 代理支持

PowerShell 7.x+ 版本增加了对 HTTPS 和 SOCKS 代理的支持。如果使用早期版本,则只能使用 HTTP 代理。升级到 PowerShell 7 以获得完整的代理协议支持:

powershell Copy
# HTTPS 代理
$response = Invoke-WebRequest -Uri "https://example.com" `
    -Proxy "https://username:password@proxy.example.com:8080"

# SOCKS5 代理
$response = Invoke-WebRequest -Uri "https://example.com" `
    -Proxy "socks5://username:password@proxy.example.com:1080"

验证您的 PowerShell 版本:

powershell Copy
$PSVersionTable.PSVersion

代理轮换策略

对于大规模抓取或 API 调用,在多个代理之间轮换可以将请求分配到不同的 IP,避免速率限制和 IP 禁令:

powershell Copy
$proxyList = @(
    "http://user:pass@proxy1.example.com:8080",
    "http://user:pass@proxy2.example.com:8080",
    "http://user:pass@proxy3.example.com:8080"
)

$urls = @(
    "https://api.example.com/page1",
    "https://api.example.com/page2",
    "https://api.example.com/page3"
)

foreach ($url in $urls) {
    $randomProxy = $proxyList | Get-Random
    $response = Invoke-WebRequest -Uri $url -Proxy $randomProxy
    Write-Output "来自 $url 的响应使用 $randomProxy"
}

这种方法为每个请求随机选择一个不同的代理,防止按 IP 请求积累,从而触发阻塞。

PowerShell 的优质住宅代理

Scrapeless 住宅代理 为 PowerShell 自动化提供专业级代理基础设施。住宅代理使用合法的 ISP 分配的 IP,显著提高对受保护网站的成功率:

powershell Copy
# Scrapeless 住宅代理,带认证
$proxyUrl = "http://username:password@superproxy.scrapeless.com:1337"

$headers = @{
    'User-Agent' = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
}

$response = Invoke-WebRequest -Uri "https://example.com" `
    -Proxy $proxyUrl `
    -Headers $headers

Write-Output $response.StatusCode

Scrapeless 管理超过 9000 万个住宅 IP,覆盖 195 个国家,支持自动轮换和地理定位。

基于会话的代理配置

对于多个相关请求,会话对象自动维护代理设置:

powershell Copy
$proxyUrl = "http://username:password@superproxy.scrapeless.com:1337"
$webSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$webSession.Proxy = New-Object System.Net.WebProxy($proxyUrl)

# 第一个请求
$response1 = Invoke-WebRequest -Uri "https://example.com/page1" `
    -WebSession $webSession

# 第二个请求自动使用相同的代理
$response2 = Invoke-WebRequest -Uri "https://example.com/page2" `
    -WebSession $webSession

# 第三个请求使用相同的代理设置
$response3 = Invoke-WebRequest -Uri "https://example.com/page3" `
    -WebSession $webSession

会话对象消除了对顺序请求重复指定代理的需求。

代理绕过列表

某些环境要求直接连接到特定主机,绕过代理。定义绕过列表以实现直接访问:

powershell Copy
$proxy = New-Object System.Net.WebProxy("http://proxy.example.com:8080")
$proxy.BypassList += "*.internal.company.com"
$proxy.BypassList += "localhost"

[System.Net.WebRequest]::DefaultWebProxy = $proxy

$response = Invoke-WebRequest -Uri "https://internal.company.com"

绕过的主机直接连接,而不是通过代理服务器路由。

处理 SSL 证书错误

某些代理配置或自签名证书会导致 SSL 错误。出于测试目的,可以跳过证书验证:

powershell Copy
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

$response = Invoke-WebRequest -Uri "https://example.com" `
    -Proxy "http://proxy.example.com:8080"

警告:禁用证书验证可能会使您面临中间人攻击的风险。仅在受控环境中使用。

PowerShell 7.4 及以上版本提供更安全的 -SkipCertificateCheck 参数:

powershell Copy
$response = Invoke-WebRequest -Uri "https://example.com" `
    -Proxy "http://proxy.example.com:8080" `
    -SkipCertificateCheck

高级用例:使用代理进行网络抓取

结合住宅代理、适当的头和延迟以进行有效的网络抓取:

powershell Copy
function Invoke-ScrapingRequest {
    param(
        [Parameter(Mandatory=$true)]
        [string]$Url,
        
        [Parameter(Mandatory=$true)]
        [string]$ProxyUrl,
        
        [int]$DelayMs = 2000
    )
    
    $headers = @{
        'User-Agent' = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
        'Accept' = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
        'Accept-Language' = 'en-US,en;q=0.9'
    }
    
    Start-Sleep -Milliseconds $DelayMs
    
    $response = Invoke-WebRequest -Uri $Url `
        -Proxy $ProxyUrl `
        -Headers $headers `
        -ErrorAction SilentlyContinue
    
    return $response
}

# 用法
$proxyUrl = "http://user:pass@superproxy.scrapeless.com:1337"
$response = Invoke-ScrapingRequest -Url "https://example.com" -ProxyUrl $proxyUrl

这种模式在调用之间实现了请求延迟,头部模拟合法浏览器,以及住宅代理轮换,以实现可靠的大规模抓取。

解决代理问题

连接被拒绝:验证代理主机和端口。直接测试连接性:

powershell Copy
Test-NetConnection -ComputerName "proxy.example.com" -Port 8080

认证失败:请确认用户名和密码正确。如果 URL 中包含特殊字符,嵌入的凭据需要 URL 编码。

性能慢:如果可用,请尝试不同的代理。一些代理可能地理位置偏远或者拥堵。

未使用代理:验证 Invoke-WebRequest 是否使用了 -Proxy 参数。环境变量或系统代理设置不会自动影响 Invoke-WebRequest。

环境变量代理

PowerShell 7.x+ 支持设置的 HTTP_PROXY 和 HTTPS_PROXY 环境变量:

powershell Copy
$env:HTTP_PROXY = "http://proxy.example.com:8080"
$env:HTTPS_PROXY = "http://proxy.example.com:8080"

$response = Invoke-WebRequest -Uri "https://example.com"

早期版本的 PowerShell 会忽略环境变量—请明确指定 -Proxy 参数。

通过代理进行的 REST API 调用

类似的 Invoke-RestMethod cmdlet 也接受用于 JSON API 调用的 -Proxy 参数:

powershell Copy
$proxyUrl = "http://username:password@superproxy.scrapeless.com:1337"

$response = Invoke-RestMethod -Uri "https://api.example.com/data" `
    -Proxy $proxyUrl `
    -Method Get

$response | ConvertTo-Json

常见问题

问:我可以在不指定代理的情况下使用 Invoke-WebRequest 吗?

答:可以。省略 -Proxy 参数以进行直接连接。但是,这会暴露您的实际 IP 地址,并防止绕过地理限制或速率限制。

问:我可以通过代理同时发起多少个 Invoke-WebRequest 调用?

答:PowerShell 根据系统资源限制并发操作。大多数脚本成功运行 10-50 个并行请求。像 Scrapeless 这样的高级住宅代理在代理基础设施级别支持无限并发。

问:-Proxy 和 -ProxyCredential 参数有什么区别?

答:-Proxy 参数指定代理服务器地址。-ProxyCredential 参数单独提供身份验证凭据,适用于凭据包含特殊字符的情况,这些字符需要 URL 编码。

问:我需要提升权限才能在 Invoke-WebRequest 中使用代理吗?

答:不需要。使用代理只需与代理服务器的网络连接。标准代理操作不需要管理员权限。

问:Invoke-WebRequest 能在 Windows PowerShell 5.1 中与 SOCKS 代理一起使用吗?

答:不能。Windows PowerShell 5.1 仅支持 HTTP 代理。升级到 PowerShell 7.x 以访问 SOCKS 代理支持。从官方 Microsoft 存储库下载 PowerShell 7,它与 Windows PowerShell 一起安装而不发生冲突。

在Scrapeless,我们仅访问公开可用的数据,并严格遵循适用的法律、法规和网站隐私政策。本博客中的内容仅供演示之用,不涉及任何非法或侵权活动。我们对使用本博客或第三方链接中的信息不做任何保证,并免除所有责任。在进行任何抓取活动之前,请咨询您的法律顾问,并审查目标网站的服务条款或获取必要的许可。

最受欢迎的文章

目录