Jump to content

Recommended Posts

Hi again, this is my last option to retrieve file from server via web.

; Initialize and get session handle
         $hOpen = _WinHttpOpen()
         _writeLog("return $hOpen:" &$hOpen)
         ; Get connection handle
         $hConnect = _WinHttpConnect($hOpen, $url, $INTERNET_DEFAULT_HTTPS_PORT)
         _writeLog("return $hConnect:" &$hConnect)
         ; Fill the login form:
         $check_login = _WinHttpSimpleFormFill($hConnect, _
        $hOpen, _
        "id1", _
        "name:LoginForm[userName]", $userID, _
        "name:LoginForm[password]", $password,_
        "[IGNORE_CERT_ERRORS]")
         If @error Then _writeLog("login failed : " & @error)
        _writeLog("check login UIS" &$check_login)

        ; Close connection handle
      _WinHttpCloseHandle($hConnect)

         ; Open new connection handle
         $hConnect = _WinHttpConnect($hOpen, $url, $INTERNET_DEFAULT_HTTPS_PORT)
         $s_data = _WinHttpSimpleSSLRequest($hConnect,Default,$Download_url,Default,Default,Default,Default,2) ; BINARY
        ;$s_data = _WinHttpSimpleSSLRequest($hConnect, Default, $Download_url)
         _writeLog("return $s_data " &$s_data)
        ; Close connection handle
         _WinHttpCloseHandle($hConnect)
         ; Close session handle
         _WinHttpCloseHandle($hOpen)

         ; Open the file for writing (append to the end of a file) and store the handle to a variable.
         Local $hFileOpen = FileOpen($sFilePath, 18)
         If $hFileOpen = -1 Then
            _writeLog("An error occurred whilst writing the temporary file.")
         EndIf
          ;Write the data to the file
         FileWrite($hFileOpen ,$s_data)
         FileClose($hFileOpen)

However it paused  at 

_WinHttpSimpleSSLRequest

before this received error: 1

$url = "https://192.168.10.167/ui/login"

$Download_url https://192.168.10.167/ui/backup/files?3-1.ILinkListener-files-1-download

Any idea? 

Link to comment
Share on other sites

Update my code based this forum helpful link by @mikell

_writeLog("url: " &$url)
         $sServerAddress = StringReplace($url, '/login', '')
         _writeLog("server address: " &$sServerAddress )
         $sGeneratorLocation = "/login"

         ; Initialize and get session handle
         $hOpen = _WinHttpOpen()
         _writeLog("return $hOpen:" &$hOpen)
         ; Get connection handle
         $hConnect = _WinHttpConnect($hOpen, $sServerAddress, $INTERNET_DEFAULT_HTTPS_PORT)
         _writeLog("return $hConnect:" &$hConnect)
         _WinHttpSimpleSSLRequest($hConnect)
         _WinHttpCloseHandle($hConnect)

         ; build and fill the login form
         $sForm = _
        '<form action="' & $sServerAddress & $sGeneratorLocation & '" method="post">' & _
        '   <input name="userName" />' & _
        '   <input name="password" />' & _
        '</form>'
         $hConnect = $sForm

        $sReturned = _WinHttpSimpleFormFill($hConnect, $hOpen, _
        Default, _
        "name:userName", $userID, _
        "name:password", $password)
        If @error Then
            _writeLog("Error login: " & @error) ; ref. WinHttp.chm
            exitProgram(5)
        Else
             _writeLog("login successful, now download")

        $sFilePath = $downloadPath& "\" &$fileName
         ;eg: File path: C:\Users\Documents\auc_ucs\cluster01_01_202007162228
         _writeLog("File path: " & $sFilePath)
        $hConnect = _WinHttpConnect($hOpen, $sServerAddress)
        $hRequest = _WinHttpOpenRequest($hConnect, "GET", $sFilePath)
        _WinHttpSendRequest($hRequest)
        _WinHttpReceiveResponse($hRequest)
        If _WinHttpQueryDataAvailable($hRequest) Then

        $headers = _WinHttpQueryHeaders($hRequest)
        $Length = StringRegExpReplace($headers, '(?s).*Content-Length:\h*(\d+).*', "$1")
        Local $bChunk, $bData = Binary("")

        ProgressOn($fileName, "Downloading", "0 %")
        While 1
         $bChunk = _WinHttpReadData($hRequest, 2, 8192) ; binary
         If @error Then ExitLoop
         $bData &= $bChunk
         $percent = Int((BinaryLen($bData)/$Length)*100)
         ProgressSet($percent, "Downloading", $percent & " %")
        WEnd
     ProgressSet(100, "Done !", "100 %")
     FileWrite(@scriptdir & "\" & $fileName, $bData)
     _writeLog("Success to click Download")
     Sleep(1000)
     ProgressOff()
     Else
      ;_writeLog("connection error")
      _writeLog("Failed to click Download")
            exitProgram(5)
    EndIf

; close handles
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)
EndIf

But the login return 4 ; Connection problems

Does someone know what's wrong? The form only have 3 input; username, password and submit button.

Link to comment
Share on other sites

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
 Share

×
×
  • Create New...