VBA Proxy auto-configuration for http requests

2019-08-14 07:52发布

I need to build a client for a web api in VBA and it needs to work behind a proxy (with authentication). I've been looking at the WinHttp.WinHttpRequest and MSXML2.XMLHTTP/ServerXMLHTTP classes. It turns out that:

  • XMLHTTP automatically detects proxy settings provided through a proxy.pac file (good)
  • WinHttpRequest doesn't (bad)

However, on the other hand:

  • XMLHTTP automatically follows redirects, and there's no way of disabling this behaviour (bad)
  • WinHttpRequest doesn't (good)

Since I'd like to be able to have my cake and eat it, is there a way to get automatic proxy configuration for a component such as WinHttpRequest that doesn't follow redirects blindly?

1条回答
在下西门庆
2楼-- · 2019-08-14 08:51

The VBA-Web project might help you with your pastry eating problem.

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

I guess what you wish to do would go something like:

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)
查看更多
登录 后发表回答