2012-03-14 63 views
0

的DefaultCredentials我有一個VBScript文件,這使得GET請求的URL:相當於在VBScript

Dim o 
Set o = CreateObject("MSXML2.XMLHTTP") 
o.open "GET", "http://www...", False 
o.send 

我是新來的VBScript。在C#中,我將使用Web服務對象上的CredentialCache.DefaultCredentials來傳遞憑據以及此請求。我怎樣才能在VBScript中做到這一點?

謝謝!

回答

1

據我所知,MSXML2不支持這一點。您可以嘗試使用Internet Explorer自動化:

CreateObject("InternetExplorer.Application") 

我知道IE可以通過集成身份驗證來傳遞您的憑據。然而,使用IE和VBScript往往會以非常不一致的結果結束,所以我建議堅持使用C#,如果這是需求並且您有選擇的話。

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 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/" 
    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