找回密码
 加入
搜索
查看: 1289|回复: 4

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

[复制链接]
发表于 2018-4-2 22:30:22 | 显示全部楼层 |阅读模式
本帖最后由 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":"数据请求失败"}
 楼主| 发表于 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   #B  R  $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 _  M  W  7 ]   x  Sص 5 n64 9[ j   
 玘&  -   N  $ ( W  [ ~ t  5   f -    } 9=HI      f s x    )c  9 U  ? Aᛉ  $  s$ 3  Hz S  R i  Vr 0 MԂ [   _       QZ   M : ހEPQ]@.m&          Q    KEM ty  /n k  _  _& h  ! ER
 s 3 5  S  ţ}   Ҧ < k >ѽ  2   %  `  dּ9 ɤ   YB  k   ^  f VP   & &OX| C   X ˖h qs   I&   i  u 鹋 U |_wr 6 Y\Y xcEH       |   =    Kau~T    s# aˉ Ʋ     .  -3N O4Z[ ^F >f  1    *  DP    Rn d R a J׶ [ ~{ w QEly M N    W? n  > - J     D    ?J / L|E  A{   i   F;(5 # O d   9>    n 3  p "  R)/_  ٶ   >  :V t  VrgVx '   K   K    j n g  ϲq V  0d $   @    // O  U m x     ^  O   < fw( w# NP{   >(  óĶ ֋j      go  ^=]3K uh P    )|ߖL 9a   x i  kho  {m?NW % N  w  u  5<:  䰿  V   qG)I     Y      ?  K EomO k mB ] ߵ vf  } / 55K   Mn m"Y\f xU :g WR! ># FoQ  􉊬 X
 Ns `c    ӟ    3V\y8=N}@-   j -Я7 ߖY R      SŨ   \:   ¼ Mzh [  ;{ O pu=L3 n ]^IZ)øV+ $]W [ɥ  ay  S v   p  8G C  d  YZeω5,_I +`đ Њ Yoh t  IJN#tFEVR
 è  O ~ 7+ ȏ   }   5  ۵ =f ޼  ^       @@       Y  hC ;% 8    ? s  {   hoF Ӵ, C:   y$m    +̣ D} b탨     o <r  x    ` ~=9 '   Q֦ ] .&    9  QEu } T      [  N:      Jk]N;}J  TL } p  9u    5 _ :   =ݮ   t c  Q]  kvyYfmV  Q  [;蒶 hw HV I t 1ks % s Awg g?  r^/   4 Ȗ V|    ~q Y9  ފ*qJ  C\ )  [        M
  k  Vp  x "H H     8 jo .t .v^G3 6 L  NH9'  T  ˫y~  h) ^g     J ~  QEy  J   
-----------------------------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}
 楼主| 发表于 2018-4-2 22:34:36 | 显示全部楼层
本帖最后由 zghwelcome 于 2018-4-3 13:41 编辑

测试页面:
http://******/investigationAccount/uploadImage.jhtml?investigationId=0&imageType=1&investigationType=1
发表于 2018-4-3 12:19:37 | 显示全部楼层
表面上看POST数据包那里的数据格式不正确,从你构造的内容来看,在最终使用send方法发送数据是“字符串(其中混有二进制数据,也是字符串)”,你按下面的方法修改试试(让使用send方法时发送的是纯二进制数据)

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

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

评分

参与人数 1金钱 +40 收起 理由
zghwelcome + 40 听君一席话,如沐清风!

查看全部评分

 楼主| 发表于 2018-4-3 13:39:57 | 显示全部楼层
回复 4# alaws


谢谢大神指点,一定要给你365个赞!
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-17 17:42 , Processed in 0.080398 second(s), 28 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表