MSXML2.XMLHTTP send method works with early binding, fails with late binding
For some reason, this works:
Dim strPostData As StringDim objRequest As ObjectstrPostData = "api_id=" & strApiId & "&user=" & strUserName & "&password=" & strPasswordSet objRequest = New MSXML2.XMLHTTPWith objRequest .Open "POST", "https://api.clickatell.com/http/auth", False .setRequestHeader "Content-Type", "application/x-www-form-urlencoded" .send (strPostData) GetSessionId = .responseTextEnd With
Instead of building the URL-encoded strPostData
via string concatenation, it's strongly advisable to use a URL encoding function:
strPostData = "api_id=" & URLEncode(strApiId) & _ "&user=" & URLEncode(strUserName) & _ "&password=" & URLEncode(strPassword)
A couple of choices for a URLEncode()
function in VBA are in this thread: How can I URL encode a string in Excel VBA?
If you use the Dim objRequest As Object then you would need to code:
Set objRequest = CreateObject("MSXML2.XMLHTTP")
I realise this is nearly identical to the code from Tomalek above (all credit due to you!), but this question helped me towards a full solution to a problem I had (Excel submitting to PHP server, then dealing with response)...so in case this is of any help to anyone else:
Sub Button1_Click2()Dim objXMLSendDoc As ObjectSet objXMLSendDoc = New MSXML2.DOMDocumentobjXMLSendDoc.async = FalseDim myxml As Stringmyxml = "<?xml version='1.0'?><Request>Do Something</Request>"If Not objXMLSendDoc.LoadXML(myxml) Then Err.Raise objXMLSendDoc.parseError.ErrorCode, , objXMLSendDoc.parseError.reasonEnd IfDim objRequest As MSXML2.XMLHTTPSet objRequest = New MSXML2.XMLHTTPWith objRequest .Open "POST", "http://localhost/SISADraftCalcs/Test2.php", False .setRequestHeader "Content-Type", "application/xml;charset=UTF-16" .setRequestHeader "Cache-Control", "no-cache" .send objXMLSendDocEnd WithDim objXMLDoc As MSXML2.DOMDocumentSet objXMLDoc = objRequest.responseXMLIf objXMLDoc.XML = "" Then objXMLDoc.LoadXML objRequest.responseText If objXMLDoc.parseError.ErrorCode <> 0 Then MsgBox objXMLDoc.parseError.reason End IfEnd IfDim rootNode As IXMLDOMElementSet rootNode = objXMLDoc.DocumentElementMsgBox rootNode.SelectNodes("text").Item(0).textEnd Sub