vba MSXML2.XMLHTTP 发送方法适用于早期绑定,晚期绑定失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/308826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:29:36  来源:igfitidea点击:

MSXML2.XMLHTTP send method works with early binding, fails with late binding

httpvbamsxmlxmlhttprequest

提问by Brendan Reynolds

The code below works. But if I comment out the line Dim objRequest As MSXML2.XMLHTTPand uncomment the line Dim objRequest As Objectit fails with the error message :

下面的代码有效。但是,如果我注释掉该行Dim objRequest As MSXML2.XMLHTTP并取消注释该行,Dim objRequest As Object它会失败并显示错误消息:

The parameter is incorrect

参数不正确

Why, and what (if anything) can I do about it?

为什么,我能做些什么(如果有的话)?

Public Function GetSessionId(strApiId, strUserName, strPassword) As String

    Dim strPostData As String

    Dim objRequest As MSXML2.XMLHTTP
    'Dim objRequest As Object '

    strPostData = "api_id=" & strApiId & "&user=" & strUserName & "&password=" & strPassword

    Set objRequest = New MSXML2.XMLHTTP
    With objRequest
        .Open "POST", "https://api.clickatell.com/http/auth", False
        .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        .send strPostData
        GetSessionId = .responseText
    End With

End Function


Corey, yes, I know I would have to do that in order for my code to work without a reference to the MSXML type library. That's not the issue here. The code fails when using Dim objRequest As Objectregardless of whether I use

Corey,是的,我知道我必须这样做才能使我的代码在不引用 MSXML 类型库的情况下工作。这不是这里的问题。Dim objRequest As Object无论我是否使用,代码都在使用时失败

Set objRequest = NEW MSXML2.XMLHTTPwith the reference, or

Set objRequest = NEW MSXML2.XMLHTTP与参考,或

Set objRequest = CreateObject("MSXML2.XMLHTTP")without the reference.

Set objRequest = CreateObject("MSXML2.XMLHTTP")没有参考。

回答by Tomalak

For some reason, this works:

出于某种原因,这有效:

Dim strPostData As String
Dim objRequest As Object

strPostData = "api_id=" & strApiId & "&user=" & strUserName & "&password=" & strPassword

Set objRequest = New MSXML2.XMLHTTP
With objRequest
  .Open "POST", "https://api.clickatell.com/http/auth", False
  .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  .send (strPostData)
   GetSessionId = .responseText
End With

Instead of building the URL-encoded strPostDatavia string concatenation, it's stronglyadvisable to use a URL encoding function:

强烈建议使用 URL 编码函数strPostData,而不是通过字符串连接构建URL 编码:

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?

URLEncode()此线程中有几个VBA 函数的选择:如何在 Excel VBA 中对字符串进行 URL 编码?

回答by Corey Trager

If you use the Dim objRequest As Object then you would need to code:
Set objRequest = CreateObject("MSXML2.XMLHTTP")

如果您使用 Dim objRequest As Object 那么您需要编码:
Set objRequest = CreateObject("MSXML2.XMLHTTP")

回答by Alistair Collins

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:

我意识到这与上面 Tomalek 的代码几乎相同(所有功劳都归功于你!),但是这个问题帮助我找到了我遇到的问题的完整解决方案(Excel 提交到 PHP 服务器,然后处理响应)...所以如果这对其他人有任何帮助:

Sub Button1_Click2()

Dim objXMLSendDoc As Object
Set objXMLSendDoc = New MSXML2.DOMDocument
objXMLSendDoc.async = False
Dim myxml As String
myxml = "<?xml version='1.0'?><Request>Do Something</Request>"
If Not objXMLSendDoc.LoadXML(myxml) Then
    Err.Raise objXMLSendDoc.parseError.ErrorCode, , objXMLSendDoc.parseError.reason
End If

Dim objRequest As MSXML2.XMLHTTP
Set objRequest = New MSXML2.XMLHTTP
With objRequest
    .Open "POST", "http://localhost/SISADraftCalcs/Test2.php", False
    .setRequestHeader "Content-Type", "application/xml;charset=UTF-16"
    .setRequestHeader "Cache-Control", "no-cache"
    .send objXMLSendDoc
End With

Dim objXMLDoc As MSXML2.DOMDocument
Set objXMLDoc = objRequest.responseXML
If objXMLDoc.XML = "" Then
    objXMLDoc.LoadXML objRequest.responseText
    If objXMLDoc.parseError.ErrorCode <> 0 Then
        MsgBox objXMLDoc.parseError.reason
    End If
End If

Dim rootNode As IXMLDOMElement
Set rootNode = objXMLDoc.DocumentElement

MsgBox rootNode.SelectNodes("text").Item(0).text

End Sub