HTTP 请求的 VBA 代理自动配置

Ale*_*man 2 excel vba

我需要在 VBA 中为 Web api 构建一个客户端,并且它需要在代理后面工作(带有身份验证)。我一直在研究 WinHttp.WinHttpRequest 和 MSXML2.XMLHTTP/ServerXMLHTTP 类。事实证明:

  • XMLHTTP 自动检测通过proxy.pac提供的代理设置(好)
  • WinHttpRequest 没有(不好)

然而,另一方面:

  • XMLHTTP 自动遵循重定向,而且没有办法此行为(不好)
  • WinHttpRequest 没有(好)

既然我希望能够鱼与熊掌兼得,有没有办法为像 WinHttpRequest 这样不盲目遵循重定向的组件获取自动代理配置?

Jeh*_*man 5

VBA-Web 项目可能会帮助您解决糕点食用问题。

https://github.com/VBA-tools/VBA-Web

我想你想做的事情会是这样的:

Dim client As New WebClient
With client
    .BaseUrl = "https://www.google.com"
    .ProxyUsername = <user>
    .ProxyPassword = <password>
    .EnableAutoProxy = True
End With

Dim request As New WebRequest
With request
    .Method = WebMethod.HttpGet
    .Format = WebFormat.PlainText
End With

Dim response As WebResponse
Set response = client.Execute(request)
Run Code Online (Sandbox Code Playgroud)