Hi everybody,
I'm currently working on a script for sending files to a remote server by using the HTTP post method.
It's not my fav. way of sending files, but the remote party only supports this method.
So be it.
After digging through many examples over the net, I've managed to create a script that seems to do the trick.
There is one issue with my approach.
When I'm checking the returned @extended value from _WinHttpWriteData it always returns 0, despite a 200 OK status code from the remote site.
How come, what am I doing wrong here.
The script runs against a public httppost test server, so if needed you can run the script as well.
Thanks for your time.
AutoIt
#include <WinHttp.au3> SendFile("a_text_file.txt") SendFile("another_text_file.txt") Local $iBytesWritten Local $sResponseHeader Local $sResponseData Local $hSession Local $hConnect Local $hRequest ; ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ; 1) Create/Open a session handle ; ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ;Depending on using a proxy we use the appropriate function call $hSession = _WinHttpOpen($sUserAgent,$WINHTTP_ACCESS_TYPE_NAMED_PROXY,$sProxyIP & ":" & $sProxyPort,$WINHTTP_NO_PROXY_BYPASS,0) $hSession = _WinHttpOpen($sUserAgent, $WINHTTP_ACCESS_TYPE_NO_PROXY,$WINHTTP_NO_PROXY_NAME,$WINHTTP_NO_PROXY_BYPASS,0) ; ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ; 2) Create/Open connection handle ; ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ; Are we using HTTP or HTTPS? ConsoleWrite("Step 2) Executing function _WinHttpConnect(" & $hSession & "," & $sURLPrefix & $sHostName & $sUrlSuffix & "," & $INTERNET_DEFAULT_HTTPS_PORT & ") " & @CRLF) Else ; HTTP ConsoleWrite("Step 2) Executing function _WinHttpConnect(" & $hSession & "," & $sURLPrefix & $sHostName & $sUrlSuffix & "," & $INTERNET_DEFAULT_HTTP_PORT & ") " & @CRLF) ; ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ; 3a) Create/Open a request handle ; ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ; Are we using HTTP or HTTPS? ConsoleWrite("Step 3a) Executing funcion: _WinHttpOpenRequest(" & $hConnect & "," & $sMethod & "," & $sUrlSuffix & $sPHP_Script & $sPHP_Param & "," & $sHttpVersion & "," & $WINHTTP_NO_REFERER & ",*/*," & $WINHTTP_FLAG_SECURE & ")" & @CRLF) Else ; HTTP ConsoleWrite("Step 3a) Executing funcion: _WinHttpOpenRequest(" & $hConnect & "," & $sMethod & "," & $sUrlSuffix & $sPHP_Script & $sPHP_Param & "," & $sHttpVersion & "," & $WINHTTP_NO_REFERER & ",*/*," & Default & ")" & @CRLF) ; ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ; 3b)Pass credentionals before sending a request ; ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- _WinHttpSetCredentials($hRequest, $WINHTTP_AUTH_TARGET_SERVER, $WINHTTP_AUTH_SCHEME_BASIC, $sUserName,$sUserPass) ConsoleWrite("Step 3b) Executing funcion: _WinHttpSetCredentials(" & $hRequest & "," & $WINHTTP_AUTH_TARGET_SERVER & "," & $WINHTTP_AUTH_SCHEME_BASIC & "," & $sUserName & "," & $sUserPass & ")" & @CRLF) ; ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ; 3c)Send the request ; ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ConsoleWrite("Step 3c) Executing funcion: _WinHttpSendRequest(" & $hRequest & "," & $WINHTTP_NO_ADDITIONAL_HEADERS & "," & $WINHTTP_NO_REQUEST_DATA & ",0,0)" & @CRLF) ; ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ; 3d) End the request ; ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- _WinHttpReceiveResponse($hRequest) $sResponseHeader = _WinHttpQueryHeaders($hRequest) $iStatusCode = _WinHttpQueryHeaders($hRequest,$WINHTTP_QUERY_STATUS_CODE) ; ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ; 4) Send the next request ; ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ConsoleWrite("Step 4) Executing function _WinHttpSendRequest(" & $hRequest & "," & $WINHTTP_NO_ADDITIONAL_HEADERS & "," & $sFileData & "," & $iFileLen & ",Default)" & @CRLF) ; ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ; 5) Write data to server ; ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- _WinHttpWriteData($hRequest,$sFileData,0) ;Success - Returns 1 ;- @extended receives the number of bytes written. ;Despite a successful post, @extended remains 0 ;Why?????? ConsoleWrite("Step 5) Executing function _WinHttpWriteData(" & $hRequest & "," & $sFileData & ",0)" & @CRLF) ConsoleWrite("Step 5) File " & $sFolder & $sFileName & " succesfully send to " & $sHostName & " Total bytes written: " & $iBytesWritten & @CRLF) ; ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ; 6) End the request ; ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- _WinHttpReceiveResponse($hRequest) ; ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ; 7) Show response from server ; ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- $sResponseHeader = _WinHttpQueryHeaders($hRequest) $iStatusCode = _WinHttpQueryHeaders($hRequest,$WINHTTP_QUERY_STATUS_CODE) ; ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ; 8) Close handles ; ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- _winHttpCloseHandle($hRequest) _winHttpCloseHandle($hConnect) _winHttpCloseHandle($hSession)