Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA ServerXMLHTTP https request with self signed certificate

Tags:

I'm using a ServerXMLHTTP object to make some http requests on an excel 2007 vba script.

But I need to connect to an https server which uses a self-signed SSL Certificate, so by default I get the message "The certificate authority is invalid or incorrect". Is there a way to configure the ServerXMLHTTP object so that it doesn't requires a CA Certificate?

Some sample code follows:

Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")   objHTTP.Open "POST", "https://invernalia.homelinux.net", False, "user", "password"   objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"   objHTTP.send ("") 
like image 671
Javier Novoa C. Avatar asked Jul 20 '12 04:07

Javier Novoa C.


2 Answers

I used to have this problem for a while, and I only managed to get past it thanks to this:

Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP") objHTTP.SetOption 2, objHTTP.GetOption(2) objHTTP.Open "POST", "https://invernalia.homelinux.net", False, "user", "password" objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" objHTTP.send ("") 

I have found this here setOption Method and getOption Method

Here in this code I have just used SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS but you can try with others more specific:

  • SXH_SERVER_CERT_IGNORE_WRONG_USAGE
  • SXH_SERVER_CERT_IGNORE_CERT_CN_INVALID
  • SXH_SERVER_CERT_IGNORE_CERT_DATE_INVALID
  • SXH_SERVER_CERT_IGNORE_UNKNOWN_CA
like image 109
Romain Avatar answered Sep 28 '22 06:09

Romain


Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP") objHTTP.SetOption(2, objHTTP.GetOption(2) - SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS) objHTTP.Open "POST", "https://invernalia.homelinux.net", False, "user", "password" objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" objHTTP.send ("") 

Got the above solution to work with a minor change. Instead of:

objHTTP.SetOption(2, objHTTP.GetOption(2) - SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS) 

I used:

objHTTP.SetOption(2) = (objHTTP.GetOption(2) - SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS) 

Otherwise I get an error of:

Microsoft VBScript compilation error '800a0414' Cannot use parentheses when calling a Sub objHTTP.SetOption(2, objHTTP.GetOption(2) - SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS)

referenced from: SXH_OPTION_IGNORE_SERVER_SSL_CERT_ERROR_FLAGS

like image 42
Ian Flynn Avatar answered Sep 28 '22 08:09

Ian Flynn