By
mLipok
I try to download some file with winhttp.au3
I use code from here:
My code looks like:
#include <FileConstants.au3>
#include "WinHttp.au3"
#AutoIt3Wrapper_Run_AU3Check=N
_Example()
Func _Example()
; Initialize and get session handle
Local $hOpen = _WinHttpOpen()
; Get connection handle
Local $hConnect = _WinHttpConnect($hOpen, "https://MY_URL")
Local $CurrentOption = _WinHttpQueryOption($hConnect, $WINHTTP_OPTION_SECURITY_FLAGS)
Local $Options = BitOR($CurrentOption, _
$SECURITY_FLAG_IGNORE_UNKNOWN_CA, _
$SECURITY_FLAG_IGNORE_CERT_CN_INVALID, _
$SECURITY_FLAG_IGNORE_CERT_DATE_INVALID)
_WinHttpSetOption($hConnect, $WINHTTP_OPTION_SECURITY_FLAGS, $Options)
If @error Then ConsoleWrite("! ---> @error=" & @error & " @extended=" & @extended & _
" : _WinHttpSetOption" & @CRLF)
; Specify the reguest
Local $hRequest = _WinHttpOpenRequest($hConnect, Default, "MY_FILE")
; Send request
_WinHttpSendRequest($hRequest)
; Wait for the response
_WinHttpReceiveResponse($hRequest)
ProgressOn("Downloading", "In Progress...")
Progress(_WinHttpQueryHeaders($hRequest, $WINHTTP_QUERY_CONTENT_LENGTH))
Local $sData
; Check if there is data available...
If _WinHttpQueryDataAvailable($hRequest) Then
While 1
$sChunk = _WinHttpReadData_Ex($hRequest, Default, Default, Default, Progress)
If @error Then ExitLoop
$sData &= $sChunk
Sleep(20)
WEnd
Else
MsgBox(48, "Error", "Site is experiencing problems (or you).")
EndIf
Sleep(1000)
ProgressOff()
; Close handles
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)
Local $hFile = FileOpen(@ScriptDir & "\MY_FILE", $FO_OVERWRITE + $FO_CREATEPATH + $FO_BINARY)
FileWrite($hFile, $sData)
FileClose($hFile)
EndFunc ;==>_Example
Func Progress($iSizeAll, $iSizeChunk = 0)
Local Static $iMax, $iCurrentSize
If $iSizeAll Then $iMax = $iSizeAll
$iCurrentSize += $iSizeChunk
Local $iPercent = Round($iCurrentSize / $iMax * 100, 0)
ProgressSet($iPercent, $iPercent & " %")
EndFunc ;==>Progress
Func _WinHttpReadData_Ex($hRequest, $iMode = Default, $iNumberOfBytesToRead = Default, $pBuffer = Default, $vFunc = Default)
__WinHttpDefault($iMode, 0)
__WinHttpDefault($iNumberOfBytesToRead, 8192)
__WinHttpDefault($vFunc, 0)
Local $tBuffer, $vOutOnError = ""
If $iMode = 2 Then $vOutOnError = Binary($vOutOnError)
Switch $iMode
Case 1, 2
If $pBuffer And $pBuffer <> Default Then
$tBuffer = DllStructCreate("byte[" & $iNumberOfBytesToRead & "]", $pBuffer)
Else
$tBuffer = DllStructCreate("byte[" & $iNumberOfBytesToRead & "]")
EndIf
Case Else
$iMode = 0
If $pBuffer And $pBuffer <> Default Then
$tBuffer = DllStructCreate("char[" & $iNumberOfBytesToRead & "]", $pBuffer)
Else
$tBuffer = DllStructCreate("char[" & $iNumberOfBytesToRead & "]")
EndIf
EndSwitch
Local $sReadType = "dword*"
If BitAND(_WinHttpQueryOption(_WinHttpQueryOption(_WinHttpQueryOption($hRequest, $WINHTTP_OPTION_PARENT_HANDLE), $WINHTTP_OPTION_PARENT_HANDLE), $WINHTTP_OPTION_CONTEXT_VALUE), $WINHTTP_FLAG_ASYNC) Then $sReadType = "ptr"
Local $aCall = DllCall($hWINHTTPDLL__WINHTTP, "bool", "WinHttpReadData", _
"handle", $hRequest, _
"struct*", $tBuffer, _
"dword", $iNumberOfBytesToRead, _
$sReadType, 0)
If @error Or Not $aCall[0] Then Return SetError(1, 0, "")
If Not $aCall[4] Then Return SetError(-1, 0, $vOutOnError)
If IsFunc($vFunc) Then $vFunc(0, $aCall[4])
If $aCall[4] < $iNumberOfBytesToRead Then
Switch $iMode
Case 0
Return SetExtended($aCall[4], StringLeft(DllStructGetData($tBuffer, 1), $aCall[4]))
Case 1
Return SetExtended($aCall[4], BinaryToString(BinaryMid(DllStructGetData($tBuffer, 1), 1, $aCall[4]), 4))
Case 2
Return SetExtended($aCall[4], BinaryMid(DllStructGetData($tBuffer, 1), 1, $aCall[4]))
EndSwitch
Else
Switch $iMode
Case 0, 2
Return SetExtended($aCall[4], DllStructGetData($tBuffer, 1))
Case 1
Return SetExtended($aCall[4], BinaryToString(DllStructGetData($tBuffer, 1), 4))
EndSwitch
EndIf
EndFunc ;==>_WinHttpReadData_Ex
As a result I get file with this contents:
As so far I found this:
So I even with added:
Local $CurrentOption = _WinHttpQueryOption($hConnect, $WINHTTP_OPTION_SECURITY_FLAGS)
Local $Options = BitOR($CurrentOption, _
$SECURITY_FLAG_IGNORE_UNKNOWN_CA, _
$SECURITY_FLAG_IGNORE_CERT_CN_INVALID, _
$SECURITY_FLAG_IGNORE_CERT_DATE_INVALID)
_WinHttpSetOption($hConnect, $WINHTTP_OPTION_SECURITY_FLAGS, $Options)
If @error Then ConsoleWrite("! ---> @error=" & @error & " @extended=" & @extended & _
" : _WinHttpSetOption" & @CRLF)
I still get the same errors.
Anyone know a way how to fix this problem?
Regards,
mLipok