本帖最后由 republican 于 2010-12-31 07:12 编辑
回复 1# papapa314
除了参考我的内容外,你还可以参考_WinHttpSimpleXXX(真的忘了,也没怎么仔细看过)
以下所有内容均以HFS作为服务端。
表单内容:POST /360Downloads/ HTTP/1.1
Accept: application/x-shockwave-flash, image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*
Referer: http://10.17.133.128:82/360Downloads/~upload
Accept-Language: zh-cn
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
Content-Type: multipart/form-data; boundary=---------------------------7da9c21d0988
Accept-Encoding: gzip, deflate
Host: 10.17.133.128:82
Content-Length: 624698
Connection: Keep-Alive
Cache-Control: no-cache
-----------------------------7da9c21d0988
Content-Disposition: form-data; name="fileupload1"; filename="名称."
Content-Type: application/octet-stream
数据段...
-----------------------------7da9c21d0988
Content-Disposition: form-data; name="fileupload2"; filename=""
Content-Type: application/octet-stream
-----------------------------7da9c21d0988
Content-Disposition: form-data; name="fileupload3"; filename=""
Content-Type: application/octet-stream
-----------------------------7da9c21d0988
Content-Disposition: form-data; name="fileupload4"; filename=""
Content-Type: application/octet-stream
-----------------------------7da9c21d0988
Content-Disposition: form-data; name="fileupload5"; filename=""
Content-Type: application/octet-stream
-----------------------------7da9c21d0988
Content-Disposition: form-data; name="fileupload6"; filename=""
Content-Type: application/octet-stream
-----------------------------7da9c21d0988
Content-Disposition: form-data; name="fileupload7"; filename=""
Content-Type: application/octet-stream
-----------------------------7da9c21d0988
Content-Disposition: form-data; name="fileupload8"; filename=""
Content-Type: application/octet-stream
-----------------------------7da9c21d0988
Content-Disposition: form-data; name="fileupload9"; filename=""
Content-Type: application/octet-stream
-----------------------------7da9c21d0988
Content-Disposition: form-data; name="fileupload10"; filename=""
Content-Type: application/octet-stream
-----------------------------7da9c21d0988--
根据上述内容,我们就可以这样编写代码:
;~ #include <WinhttpGetrespond.au3>
#include <Winhttp.au3>
;~ $StrBoundary=_IntBoundary()
#include <array.au3>
Local $FPath[10]
$FPath[0] =FileOpenDialog("请选择文件","F\","所有文件(*.*)")
$MyOpen = _WinHttpOpen()
$Result=_WinHTTP_POSTFile($MyOpen,"10.17.133.128","/360Downloads/",$FPath,"fileupload",4)
If @error Then MsgBox(0,"",@error)
MsgBox(0,5,$Result[0])
_WinHttpCloseHandle($MyOpen)
Func _WinHTTP_POSTFile($hOpen,$ServerHost,$URLObj,$FileArray,$IDName,$EndNum,$ReturnMode=0,$Cookies = "",$MoreHeader = "")
If $hOpen = "" Or $ServerHost = "" Then Return SetError(1,0,-1)
$hConnect=_WinHttpConnect($hOpen, $ServerHost,82)
;建立连接
$hRequest = _WinHttpOpenRequest($hConnect, "POST", $URLObj)
If @error Or $hRequest = 0 Then Return SetError(1,0,-1)
;------------计算数据长度---------------
$POSTDateLen = 0
$POSTBasicLen=BinaryLen(StringToBinary(@CRLF&'-----------------------------012345678901'&@CRLF & _
'Content-Disposition: form-data; name="'&$IDName&'"; filename=""'&@CRLF & _
'Content-Type: application/octet-stream'&@CRLF&@CRLF))
For $i = 1 To $EndNum
$POSTDateLen += $POSTBasicLen + BinaryLen(String($i))
Next
$POSTDateLen +=47
;基本长度获取完毕
For $i = 1 To UBound($FileArray)
If FileExists($FileArray[$i -1 ]) Then $POSTDateLen +=FileGetSize($FileArray[$i -1 ]) + BinaryLen(StringToBinary($FileArray[$i -1])) ;添加文件及文件路径长度
Next
;-------------------End of Lenth-----------------------------------
$POSTBoundary=_IntBoundary()
_WinHttpAddRequestHeaders($hRequest, "Content-Length: "&$POSTDateLen&@CRLF)
_WinHttpAddRequestHeaders($hRequest, "Content-Type: multipart/form-data; boundary=---------------------------"&$POSTBoundary)
_WinHttpSendRequest($hRequest) ;发送请求
;--------------------------------发送数据----------------------------------------------
For $i=1 To $EndNum
If $i <=UBound($FileArray) And FileExists($FileArray[$i -1 ]) Then
ConsoleWrite($i&@CRLF)
$ContextDate =StringToBinary(@CRLF & '-----------------------------'&$POSTBoundary&@CRLF & _
'Content-Disposition: form-data; name="'&$IDName&$I&'"; filename="'&$FileArray[$i -1]&'"'&@CRLF & _
'Content-Type: application/octet-stream'&@CRLF&@CRLF)
_WinHttpWriteData($hRequest,$ContextDate,1)
$hFile = FileOpen($FileArray[$i -1],16)
While 1
$dFile = FileRead($hFile,1024 * 512)
If @error Then ExitLoop
_WinHttpWriteData($hRequest,$dFile,1)
Sleep(10)
WEnd
FileClose($hFile)
Else
$ContextDate =StringToBinary(@CRLF & '-----------------------------'&$POSTBoundary&@CRLF & _
'Content-Disposition: form-data; name="'&$IDName&$I&'"; filename=""'&@CRLF & _
'Content-Type: application/octet-stream'&@CRLF&@CRLF)
_WinHttpWriteData($hRequest,$ContextDate,1)
EndIf
Next
$ContextDate =StringToBinary(@CRLF &'-----------------------------'&$POSTBoundary&'--'&@CRLF)
_WinHttpWriteData($hRequest,$ContextDate,1)
;~ Sleep(200)
_WinHttpReceiveResponse($hRequest) ;获取回应
If Not _WinHttpQueryDataAvailable($hRequest) Then Return SetError(4,0,-4)
Local $iReturn[6] = [-1,-1,-1,-1,-1,-1]
$iReturn[1]=_WinHttpQueryHeaders($hRequest,$WINHTTP_QUERY_STATUS_CODE) ;服务器状态
$iReturn[2]=_WinHttpQueryHeaders($hRequest,$WINHTTP_QUERY_CONTENT_LENGTH) ;文件长度
$iReturn[3]=_WinHttpQueryHeaders($hRequest,$WINHTTP_QUERY_SET_COOKIE) ;首个Cookies
$iReturn[4]=_WinHttpQueryHeaders($hRequest,$WINHTTP_QUERY_LOCATION) ;重定向
$iReturn[5]=_WinHttpQueryHeaders($hRequest,$WINHTTP_QUERY_CONTENT_TYPE) ;编码类型
$iReturn[0]= BinaryToString(_WinHttpReadData($hRequest,2,Int($iReturn[2])))
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
Return $iReturn
EndFunc
Func _IntBoundary()
Local $Letter[20],$Boundary="7f"
For $i = 0 To 19
If $i < 10 Then $Letter[$i] = Chr(Random(97, 122, 1))
If $i >= 10 Then $Letter[$i]=Random(0,9,1)
Next
For $i = 0 To 9
$Boundary &=$Letter[Random(0,19,1)]
Next
Return $Boundary
EndFunc
POST FILE必需先研究对方的格式,再自己定义.
演示图:
|