zghwelcome 发表于 2018-4-2 22:30:22

【已解决】请问各位大神,如何模拟HTTP multipart/form-data 上传图片呢?

本帖最后由 zghwelcome 于 2018-4-3 13:46 编辑

折腾了一天,看了 multipart/form-data 表单的定义,服务器总数返回“数据请求失败”,抓包对比了不知道问题究竟出在哪,跪了。求大神支招!
谢谢 alaws 大佬指点,错误就是 二进制和字符串夹杂传输导致的。下面都改用二进制传输正常了!再次表示感谢!
解决的代码:$str = _UpFile(@ScriptDir & '\jzpz.jpg')
MsgBox(0,0,$str)




Func _UpFile($sJpgFile)
        Local $boundary = '----WebKitFormBoundaryAYqnZ87PoiVPmW05' ;//分隔字符串
       
        ;//读取图片文件
        $open = FileOpen($sJpgFile, 16)
        Local $sReadJpg = FileRead($open)
        FileClose($open)

       
        ;//构建图片数据包
        Local $sPostData =StringToBinary($boundary & @CRLF & _ ;//分界线
                        'Content-Disposition: form-data; name="upFile"; filename="jzpz.jpg"' & @CRLF & _
                        'Content-Type: image/jpeg' & @CRLF & @CRLF ,1)& _
                        $sReadJpg & StringToBinary( $boundary & @CRLF & _
                        'Content-Disposition: form-data; name="file_id"' & @CRLF & @CRLF & _
                        '0' & @CRLF & _
                       $boundary & '--' ,1)

        Local $oHTTP = ObjCreate("Microsoft.XMLHTTP")
        $oHTTP.Open('POST', 'http://*******/sysEntFile/BootstrapUploadFile.jhtml?moduleType=trouble&investigationType=1&url=/investigationImage/saveBootstrapInvestigationImage.jhtml&investigationId=0&imageType=1', True) ;// 异步
        $oHTTP.setRequestHeader("Host", '******')
        $oHTTP.setRequestHeader("Pragma", "no-cache")
        $oHTTP.setRequestHeader("Connection", "keep-alive")
        $oHTTP.setRequestHeader("Accept", "application/json, text/javascript, */*; q=0.01")
        $oHTTP.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko")
        $oHTTP.setRequestHeader("Referer", 'http://******/investigationAccount/uploadImage.jhtml?investigationId=0&imageType=1&investigationType=1')
        $oHTTP.setRequestHeader("Accept-Language", "zh-CN,zh;q=0.9")
        $oHTTP.setRequestHeader("X-Requested-With", "XMLHttpRequest")
        $oHTTP.setRequestHeader("Content-Type", "multipart/form-data; boundary=" & $boundary)
        $oHTTP.setRequestHeader("Origin", "http://*****")
        $oHTTP.Send($sPostData)
        While 1
                Sleep(50)
                $readystate = $oHTTP.readystate
                If $readystate = 4 Then ExitLoop
        WEnd
        $Content = $oHTTP.responseBody
        $header = $oHTTP.getAllresponseheaders
        $oHTTP.abort()
        $oHTTP = 0
        Return$header & @CRLF & @CRLF & BinaryToString($Content, 4)
EndFunc返回信息:Server: nginx/1.11.1
Date: Mon, 02 Apr 2018 14:43:38 GMT
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: http://******
Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE
Access-Control-Max-Age: 3600
Access-Control-Allow-Headers: x-requested-with,Authorization,Access-Control,Content-Type
Access-Control-Allow-Credentials: true
Content-Disposition: inline;filename=f.txt



{"code":500,"url":"http://*******/sysEntFile/BootstrapUploadFile.jhtml","message":"数据请求失败"}

zghwelcome 发表于 2018-4-2 22:31:32

本帖最后由 zghwelcome 于 2018-4-3 13:42 编辑

抓包信息:POST http://*******/sysEntFile/BootstrapUploadFile.jhtml?moduleType=trouble&investigationType=1&url=/investigationImage/saveBootstrapInvestigationImage.jhtml&investigationId=0&imageType=1 HTTP/1.1
X-Requested-With: XMLHttpRequest
Accept: application/json, text/javascript, */*; q=0.01
Content-Type: multipart/form-data; boundary=---------------------------7e233301b139c
Referer: http://spiss.91safety.com/investigationAccount/uploadImage.jhtml?investigationId=0&imageType=1&investigationType=1
Accept-Language: zh-CN
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko
Connection: Keep-Alive
Content-Length: 2492
Host: spiss.91safety.com
Pragma: no-cache
Cookie: _qdda=3-1.1; tencentSig=1359885312; _qddamta_800032875=3-0; JSESSIONID=SkmcfRCWdu1qCACunvQ6.tomcat45; _qddaz=QD.k4coos.pmsk7e.jfgl2m3t; pgv_pvi=2371856384

-----------------------------7e233301b139c
Content-Disposition: form-data; name="upFile"; filename="jzpz.jpg"
Content-Type: image/jpeg

   JFIF     C                     



 $.' ",#(7),01444'9=82<.342   C                       


2!!22222222222222222222222222222222222222222222222222    8 Z"                  

     } !1AQa "q2   #BR$3br        
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz                                                                                           

       w !1AQ aq"2 B            #3R br
$4 % &'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz                                                                              ?l
0=( J0=( M*  ?܍K7 4   S\ O   Z P f+h  V'  ]KǎLJ n   iD 2 a Ўl {k[Ps
   i _MW7 ]   xSص 5 n64 9[ j   
玘&-   N$ ( W[ ~ t5   f -} 9=HI      f s x    )c9 U? Aᛉ$s$ 3Hz SR iVr 0 MԂ [   _       QZ   M : ހEPQ]@.m&        Q    KEM ty/n k__& h! ER
s 3 5Sţ}   Ҧ < k >ѽ2 %`dּ9 ɤ YBk ^f VP   & &OX| C X ˖h qs   I&   iu 鹋 U |_wr 6 Y\Y xcEH       |   =    Kau~T    s# aˉ Ʋ   .-3N O4Z[ ^F >f1    *DP    Rn d R a J׶ [ ~{ w QEly M N    W? n> - J   D?J / L|EA{   i   F;(5 # O d   9>    n 3p "  R)/_  ٶ   >:V tVrgVx '   K K    j n gϲq V  0d $   @    // OU m x   ^O < fw( w# NP{   >(óĶ ֋j      go^=]3K uh P    )|ߖL 9a   x ikho{m?NW % Nw  u5<:䰿V   qG)I   Y      ?K EomO k mB ] ߵ vf} / 55K   Mn m"Y\f xU :g WR! ># Fo Q&#1086124; X
Ns `c    ӟ    3V\y8=N}@- j -Я 7 ߖY R      SŨ   \:   ¼ Mzh [;{ O pu=L3 n ]^IZ)øV+ $]W [ɥayS v   p  8G CdYZeω5,_I +`đ Њ Yoh tIJN#tFEVR
èO ~ 7+ ȏ   }   5۵ =f ޼  ^       @@       YhC ;% 8    ? s{   hoF Ӵ, C: y$m    +̣ D} b탨    o <r  x    ` ~=9 '   Q֦ ] .&    9QEu } T      N;}JTL } p9u    5 _ :   =ݮ   t cQ]kvyYfmVQ[;蒶 hw HV I t 1ks % s Awg g?r^/   4 Ȗ V|    ~q Y9ފ*qJ  C\ )  [        M
k  Vpx "H H   8 jo .t .v^G3 6 LNH9'T˫y~h) ^g   J ~QEyJ 
-----------------------------7e233301b139c
Content-Disposition: form-data; name="file_id"

0
-----------------------------7e233301b139c--正确返回信息:HTTP/1.1 200 OK
Server: nginx/1.11.1
Date: Mon, 02 Apr 2018 14:22:39 GMT
Content-Type: application/json;charset=UTF-8
Connection: keep-alive
Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE
Access-Control-Max-Age: 3600
Access-Control-Allow-Headers: x-requested-with,Authorization,Access-Control,Content-Type
Access-Control-Allow-Credentials: true
Content-Disposition: inline;filename=f.txt
Content-Length: 34
Cache-Control: no-cache

{"imageId":2163654,"success":true}

zghwelcome 发表于 2018-4-2 22:34:36

本帖最后由 zghwelcome 于 2018-4-3 13:41 编辑

测试页面:http://******/investigationAccount/uploadImage.jhtml?investigationId=0&imageType=1&investigationType=1

alaws 发表于 2018-4-3 12:19:37

表面上看POST数据包那里的数据格式不正确,从你构造的内容来看,在最终使用send方法发送数据是“字符串(其中混有二进制数据,也是字符串)”,你按下面的方法修改试试(让使用send方法时发送的是纯二进制数据)

你原来的格式:$sPostData=字符串 + 图片二进制 + 字符串
请尝试修改为:$sPostData=StringToBinary(字符串) + 图片二进制 + StringToBinary(字符串)

另外,请不要试图:sPostData=StringToBinary(字符串 + 图片二进制 + 字符串)

zghwelcome 发表于 2018-4-3 13:39:57

回复 4# alaws


谢谢大神指点,一定要给你365个赞! {:face (356):}
页: [1]
查看完整版本: 【已解决】请问各位大神,如何模拟HTTP multipart/form-data 上传图片呢?