;~ #include-once #cs HTTP.au3 made by @Jefrey Repo: http://github.com/jesobreira/HTTP.au3 #ce $test = _HTTP_Upload("http://mysitetest/index.php", @ScriptDir & "\myFile.txt", "uploadinput", "pwd=123456&filename=" & URLEncode("test.txt") ) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $test = ' & $test & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console Func _HTTP_Get($url) Local $oHTTP = ObjCreate("winhttp.winhttprequest.5.1") $oHTTP.Open("GET", $url, False) If (@error) Then Return SetError(1, 0, 0) $oHTTP.Send() If (@error) Then Return SetError(2, 0, 0) $sReceived = $oHTTP.ResponseText $iStatus = $oHTTP.Status If $iStatus = 200 Then Return $sReceived Else Return SetError(3, $iStatus, $sReceived) EndIf EndFunc ;==>_HTTP_Get Func _HTTP_Post($url, $postdata = '') Local $oHTTP = ObjCreate("winhttp.winhttprequest.5.1") $oHTTP.Open("POST", $url, False) If (@error) Then Return SetError(1, 0, 0) $oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded") $oHTTP.Send($postdata) If (@error) Then Return SetError(2, 0, 0) $sReceived = $oHTTP.ResponseText $iStatus = $oHTTP.Status If $iStatus = 200 Then Return $sReceived Else Return SetError(3, $iStatus, $sReceived) EndIf EndFunc ;==>_HTTP_Post Func _HTTP_Upload($strUploadUrl, $strFilePath, $strFileField, $strDataPairs = '', $strFilename = Default) If $strFilename = Default Then $strFilename = StringMid($strFilePath, StringInStr($strFilePath, "\", 0, -1) + 1) Local $MULTIPART_BOUNDARY = "----WebKitFormBoundary" $pwd = "" Dim $aSpace[3] For $i = 1 To 16 $aSpace[0] = Chr(Random(65, 90, 1)) ;A-Z $aSpace[1] = Chr(Random(97, 122, 1)) ;a-z $aSpace[2] = Chr(Random(48, 57, 1)) ;0-9 $MULTIPART_BOUNDARY &= $aSpace[Random(0, 2, 1)] Next Local $bytFormData, $bytFormStart, $bytFile Local $strFormStart, $strFormEnd, $strDataPair If Not FileExists($strFilePath) Then Return SetError(4, 0, 0) EndIf $h = FileOpen($strFilePath, 16) $bytFile = FileRead($h) FileClose($h) ; Create the multipart form data ; Define the end of form $strFormEnd = @CRLF & "--" & $MULTIPART_BOUNDARY & "--" & @CRLF ; First add any ordinary form data pairs If $strDataPairs Then Local $split = StringSplit($strDataPairs, "&") For $i = 1 To $split[0] $splitagain = StringSplit($split[$i], "=") $strFormStart &= "--" & $MULTIPART_BOUNDARY & @CRLF & _ "Content-Disposition: form-data; " & _ "name=""" & $splitagain[1] & """" & _ @CRLF & @CRLF & _ URLDecode($splitagain[2]) & @CRLF Next EndIf ; Now add the header for the uploaded file $strFormStart &= "--" & $MULTIPART_BOUNDARY & @CRLF & _ "Content-Disposition: form-data; " & _ "name=""" & $strFileField & """; " & _ "filename=""" & $strFilename & """" & @CRLF & _ "Content-Type: application/upload" & _ ; bogus, but it works @CRLF & @CRLF ; Now merge it all $bytFormData = StringToBinary($strFormStart) & $bytFile & StringToBinary($strFormEnd) ; Upload it Local $oHTTP = ObjCreate("winhttp.winhttprequest.5.1") $oHTTP.Open("POST", $strUploadUrl, False) If (@error) Then Return SetError(1, 0, 0) $oHTTP.SetRequestHeader("Content-Type", "multipart/form-data; boundary=" & $MULTIPART_BOUNDARY) $oHTTP.Send($bytFormData) If (@error) Then Return SetError(2, 0, 0) $sReceived = $oHTTP.ResponseText $iStatus = $oHTTP.Status If $iStatus = 200 Then Return $sReceived Else Return SetError(3, $iStatus, $sReceived) EndIf EndFunc ;==>_HTTP_Upload Func URLEncode($vData) If IsBool($vData) Then Return $vData Local $aData = StringToASCIIArray($vData, Default, Default, 2) Local $sOut = '', $total = UBound($aData) - 1 For $i = 0 To $total Switch $aData[$i] Case 45, 46, 48 To 57, 65 To 90, 95, 97 To 122, 126 $sOut &= Chr($aData[$i]) Case 32 $sOut &= '+' Case Else $sOut &= '%' & Hex($aData[$i], 2) EndSwitch Next Return $sOut EndFunc Func URLDecode($sData) ; Prog@ndy Local $aData = StringSplit(StringReplace($sData, "+", " ", 0, 1), "%") $sData = "" For $i = 2 To $aData[0] $aData[1] &= Chr(Dec(StringLeft($aData[$i], 2))) & StringTrimLeft($aData[$i], 2) Next Return BinaryToString(StringToBinary($aData[1], 1), 4) EndFunc ;==>URLDecode