在VBS

时间:2016-05-11 09:29:50

标签: post curl vbscript zip httprequest

我已经把头发拉过这个头发几天了。我正在寻找一个zip文件的cURL HTTP Post的等效VBS实现。经过大量的谷歌搜索,尝试不同的HTTP对象,搞乱请求标题,以及大量的实验,我仍然无法让它正常工作。

CURL实现如下:

curl "https://www.mycompany.com/myspace/api/admin/khub/sources/mysource/upload?mode=INCR" -H "FT-Authorization: Basic MyAuthorizationKey==" -F 'file1=@my_archive.zip'

我到目前为止最远的(使用VBS)是让服务器返回代码400(错误请求)给我,而responseText是“你一次只能上传一个文件”(我假设这是来自服务器端的自定义消息)。

我认为ZIP文件需要是base64编码(我做过),而不是通过文件名直接传递给请求。任何帮助将不胜感激!

Dim objFileSys
Dim http
Dim inByteArray
Dim base64Encoded
Dim inFile
Dim strData

Set objFileSys = CreateObject("Scripting.FileSystemObject") 
Set http = CreateObject("Microsoft.XmlHttp")

Const MULTIPART_BOUNDARY = "------------------B0und4Ry---"
Const objAuthKey = "MyAuthorizationKey==" 

' Check input file exists
inFile = "d:\my_archive.zip"

http.onreadystatechange = getRef("HTTPCallback")

inByteArray = readBytes(inFile)
base64Encoded = encodeBase64(inByteArray)
strData = strData & MULTIPART_BOUNDARY & vbCrLf
strData = strData & "Content-Disposition: form-data; name=""file1"";" & vbCrLf & vbCrLf
strData = strData & base64Encoded
strData = strData & vbCrLf
strData = strData & MULTIPART_BOUNDARY

Call http.open("POST","https://www.mycompany.com/myspace/api/admin/khub/sources/mysource/upload?mode=INCR",False) 
Call http.setRequestHeader("FT-Authorization","Basic " & objAuthKey)
Call http.setRequestHeader("Content-Type","multipart/form-data;boundary=" & MULTIPART_BOUNDARY)
Call http.SetRequestHeader("Content-Length", Len(strData))
Call http.send(strData)


'Following method collects the response from  Web API
Sub HTTPCallback
    If(http.readyState = 4) Then
        Dim objFSO, outFile
        Set objFSO=CreateObject("Scripting.FileSystemObject")

        outFile="d:\response.txt"
        Set objFile = objFSO.CreateTextFile(outFile,True)
        objFile.Write http.responseText & vbCrLf
        objFile.Close

        MsgBox(http.status & ": " & http.responseText)
    End If
End Sub

Private function readBytes(file)
  dim inStream
  ' ADODB stream object used
  set inStream = WScript.CreateObject("ADODB.Stream")
  ' open with no arguments makes the stream an empty container 
  inStream.Open
  inStream.type= 1
  inStream.LoadFromFile(file)
  readBytes = inStream.Read()
end function

Private function encodeBase64(bytes)
  dim DM, EL
  Set DM = CreateObject("Microsoft.XMLDOM")
  ' Create temporary node with Base64 data type
  Set EL = DM.createElement("tmp")
  EL.DataType = "bin.base64"
  ' Set bytes, get encoded String
  EL.NodeTypedValue = bytes
  encodeBase64 = EL.Text
end function

0 个答案:

没有答案