简体   繁体   English

VBA发布请求错误-多部分/表单数据

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

I am trying to make a POST request on a website in order to upload an Excel file. 我正在尝试在网站上发出POST请求,以上传Excel文件。 When I do it manually (manually browsing a file in a file input html element) and capture the network traffic, the following message appears on fiddler: 当我手动执行此操作(手动浏览文件输入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
..."

I'd like to do the same with excel vba (selecting the file and posting the same message in order to upload the excel file) 我想对excel vba进行同样的操作(选择文件并发布相同的消息,以便上传excel文件)

The code I have now: 我现在拥有的代码:

    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

The response I get is this: 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! ng failed; nested exception is com.electool.sourcing.framework.util.request.exception.RequestParameterNotPresentException: Not found parameter: [qId] in request!

I also tried what I have found on this link, but still not working (or I am not implementing it well in VBA) https://wqweto.wordpress.com/2011/07/12/vb6-using-wininet-to-post-binary-file/ 我也尝试了在此链接上找到的内容,但是仍然无法正常工作(或者我在VBA中无法很好地实现它) https://wqweto.wordpress.com/2011/07/12/vb6-using-wininet-to-后二进制文件/

I managed to make this work, useig the concept from the link below (there were also issues with the boundaries). 我设法通过下面链接中的概念来完成这项工作(边界也存在问题)。 File updload in post form in VBS 在VBS中以帖子形式上传文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将多部分/表单数据发布到端点 - Post multipart/form-data to endpoint VBA POST请求发送包含上载文件的多部分表单数据 - VBA POST request sending multipart form data with uploading file Powershell:如何在多部分/表单数据请求中发布 excel (.xlsx) 文件? - Powershell: How can I POST excel (.xlsx) files in a multipart/form-data request? 如何使用 WinHTTPRequest 在 Excel 中使用 VBA 发送表单数据 POST 请求 - How to send a form-data POST request with VBA in Excel using WinHTTPRequest 如何使用Power Query的Web发布目录/表单数据 - How to POST a multipart/form-data using Power Query's Web.Contents 如何使用python中的POST请求在multipart/form-data中发送excel文件? - How to send excel file in multipart/form-data with POST Requests in python? 从 ASP.NET Core HttpContext.Request 的 multipart/form-data 内容读取 excel 文件? - Reading excel file from ASP.NET Core HttpContext.Request of multipart/form-data content? 为什么带有multipart / form-data的HttpPost的ActionResult在“ archivoExcel”参数中接收空值? - Why is my ActionResult with an HttpPost with multipart/form-data receiving a null value in the “archivoExcel” parameter? 使用vba将带有当前excel表的Post请求作为多部分表单数据发送 - Send a Post request with the current excel sheet as multi-part form data using vba Power Query,使用表单数据发出 http POST 请求 - Power Query, make http POST request with form data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM