MSXML2.XMLHTTP发送方法与早期绑定一起使用,后期绑定失败 [英] MSXML2.XMLHTTP send method works with early binding, fails with late binding

查看:549
本文介绍了MSXML2.XMLHTTP发送方法与早期绑定一起使用,后期绑定失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码有效。但是如果我注释掉行 Dim objRequest As MSXML2.XMLHTTP 并取消注释行 Dim objRequest As Object 它失败了错误消息:

The code below works. But if I comment out the line Dim objRequest As MSXML2.XMLHTTP and uncomment the line Dim objRequest As Object it fails with the error message :


参数不正确

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,是的,我知道我必须这样做才能让我的代码在没有引用MSXML类型库的情况下工作。这不是问题所在。使用 Dim objRequest As Object 时代码失败,无论我是否使用


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 Object regardless of whether I use

设置objRequest =带有引用的新MSXML2.XMLHTTP ,或

设置objRequest = CreateObject(MSXML2.XMLHTTP)没有参考。

推荐答案

出于某种原因,这有效:

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

不是通过字符串连接构建URL编码的 strPostData ,而是强烈建议使用网址编码功能:

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)

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

这篇关于MSXML2.XMLHTTP发送方法与早期绑定一起使用,后期绑定失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆