簡體   English   中英

VBA發布請求錯誤-多部分/表單數據

[英]VBA post request error - multipart/form-data

我正在嘗試在網站上發出POST請求,以上傳Excel文件。 當我手動執行此操作(手動瀏覽文件輸入html元素中的文件)並捕獲網絡流量時,fiddler上將顯示以下消息:

Host: electool.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0
Accept: */*
Accept-Language: es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate, br
X-Requested-With: XMLHttpRequest
Content-Type: multipart/form-data; boundary=---------------------------8432274816859
Content-Length: 35577
DNT: 1
Connection: keep-alive
Referer: https://electool.com/sourcingtool/pageflowViews/tender_view/action.html?pageflow=tender_view:-2
Cookie: JSESSIONID=87C7F4550EFBDF7A8D6F94C2D021CBB5; electool_sso=5A7A319313EA0EF4

-----------------------------8432274816859
Content-Disposition: form-data; name="qId"

1
-----------------------------8432274816859
Content-Disposition: form-data; name="attachment_1"; filename="A_aFRR negatív_QWERTY00_W38.xlsx"
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
..."

我想對excel vba進行同樣的操作(選擇文件並發布相同的消息,以便上傳excel文件)

我現在擁有的代碼:

    For i = 1 To ie.Manage.Cookies.Count
     myCookie = myCookie & ie.Manage.Cookies.Item(i).Name & "=" & ie.Manage.Cookies.Item(i).Value & "; "
    Next
    myCookie = Left(myCookie, Len(myCookie) - 2)
    Dim strBoundary As String
    strBoundary = "-----------------------------501911906621"

    myURL2 = "https://electool.com/sourcingtool/participant/ajaxSaveAttachment.htm"
    sPayLoad = strBoundary & vbNewLine & _
      "Content-Disposition: form-data; name=""qId""" & vbNewLine & vbNewLine & _
      "1" & vbNewLine & strBoundary & vbNewLine & _
      "Content-Disposition: form-data; name=""attachment_1"";filename=""E_W_37_negativ.xlsx""" & _
      vbNewLine & "Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" & _
      vbNewLine & vbNewLine

    sPayLoad = sPayLoad & GetFile(myFile) & strBoundary

    With CreateObject("Msxml2.ServerXMLHTTP.6.0")
        .Open "POST", myURL2, False
        .setRequestHeader "Content-Type", "multipart/form-data; boundary=" & strBoundary
        .setRequestHeader "Content-Length", LenB(sPayLoad)
        .setRequestHeader "Host", "electool.com"
        .setRequestHeader "Cookie", myCookie
        .setRequestHeader "Referer", ie.url
        .send (sPayLoad)
        Debug.Print .responseText
    End With
End Sub

Function GetFile(ByVal FileName As String) As String
    Dim FileContents() As Byte, FileNumber As Integer
    ReDim FileContents(FileLen(FileName) - 1)
    FileNumber = FreeFile()
    Open FileName For Binary As FileNumber
    Get FileNumber, , FileContents
    Close (FileNumber)
    GetFile = StrConv(FileContents, vbUnicode)
End Function

我得到的響應是: ng failed; nested exception is com.electool.sourcing.framework.util.request.exception.RequestParameterNotPresentException: Not found parameter: [qId] in request! ng failed; nested exception is com.electool.sourcing.framework.util.request.exception.RequestParameterNotPresentException: Not found parameter: [qId] in request!

我也嘗試了在此鏈接上找到的內容,但是仍然無法正常工作(或者我在VBA中無法很好地實現它) https://wqweto.wordpress.com/2011/07/12/vb6-using-wininet-to-后二進制文件/

我設法通過下面鏈接中的概念來完成這項工作(邊界也存在問題)。 在VBS中以帖子形式上傳文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM