trainer Posted November 22, 2010 Posted November 22, 2010 Hi there, maybe someone can help me with this: how do I upload a file via WinHTTP to a .htaccess-protected php-script via SSL? Is this possible? What I already have is: - Posting SSL encrypted ASCII data to a .htaccess protected script (Script 1) - Sending (NOT SSL encrypted) files to a NOT .htaccess protected script (Script 2) Any Ideas? Thanks for helping, trainer Script 1: $http = ObjCreate("WinHttp.WinHttpRequest.5.1") $http.Open("POST", "https://www.domain.de/test.php", false) $http.SetRequestHeader ("Content-Type", "application/x-www-form-urlencoded") $http.SetCredentials("user", "passwort", 0) $http.Send("content=123") ConsoleWrite($http.ResponseText & @CR) Script2: expandcollapse popup#include-once #include "Array.au3" #include "WinHTTP.au3" Dim $file[1][2] = [["file_upload","export.zip"]] $test = post_multipart("http://www.domain.de/test.php", "", 0, $file) ConsoleWrite($test[0] & @CR) ConsoleWrite($test[1] & @CR) ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Global $MIMETypes[1][2] = [["zip", "application/zip"]] _ArraySort($MIMETypes, 0, 0, 0, 2) ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Func post_multipart($host, $selector, $fields, $files) Local $Return = encode_multipart_formdata($fields, $files) Local $content_type = 'Content-Type: ' & $Return[0] & @CRLF $body = $Return[1] Local $URL = _WinHttpCrackUrl($host) Local $hSession = _WinHttpOpen() Local $hConnection = _WinHTTPConnect($hSession,$URL[2],$URL[3]) Local $hRequest = _WinHttpOpenRequest($hConnection,"POST",$URL[6]&$URL[7],"HTTP/1.1","http://"&$URL[2]) _WinHttpSendRequest($hRequest,$content_type,$WINHTTP_NO_REQUEST_DATA,StringLen($Return[1])) _WinHTTPWriteDataBin($hRequest,StringToBinary($Return[1])) _WinHttpReceiveResponse($hRequest) Local $Return[2] If _WinHttpQueryDataAvailable($hRequest) Then Local $temp While 1 $temp = _WinHttpReadData($hRequest) If $temp = "" Then ExitLoop $Return[1] &=$temp WEnd $temp ="" EndIf $Return[0] = _WinHttpQueryHeaders($hRequest) _WinHttpCloseHandle($hRequest) _WinHttpCloseHandle($hConnection) _WinHttpCloseHandle($hSession) Return $Return EndFunc ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Func _WinHttpWriteDataBin($hRequest, $binary) Local $lpBinary Local $iNumberOfBytesToWrite If IsDllStruct($binary) Then $lpBinary = DllStructGetPtr($binary) $iNumberOfBytesToWrite = DllStructGetSize($binary) Else $iNumberOfBytesToWrite = BinaryLen($binary) Local $sBinary = DllStructCreate("byte[" & $iNumberOfBytesToWrite & "]") DllStructSetData($sBinary, 1, $binary) $lpBinary = DllStructGetPtr($sBinary) EndIf Local $a_iCall = DllCall("Winhttp.dll", "int", "WinHttpWriteData", _ "hwnd", $hRequest, _ "ptr", $lpBinary, _ "dword", $iNumberOfBytesToWrite, _ "dword*", 0) If @error Or Not $a_iCall[0] Then Return SetError(1, 0, 0) EndIf Return SetError(0, $a_iCall[4], 1) EndFunc ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Func encode_multipart_formdata($fields, $files) Local Const $BOUNDARY = getBoundary() $L = "" For $i = 0 To UBound($fields) - 1 $L &= ('--' & $BOUNDARY) & @CRLF $L &= ('Content-Disposition: form-data; name="' & $fields[$i][0] & '"') & @CRLF $L &= @CRLF $L &= $fields[$i][1] & @CRLF Next For $i = 0 To UBound($files) - 1 $L &= ('--' & $BOUNDARY) & @CRLF $L &= ('Content-Disposition: form-data; name="' & $files[$i][0] & '"; filename="' & $files[$i][1] & '"') & @CRLF $content_type = get_content_type($files[$i][1]) $L &= ('Content-Type: ' & $content_type) & @CRLF $L &= @CRLF $L &= FileRead($files[$i][1]) & @CRLF Next $L &= ('--' & $BOUNDARY & '--') & @CRLF $L &= @CRLF $content_type = 'multipart/form-data; boundary="' & $BOUNDARY & '"' Local $Return[2] = [$content_type, $L] Return $Return EndFunc ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Func get_content_type($path) Local $szExt = StringLower(StringRegExpReplace( $path ,".*(?:\.([^.\\/]*))?\Z","$1")) If $szExt = "" Then Return 'application/octet-stream' Local $mimeid = _ArrayBinarySearch2D($MIMETypes, $szExt) If $mimeid = -1 Then Return SetError(1, 0, 'application/octet-stream') Return $MIMETypes[$mimeid][1] EndFunc ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Func _ArrayBinarySearch2D(Const ByRef $avArray, $vValue, $iStart = 0, $Column = 0, $iEnd = 0) If Not IsArray($avArray) Then Return SetError(1, 0, -1) Local $iUBound = UBound($avArray) - 1 ; Bounds checking If $iEnd < 1 Or $iEnd > $iUBound Then $iEnd = $iUBound If $iStart < 0 Then $iStart = 0 If $iStart > $iEnd Then Return SetError(4, 0, -1) Local $iMid = Int(($iEnd + $iStart) / 2) If $avArray[$iStart][$Column] > $vValue Or $avArray[$iEnd][$Column] < $vValue Then Return SetError(2, 0, -1) ; Search While $iStart <= $iMid And $vValue <> $avArray[$iMid][$Column] If $vValue < $avArray[$iMid][$Column] Then $iEnd = $iMid - 1 Else $iStart = $iMid + 1 EndIf $iMid = Int(($iEnd + $iStart) / 2) WEnd ; Entry not found If $iStart > $iEnd Then Return SetError(3, 0, -1) Return $iMid EndFunc ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Func getBoundary() return Stringleft(_TimeGetStamp()*random(10,256,1), 16) EndFunc ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Func _TimeGetStamp() Local $av_Time $av_Time = DllCall('CrtDll.dll', 'long:cdecl', 'time', 'ptr', 0) If @error Then SetError(99) Return False EndIf Return $av_Time[0] EndFunc ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now