本帖最后由 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":"数据请求失败"}
|