【问题标题】:VBA post request error - multipart/form-dataVBA 发布请求错误 - 多部分/表单数据
【发布时间】:2020-01-18 11:57:18
【问题描述】:

我正在尝试在网站上发出 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!

我还尝试了在此链接上找到的内容,但仍然无法正常工作(或者我没有在 VBA 中很好地实现它) https://wqweto.wordpress.com/2011/07/12/vb6-using-wininet-to-post-binary-file/

【问题讨论】:

    标签: excel vba http xmlhttprequest


    【解决方案1】:

    我设法完成了这项工作,使用了下面链接中的概念(边界也存在问题)。 File updload in post form in VBS

    【讨论】:

      猜你喜欢
      • 2016-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-29
      • 2022-11-19
      • 1970-01-01
      • 2018-03-19
      • 2019-03-02
      相关资源
      最近更新 更多