Monday, December 12, 2005

Post with VBScript Using NTLM, HTTPS

There are many ways to post data to a web site using VBScript. It becomes harder when you must use HTTPS. It gets downright silly when the script must use integrated authentication to the web server as the user running it.

Here are all my attempts to do just that, listing the last one that finally succeeded:

Function post(sText)
  dim http

  'This line sets the client XML object.
  'This method appears to require hard-coded credentials, or it will revert to anonymous.
  'It also needs the host and certificate names to match when using HTTPS.
  ' set http = Createobject("MSXML2.XMLHTTP")

  'This line sets the server XML object.
  'This method appears to require hard-coded credentials, or it will revert to anonymous.
  'It will do HTTPS if the host and certificate names don't match.
  ' set http = Createobject("MSXML2.ServerXMLHTTP")

  'These lines set the server XML object, version 4.
  'This method will pass NTLM credentials when the proxy configuration is set.
  'It also will do HTTPS if the host and certificate names don't match.
  'But most clients don't have MSXML4.
  ' set http = Createobject("MSXML2.ServerXMLHTTP.4.0")
  ' http.setProxy 2, "gkproxy", "gkweb"

  'These lines set the WinHTTPRequest object.
  'This method will pass NTLM credentials when the proxy configuration is set.
  'It also will do HTTPS if the host and certificate names don't match.
  'And most clients support it!
  set http = CreateObject("WinHttp.WinHttpRequest.5.1")
  http.SetAutoLogonPolicy 0

  http.Open "POST", "https://host/default.spx", false
  http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  http.send sText
  post = http.responseText
  Set http = nothing
End Function

No comments: