Jump to content

Recommended Posts

Posted (edited)

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:
 

  Quote

Bad Request

Your browser sent a request that this server could not understand.
Reason: You're speaking plain HTTP to an SSL-enabled server port.
Instead use the HTTPS scheme to access this URL, please.

Expand  

As so far I found this:

  

  On 6/3/2015 at 5:08 PM, bPi said:

I was able to get it to go using the undocumented parameter for ignoring all cert errors.

Expand  


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

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)

try this way if not solve then I don't know

Local $hConnect = _WinHttpConnect($hOpen, "https://MY_URL", $INTERNET_DEFAULT_HTTPS_PORT)
Local $hRequest = _WinHttpOpenRequest($hConnect, Default, "MY_FILE", Default, Default, Default, $WINHTTP_FLAG_SECURE)

or try adding _WinHttpSetOption

;~ WinHttp SetOption to use SECURE PROTOCOL TLS1.1 or TLS1.2
_WinHttpSetOption($hOpen, $WINHTTP_OPTION_SECURE_PROTOCOLS, _
  BitOR($WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1, $WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2))

 

Edited by jugador
typo error
Posted

Where $WINHTTP_OPEN_REQUEST_FLAGS is defined ?

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)

This:
 

Local $hConnect = _WinHttpConnect($hOpen, "https://MY_URL", $INTERNET_DEFAULT_HTTPS_PORT)
Local $hRequest = _WinHttpOpenRequest($hConnect, Default, "MY_FILE", Default, Default, Default, $WINHTTP_FLAG_SECURE)

partially solve the problem.

I mean progress bar was showed longer, downloading was in progress, but the downloaded data was not the same which I expected.
This particular file is grather then fiel saved to disc.

EDIT 1:


using:

Func Progress($iSizeAll, $iSizeChunk = 0)
    Local Static $iMax, $iCurrentSize
    If $iSizeAll Then $iMax = $iSizeAll
    $iCurrentSize += $iSizeChunk
    ConsoleWrite("! $iCurrentSize = " &$iCurrentSize & @CRLF)

    Local $iPercent = Round($iCurrentSize / $iMax * 100, 0)
    ProgressSet($iPercent, $iPercent & " %")
EndFunc   ;==>Progress

and:

ConsoleWrite("! BinaryLen($sData) = " & BinaryLen($sData) & @CRLF)
    Local $hFile = FileOpen(@ScriptDir & "\MY_FILE", $FO_OVERWRITE + $FO_CREATEPATH + $FO_BINARY)
    FileWrite($hFile, $sData)
    FileClose($hFile)

 

I see in console:

  Quote

! $iCurrentSize = 21725184
! $iCurrentSize = 21733376
! $iCurrentSize = 21734280
! BinaryLen($sData) = 928021

Expand  


EDIT 2:
using:

$sChunk = _WinHttpReadData_Ex($hRequest, 1, Default, Default, Progress)
  Quote

! $iCurrentSize = 21734280
! BinaryLen($sData) = 20555515
 

Expand  

using:

$sChunk = _WinHttpReadData_Ex($hRequest, 2, Default, Default, Progress)
  Quote

! $iCurrentSize = 21734280
! BinaryLen($sData) = 43473868

Expand  


 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

Finally I get it works:
 

;~ https://www.autoitscript.com/forum/topic/178561-simple-web-downloader-with-progress-bar/?do=findComment&comment=1301321
#include <FileConstants.au3>
#include <StringConstants.au3>
#include "WinHttp.au3"

Global $EXAMPLE_URL = ""
Global $EXAMPLE_FILE = ""

;~ _Example(0)
_Example(1)
;~ _Example(2)

Func _Example($iMode)
    Local $hOpen = _WinHttpOpen() ; Initialize and get session handle
    Local $hConnect = _WinHttpConnect($hOpen, $EXAMPLE_URL, $INTERNET_DEFAULT_HTTPS_PORT) ; Get connection handle

;~  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)

    Local $hConnect = _WinHttpConnect($hOpen, $EXAMPLE_URL, $INTERNET_DEFAULT_HTTPS_PORT) ; Get connection handle
    Local $hRequest = _WinHttpOpenRequest($hConnect, Default, $EXAMPLE_FILE, Default, Default, Default, $WINHTTP_FLAG_SECURE)
    If @error Then ConsoleWrite("! ---> @error=" & @error & "  @extended=" & @extended & _
            " : _WinHttpOpenRequest" & @CRLF)

    _WinHttpSendRequest($hRequest)    ; Send request

    _WinHttpReceiveResponse($hRequest)    ; Wait for the response

    ProgressOn("Downloading", "In Progress...")
    Progress(_WinHttpQueryHeaders($hRequest, $WINHTTP_QUERY_CONTENT_LENGTH))

    Local $sData
    If _WinHttpQueryDataAvailable($hRequest) Then    ; Check if there is data available...
        While 1
            $sChunk = _WinHttpReadData_Ex($hRequest, $iMode, Default, Default, Progress)
            If @error Then ExitLoop
            $sData &= $sChunk
        WEnd
    Else
        MsgBox(48, "Error", "Site is experiencing problems (or you).")
    EndIf

    Sleep(1000)
    ProgressOff()

    ; Close handles
    _WinHttpCloseHandle($hRequest)
    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hOpen)

    ConsoleWrite("! BinaryLen($sData) = " & BinaryLen($sData) & @CRLF)
    Local $hFile = FileOpen(@ScriptDir & "\" & $EXAMPLE_FILE, $FO_OVERWRITE + $FO_CREATEPATH + $FO_BINARY + $FO_UTF8)
    FileWrite($hFile, $sData)
    FileClose($hFile)

EndFunc   ;==>_Example

Func Progress($iSizeAll, $iSizeChunk = 0)
    Local Static $iMax, $iCurrentSize
    If $iSizeAll Then $iMax = $iSizeAll
    $iCurrentSize += $iSizeChunk
    If Not @compiled Then  ConsoleWrite("! $iCurrentSize = " & $iCurrentSize & @CRLF)

    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])
    Local $dBinary = DllStructGetData($tBuffer, 1)
    If $aCall[4] < $iNumberOfBytesToRead Then
        Switch $iMode
            Case 0
                Return SetExtended($aCall[4], StringLeft($dBinary, $aCall[4]))
            Case 1
                Return SetExtended($aCall[4], BinaryToString(BinaryMid($dBinary, 1, $aCall[4]), $SB_ANSI))
            Case 2
                Return SetExtended($aCall[4], BinaryMid($dBinary, 1, $aCall[4]))
        EndSwitch
    Else
        Switch $iMode
            Case 0, 2
                Return SetExtended($aCall[4], $dBinary)
            Case 1
                Return SetExtended($aCall[4], BinaryToString($dBinary, $SB_ANSI))
        EndSwitch
    EndIf
EndFunc   ;==>_WinHttpReadData_Ex

The problem was related to BinaryToString() which used $SB_UTF8  but $SB_ANSI should be used.
 

Also thanks to @jugador for pointing me on the right track:

  On 10/4/2021 at 11:23 AM, jugador said:

try this way if not solve then I don't know

Local $hConnect = _WinHttpConnect($hOpen, "https://MY_URL", $INTERNET_DEFAULT_HTTPS_PORT)
Local $hRequest = _WinHttpOpenRequest($hConnect, Default, "MY_FILE", Default, Default, Default, $WINHTTP_OPEN_REQUEST_FLAGS)

 

Expand  


I mean his post allow me to find out that proper settings are:

Local $hConnect = _WinHttpConnect($hOpen, $EXAMPLE_URL, $INTERNET_DEFAULT_HTTPS_PORT) ; Get connection handle
    Local $hRequest = _WinHttpOpenRequest($hConnect, Default, $EXAMPLE_FILE, Default, Default, Default, $WINHTTP_FLAG_SECURE)

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

Here is better example:

#include <FileConstants.au3>
#include <StringConstants.au3>
#include "WinHttp.au3"

_Example()

Func _Example()
    ; "YOUR_SOURCE_URL"
    Local $EXAMPLE_URL = "https://www.autoitscript.com/cgi-bin/getfile.pl?../autoit3/scite/download/SciTE4AutoIt3.exe"
    ; "YOUR_DESTINATION"
    Local $LOCAL_FILE = @ScriptDir & "\SciTE4AutoIt3.exe"

;~ _WinHTTP_GetAndSaveFileToDisc_Ex($EXAMPLE_URL, $LOCAL_FILE, 0)
    _WinHTTP_GetAndSaveFileToDisc_Ex($EXAMPLE_URL, $LOCAL_FILE, 1)
;~ _WinHTTP_GetAndSaveFileToDisc_Ex($EXAMPLE_URL, $LOCAL_FILE, 2)
EndFunc   ;==>_Example

Func _WinHTTP_GetAndSaveFileToDisc_Ex($EXAMPLE_URL, $LOCAL_FILE, $iMode = 1)
    Local $hTimer = TimerInit()
    Local $hOpen = _WinHttpOpen() ; Initialize and get session handle
    Local $s_URL_Domain = StringRegExpReplace($EXAMPLE_URL, '(?i)(.*?\..*?)(\/.+)', '$1')
    Local $hConnect = _WinHttpConnect($hOpen, $s_URL_Domain, $INTERNET_DEFAULT_HTTPS_PORT) ; Get connection handle

;~  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("! IFNC: ---> @error=" & @error & "  @extended=" & @extended & _
;~          " : _WinHttpSetOption" & @CRLF)

    Local $s_URL_File = StringReplace($EXAMPLE_URL, $s_URL_Domain, '')
    Local $hRequest = _WinHttpOpenRequest($hConnect, Default, $s_URL_File, Default, Default, Default, $WINHTTP_FLAG_SECURE)
    If @error Then ConsoleWrite("! ---> @error=" & @error & "  @extended=" & @extended & _
            " : _WinHttpOpenRequest" & @CRLF)

    _WinHttpSendRequest($hRequest)    ; Send request

    _WinHttpReceiveResponse($hRequest)    ; Wait for the response

    ProgressOn("Downloading", "In __WinHTTP_DownloadProgress...")
    __WinHTTP_DownloadProgress(_WinHttpQueryHeaders($hRequest, $WINHTTP_QUERY_CONTENT_LENGTH))

    Local $sData, $sChunk
    If _WinHttpQueryDataAvailable($hRequest) Then    ; Check if there is data available...
        While 1
            $sChunk = _WinHttpReadData_Ex($hRequest, $iMode, Default, Default, __WinHTTP_DownloadProgress)
            If @error Then ExitLoop
            $sData &= $sChunk
        WEnd
    Else
        MsgBox(48, "Error", "Site is experiencing problems (or you).")
    EndIf

    Sleep(100)
    ProgressOff()

    ; Close handles
    _WinHttpCloseHandle($hRequest)
    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hOpen)

    If Not @Compiled Then ConsoleWrite("! IFNC: BinaryLen($sData) = " & BinaryLen($sData) & @CRLF)
    Local $hFile = FileOpen($LOCAL_FILE, $FO_OVERWRITE + $FO_CREATEPATH + $FO_BINARY + $FO_UTF8)
    FileWrite($hFile, $sData)
    FileClose($hFile)
    If Not @Compiled Then ConsoleWrite("> IFNC: " & TimerDiff($hTimer) / 1000 & @CRLF)
EndFunc   ;==>_WinHTTP_GetAndSaveFileToDisc_Ex

Func __WinHTTP_DownloadProgress($iSizeAll, $iSizeChunk = 0)
    Local Static $iMax, $iCurrentSize
    If $iSizeAll Then $iMax = $iSizeAll
    $iCurrentSize += $iSizeChunk
    If Not @Compiled Then ConsoleWrite("! IFNC: $iCurrentSize = " & $iCurrentSize & @CRLF)
    Local Static $iPercent_static = 0

    Local $iPercent = Round($iCurrentSize / $iMax * 100, 0)
    If $iPercent_static <> $iPercent Then
        $iPercent_static = $iPercent
        ProgressSet($iPercent, $iPercent & " %")
    EndIf
EndFunc   ;==>__WinHTTP_DownloadProgress

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])
    Local $dBinary = DllStructGetData($tBuffer, 1)
    If $aCall[4] < $iNumberOfBytesToRead Then
        Switch $iMode
            Case 0
                Return SetExtended($aCall[4], StringLeft($dBinary, $aCall[4]))
            Case 1
                Return SetExtended($aCall[4], BinaryToString(BinaryMid($dBinary, 1, $aCall[4]), $SB_ANSI))
            Case 2
                Return SetExtended($aCall[4], BinaryMid($dBinary, 1, $aCall[4]))
        EndSwitch
    Else
        Switch $iMode
            Case 0, 2
                Return SetExtended($aCall[4], $dBinary)
            Case 1
                Return SetExtended($aCall[4], BinaryToString($dBinary, $SB_ANSI))
        EndSwitch
    EndIf
EndFunc   ;==>_WinHttpReadData_Ex

 

I mean I fixed issue with Au3Check compability, but also added proper code for: $s_URL_Domain and $s_URL_File, also some refactoring.
Have fun :D

@mLipok

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

  • 2 weeks later...
Posted

I still have some problems with: "Site is experiencing problems (or you)"
WIP ... investigating.

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

  • 2 months later...
Posted (edited)

@mLipok Can you try replacing your error MsgBox with this and show us the results?:

#include <WinAPIError.au3>

MsgBox(48, "Error " & _WinAPI_GetLastError(), _WinAPI_GetLastErrorMessage())

You can find the documentation for the WinHttpQueryDataAvailable function here: https://docs.microsoft.com/en-us/windows/win32/api/winhttp/nf-winhttp-winhttpquerydataavailable

This is the function called by the _WinHttpQueryDataAvailable UDF.

Edited by TheDcoder
Add links to docs

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

  • 3 months later...
Posted (edited)
Local $s_Information = _
                "Site is experiencing problems (or you)" & @CRLF & _
                _WinAPI_GetLastError() & @CRLF & _
                _WinAPI_GetLastErrorMessage() & @CRLF & _
                ""
        ClipPut($s_Information)
        MsgBox(48, "Error ", $s_Information)

Here are results:

  Quote

Site is experiencing problems (or you)
12019

Expand  

 

https://docs.microsoft.com/en-us/windows/win32/wininet/wininet-errors

  Quote

ERROR_INTERNET_INCORRECT_HANDLE_STATE
12019
The requested operation cannot be carried out because the handle supplied is not in the correct state.

Expand  

 

Also found this:
https://stackoverflow.com/questions/69705380/error-winhttp-incorrect-handle-state-from-winhttpqueryheaders

But this not fix my issue as so far ;(

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

I don't remember the details, but it looks like you are calling the function at the wrong time, perhaps the data is not available yet? What happens if you keep retrying with sleep in between?

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Posted

@mLipok 

#include <WinAPIError.au3>
#include "WinHTTP.au3"

__Example1()
Func __Example1()
    Local $LOCAL_FILE = @ScriptDir & "\SciTE4AutoIt3.exe"
    Local $o_Url = "https://www.autoitscript.com/cgi-bin/getfile.pl?../autoit3/scite/download/SciTE4AutoIt3.exe"
    __WinhttpExampleA($o_Url, 2, $LOCAL_FILE)
EndFunc

Func __WinhttpExampleA($o_BUrl, $o_iMode = 0, $o_Fpath = '')
    Local $o_CrackedUrl = _WinHttpCrackUrl($o_BUrl)
    Local $h_Open = _WinHttpOpen()
#cs
    _WinHttpSetOption($h_Open, _
                     $WINHTTP_OPTION_SECURE_PROTOCOLS, _
                     BitOR($WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1, $WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2))
#ce
    Local $h_Connect = _WinHttpConnect($h_Open, $o_CrackedUrl[2], $INTERNET_DEFAULT_HTTPS_PORT)
    Local $h_Request = _WinHttpOpenRequest($h_Connect, 'GET', $o_CrackedUrl[6] & $o_CrackedUrl[7], _
                                                    Default, Default, Default, $WINHTTP_FLAG_SECURE)
    _WinHttpSendRequest($h_Request)
    _WinHttpReceiveResponse($h_Request)
    Local $o_QHeaders = _WinHttpQueryHeaders($h_Request, $WINHTTP_QUERY_CONTENT_LENGTH)

    Local $o_Response = (($o_iMode = 0) ? "" : Binary(""))
    Local $D_percent
    ProgressOn("Downloading", "In Progress...")
    If _WinHttpQueryDataAvailable($h_Request) Then
        Do
            $D_percent = (BinaryLen($o_Response) * 100) / $o_QHeaders
            ProgressSet(Floor($D_percent), Floor($D_percent) & "%")
            $o_Response &= _WinHttpReadData($h_Request, $o_iMode)
        Until @error
    EndIf
    ProgressSet(100, "Done", "Downloading Complete")
    Sleep(500)
    ProgressOff()

    _WinHttpCloseHandle($h_Request)
    _WinHttpCloseHandle($h_Connect)
    _WinHttpCloseHandle($h_Open)
    ConsoleWrite('Download Complete..... ' & @CRLF)

    If ($o_iMode = 2) And ($o_Fpath <> '') Then
        Local $hFile = FileOpen($o_Fpath, 26)
        FileWrite($hFile, $o_Response)
        FileClose($hFile)
    Endif
    Return
EndFunc

 

  • 3 weeks later...
Posted
  On 4/26/2022 at 1:46 PM, TheDcoder said:

I don't remember the details, but it looks like you are calling the function at the wrong time, perhaps the data is not available yet? What happens if you keep retrying with sleep in between?

Expand  

Yes, this solved one of our problems I recall! 

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...