需要具有WinHttpRequest的代理身份验证(http错误407)和具有集成Windows身份验证和https的代理

需要具有WinHttpRequest的代理身份验证(http错误407)和具有集成Windows身份验证和https的代理

问题描述:

我正在尝试使用VBA中的 WinHttpRequest 从我的公司网络访问外部网站.该公司的代理服务器需要集成的Windows身份验证.

I'm trying to access an external website from my company network using WinHttpRequest from VBA. The company has a proxy server that requires integrated Windows authentication.

如果我尝试访问带有纯http的URL,以下代码将起作用,但是如果我尝试访问带有https的URL,则会获得http状态代码 407-需要代理身份验证.

The following code works if I try to access a URL with plain http, but I get http status code 407 - proxy authentication required if I try to access a URL with https.

我需要做什么才能使代码与https一起使用?

What do I need to do to make the code work with https?

Sub a()
    Dim w As New WinHttp.WinHttpRequest

    'set proxy        
    w.SetProxy 2, "myproxy:8080"
    'use integrated windows authentication
    w.SetAutoLogonPolicy AutoLogonPolicy_Always
    w.Option(WinHttpRequestOption_EnableRedirects) = True
    w.Open "GET", "https://..."
    w.Send
    Debug.Print w.Status ' Status = 407
    Debug.Print w.Option(WinHttpRequestOption_URL)
End Sub

尝试,请插入一行(在 Open Send 之间)用于设置凭据的行,另一行用于设置凭据.忽略(所有)SSL错误.尝试一些卑鄙的事情:

Try, please insert (between Open and Send) a line for setting credentials and another one for ignoring (all) SSL errors. Try something lile that:

Sub AccessSiteThroughProxy()
    Dim w As New WinHttp.WinHttpRequest

    'set proxy
    w.setProxy 2, "myproxy:8080"
    'use integrated windows authentication
    w.SetAutoLogonPolicy AutoLogonPolicy_Always
    w.Option(WinHttpRequestOption_EnableRedirects) = True
    w.Open "GET", "https://..."

    'try inserting a line to set credentials:________________________________________
     w.SetCredentials UserName, Password, HTTPREQUEST_SETCREDENTIALS_FOR_SERVER ' try Const HTTPREQUEST_SETCREDENTIALS_FOR_SERVER = 0
     'in order to ignore SSL erors, try:
     w.Option(WinHttpRequestOption_SslErrorIgnoreFlags) = &H3300
    '_________________________________________________________________________________

    w.send
    Debug.Print w.Status ' Status = 407
    Debug.Print w.Option(WinHttpRequestOption_URL)
End Sub