用下述代码,生成参数,发送请求头后,返回:{"code":501,"msg":"Required MultipartFile parameter 'file' is not present"},求指导。
模拟提报代码:
sub Main()
Dim FileName As String
FileName = "C:\Users\danl.txt"
With CreateObject("winhttp.winhttprequest.5.1")
.Open "POST", "https://datasource/localConnection/uploadFile", False
.setRequestHeader "Content-Type", "multipart/form-data; boundary=---------------------------1997060815188" '要与下面SendBody里的一致
.setRequestHeader "Accept-Encoding", "gzip, deflate, br"
.setRequestHeader "Cookie", MyCookie
.setRequestHeader "Connection", "Keep-Alive"
.send SendBody(FileName) '发送字串
Debug.Print .responseText '可以登录论坛到发帖页面查看是否有“未使用附件”
End With
end sub
参数生成代码:
Function SendBody(ByVal Fn As String)
Dim MyStr As String
Dim b() As Byte
Dim StrByte() As Byte
Dim sLen As Long
MyStr = "-----------------------------1997060815188" '系统产生的随机数字 '头部
MyStr = MyStr & vbCrLf & "Content-Disposition: form-data; name=""config""" & vbCrLf & vbCrLf & "%7B%22fileType%22%3A0%2C%22delimiter%22%3A0%7D"
MyStr = MyStr & vbCrLf & "-----------------------------1997060815188" '系统产生的随机数字 '头部
MyStr = MyStr & vbCrLf & "Content-Disposition: form-data; name=""file""; filename=""danl.csv"""
MyStr = MyStr & vbCrLf & "Content-Type: application/vnd.ms-excel" & vbCrLf
MyStr = MyStr & vbCrLf
' MsgBox MyStr
StrByte = StrConv(MyStr, vbFromUnicode128) '将之前的字符串转为byte
b = FileToByte(Fn) '将要上传的文件转为byte
' 合并字符串及文件成一个byte
Debug.Print b
sLen = UBound(StrByte)
ReDim Preserve StrByte(sLen + UBound(b) - 1)
For i = 2 To UBound(b)
StrByte(sLen + i - 1) = b(i)
Next
MyStr = vbCrLf & "-----------------------------1997060815188--" '结束多2杆
MyStr = MyStr & vbCrLf
' MsgBox MyStr
b = StrConv(MyStr, vbFromUnicode128)
' Debug.Print b
sLen = UBound(StrByte)
ReDim Preserve StrByte(sLen + UBound(b) + 1)
For i = 0 To UBound(b)
StrByte(sLen + i + 1) = b(i)
Next
SendBody = StrByte
Debug.Print SendBody
End Function
Function FileToByte(ByVal Fn As String)
With CreateObject("adodb.stream")
.Open
.LoadFromFile Fn
.Charset = "GB2312"
.Type = 1
FileToByte = .Read
.Close
End With
End Function