我有一个VBScript文件,它向URL发出GET请求:
Dim o
Set o = CreateObject("MSXML2.XMLHTTP")
o.open "GET", "http://www...", False
o.send
我是VBScript的新手。在C#中,我将在Web服务对象上使用CredentialCache.DefaultCredentials
来传递凭据以及此请求。我怎样才能在VBScript中做同样的事情?
谢谢!
答案 0 :(得分:1)
据我所知,MSXML2不支持此功能。您可以尝试使用以下方法自动化Internet Explorer:
CreateObject("InternetExplorer.Application")
我知道IE可以通过集成身份验证传递您的凭据。然而,使用IE和VBScript往往以非常不一致的结果结束,所以我建议坚持使用C#,如果这是一个要求,你可以选择。
答案 1 :(得分:1)
看起来你需要的是setRequestHeader()。 http://msdn.microsoft.com/en-us/library/ms536752(v=vs.85).aspx
我使用以下代码:
dim xmlhttp : set xmlhttp = createobject("microsoft.xmlhttp")
xmlhttp.open "post", url, false
xmlhttp.setRequestHeader "Authorization", "Basic " & passcode
xmlhttp.setRequestHeader "Content-Type","application/x-www-form-urlencoded"
xmlhttp.send(postdata)
密码是base64编码的用户名:密码
我使用以下base64函数
Function Base64Encode(inData)
'rfc1521
'2001 Antonin Foller, Motobit Software, http://Motobit.cz
Const Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Dim cOut, sOut, I
'For each group of 3 bytes
For I = 1 To Len(inData) Step 3
Dim nGroup, pOut, sGroup
'Create one long from this 3 bytes.
nGroup = &H10000 * Asc(Mid(inData, I, 1)) + _
&H100 * MyASC(Mid(inData, I + 1, 1)) + MyASC(Mid(inData, I + 2, 1))
'Oct splits the long To 8 groups with 3 bits
nGroup = Oct(nGroup)
'Add leading zeros
nGroup = String(8 - Len(nGroup), "0") & nGroup
'Convert To base64
pOut = Mid(Base64, CLng("&o" & Mid(nGroup, 1, 2)) + 1, 1) + _
Mid(Base64, CLng("&o" & Mid(nGroup, 3, 2)) + 1, 1) + _
Mid(Base64, CLng("&o" & Mid(nGroup, 5, 2)) + 1, 1) + _
Mid(Base64, CLng("&o" & Mid(nGroup, 7, 2)) + 1, 1)
'Add the part To OutPut string
sOut = sOut + pOut
'Add a new line For Each 76 chars In dest (76*3/4 = 57)
'If (I + 2) Mod 57 = 0 Then sOut = sOut + vbCrLf
Next
Select Case Len(inData) Mod 3
Case 1: '8 bit final
sOut = Left(sOut, Len(sOut) - 2) + "=="
Case 2: '16 bit final
sOut = Left(sOut, Len(sOut) - 1) + "="
End Select
Base64Encode = sOut
End Function
Function MyASC(OneChar)
If OneChar = "" Then MyASC = 0 Else MyASC = Asc(OneChar)
End Function