default credentials in VBA

2019-06-07 01:47发布

I'm tryng to make a HTTP request from VB for Applications ( VBA) that should have been simple:

URL = "https://www.google.com/"
Set xhr = CreateObject("MSXML2.ServerXMLHTTP.6.0")
xhr.Open "GET", URL, False
xhr.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xhr.Send
MsgBox xhr.responseText

BUT I'm behind a proxy server which require basic auth (base64).

How to set the credentials in VBA ?

(something equivalent to : myHttpWebRequest.Credentials = CredentialCache.DefaultCredentials)

If this is not possible, how to solve the issue with another approach ?

Thanks !

1条回答
该账号已被封号
2楼-- · 2019-06-07 02:00

You'd need the setProxyCredentials method. I don't have a proxy to test, but this should do the trick:

URL = "https://www.google.com/"
Set xhr = CreateObject("MSXML2.ServerXMLHTTP.6.0")
xhr.Open "GET", URL, False
xhr.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xhr.setProxy 2, "192.168.0.222:8080"
xhr.setProxyCredentials "your_username" , "password_for_username"
xhr.Send
MsgBox xhr.responseText

Taken from This Question which points out the 2 argument is a version number that you may need to change.

查看更多
登录 后发表回答