-8

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.

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.

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
Shadow
  • 33,525
  • 10
  • 51
  • 64

1 Answers1

0

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
FaneDuru
  • 38,298
  • 4
  • 19
  • 27
  • SetCredentials will be a problem because it requires username and password entered in clear text, which is not allowed by company security policy. I'll try the ignore ssl error flag. – Shadow May 21 '20 at 12:09
  • Ups... Then, try replacing of the the line setting credentials with `w.SetAutoLogonPolicy 0`. I am not connected through a proxy to test it... I would also try `w.SetAutoLogonPolicy AutoLogonPolicy_OnlyIfBypassProxy`... Having a correct reference, it should use the appropriate constant value, I think. – FaneDuru May 21 '20 at 13:42
  • @Shadow: Did you find time to check if `SetAutoLogonPolicy` changed something in the code behavior? – FaneDuru May 22 '20 at 09:16
  • Unfortunately, neither the ssl ignore errors, nor the change in the authentication helped. However, what I did observe during the test, that for some URLs https works, for some it does not. Maybe something to do with ssl / tls level required, will check. – Shadow May 22 '20 at 09:34
  • I would try playing with `w.Option(` and choose from options offered by intellisense. You maybe will find something able to solve your problem. Good luck! I will delete my answer after you confirm that read my suggestion... – FaneDuru May 22 '20 at 09:54