以下代码有效.但是,如果我注释掉该行Dim objRequest As MSXML2.XMLHTTP
并取消注释该行,Dim objRequest As Object
则会失败并显示错误消息:
参数不正确
为什么,以及我可以做些什么(如果有的话)?
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,是的,我知道我必须这样做才能使我的代码在没有引用MSXML类型库的情况下工作.这不是问题所在.Dim objRequest As Object
无论我是否使用,代码在使用时都会失败
Set objRequest = NEW MSXML2.XMLHTTP
与参考,或
Set objRequest = CreateObject("MSXML2.XMLHTTP")
没有参考.
出于某种原因,这有效:
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
不建议strPostData
通过字符串连接构建URL编码,强烈建议使用URL编码功能:
strPostData = "api_id=" & URLEncode(strApiId) & _ "&user=" & URLEncode(strUserName) & _ "&password=" & URLEncode(strPassword)
URLEncode()
VBA中的一个函数的几个选择是在这个线程中:如何在Excel VBA中对字符串进行URL编码?