Upload via _WinHttp auf HTTPS Server

  • Hallo zusammen

    Vor kurzem habe ich mit Eurer Hilfe das nachfolgende Script erstellen, können, welches von einem HTTPS Server Dateien herunter laden kann.
    Nun müsste ich das ändern, und auch Dateien hochladen können.
    Leider bin ich bisher gescheitert.

    Kann mir bitte jemand helfen?

    Download File from HTTPS Server
    [autoit]

    Func _ConnectToUpdateServer($sFileToDownload, $sLocalFilePath)
    Local Const $sUserAgent = "XYZ"
    Local Const $sFullServerName = "https://www.firma.com"
    Local Const $sServerName = "www.firma.com"
    Local Const $sUsername = _StringEncrypt(0, "XYZ", "XYZ", 2)
    Local Const $sPassword = _StringEncrypt(0, "XYZ", "XYZ", 2)
    Local Const $sFolder = "/Verzeichnis/Log/"
    Local Const $sTmpFile = @AppDataCommonDir & "\XYZ\~TmpFile"
    Local Const $sTextContent = "text/"
    Local $hSession, $hConnect, $hRequest
    Local $sData, $hFile, $TotalBytes, $ReadBytes = 0
    Local $sContentType

    [/autoit] [autoit][/autoit] [autoit]

    $hSession = _WinHttpOpen($sUserAgent, $WINHTTP_ACCESS_TYPE_DEFAULT_PROXY)
    If @error Then Return SetError(1, 1, 0) ;Could not initialize HTTP Functions
    _WinHttpSetTimeouts($hSession, 100, 10000, 5000, 5000)
    If @error Then
    _WinHttpCloseHandle($hSession)
    Return SetError(1, 2, 0) ;Could not set timeouts
    EndIf
    $hConnect = _WinHttpConnect($hSession, $sServerName, $INTERNET_DEFAULT_HTTPS_PORT)
    If @error Then
    _WinHttpCloseHandle($hSession)
    Return SetError(1, 3, 0) ;Could not connect to Server
    EndIf
    $hRequest = _WinHttpOpenRequest($hConnect, "GET", $sFolder & $sFileToDownload, Default, $sFullServerName, "*/*", $WINHTTP_FLAG_SECURE)
    If @error Then
    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hSession)
    Return SetError(1, 4, 0) ;Could not create a Http request
    EndIf
    _WinHttpSetOption($hRequest, $WINHTTP_OPTION_SECURITY_FLAGS, BitOr($SECURITY_FLAG_IGNORE_CERT_CN_INVALID, $SECURITY_FLAG_IGNORE_CERT_DATE_INVALID, $SECURITY_FLAG_IGNORE_UNKNOWN_CA))
    If @error Then
    _WinHttpCloseHandle($hRequest)
    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hSession)
    Return SetError(1, 5, 0) ;Could not set required options
    EndIf
    _WinHttpSetOption($hRequest, $WINHTTP_OPTION_USERNAME, $sUsername)
    If @error Then
    _WinHttpCloseHandle($hRequest)
    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hSession)
    Return SetError(1, 6, 0) ;Could not set required options
    EndIf
    _WinHttpSetOption($hRequest, $WINHTTP_OPTION_PASSWORD, $sPassword)
    If @error Then
    _WinHttpCloseHandle($hRequest)
    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hSession)
    Return SetError(1, 7, 0) ;Could not set required options
    EndIf
    _WinHttpSendRequest($hRequest)
    If @error Then
    _WinHttpCloseHandle($hRequest)
    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hSession)
    Return SetError(1, 8, 0) ;Could not send Http request
    EndIf
    _WinHttpReceiveResponse($hRequest)
    If @error Then
    _WinHttpCloseHandle($hRequest)
    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hSession)
    Return SetError(1, 9, 0) ;No Http Receive Response
    EndIf
    If _WinHttpQueryDataAvailable($hRequest) Then ;Data available => Read
    $TotalBytes = Number(_WinHttpQueryHeaders($hRequest, $WINHTTP_QUERY_CONTENT_LENGTH))
    If $TotalBytes = 0 Or $TotalBytes = "" Then $TotalBytes = 5000000
    $sContentType = _WinHttpQueryHeaders($hRequest, $WINHTTP_QUERY_CONTENT_TYPE)
    $hFile = FileOpen($sTmpFile, 10)
    While True
    $sData = _WinHttpReadData($hRequest, 2) ;Read in Binary mode
    $ReadBytes += @extended
    If @error Then ExitLoop
    ;_SetProgress($ReadBytes * 100 / $TotalBytes, $sFileToDownload, $NrActualFile, $NrOfFiles)
    FileWrite($hFile, BinaryToString($sData))
    WEnd
    FileFlush($hFile)
    FileClose($hFile)
    Else
    _WinHttpCloseHandle($hRequest)
    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hSession)
    Return SetError(1, 10, 0) ;No Data available for reading
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    If StringInStr($sContentType, $sTextContent) Then ;If content is plain text, then check if it could be an error!
    $hFile = FileOpen($sTmpFile, 0)
    $sData = FileRead($hFile)
    FileClose($hFile)
    If StringInStr($sData, "<title>401 Authorization Required</title>") Or StringInStr($sData, "You don't have permission to access") Then
    FileDelete($sTmpFile)
    _WinHttpCloseHandle($hRequest)
    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hSession)
    Return SetError(2, 0, 0) ;Wrong password / no permissions
    EndIf
    If StringInStr($sData, "<!DOCTYPE html PUBLIC") Then
    FileDelete($sTmpFile)
    _WinHttpCloseHandle($hRequest)
    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hSession)
    Return SetError(3, 0, 0) ;File not found
    EndIf
    EndIf
    _WinHttpCloseHandle($hRequest)
    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hSession)
    FileMove($sTmpFile, $sLocalFilePath & "\" & $sFileToDownload, 9)
    Return 1
    EndFunc ;==>_ConnectToUpdateServer

    [/autoit]

    Irgendwie müsste ich bei _WinHttpOpenRequest anstelle von GET ein POST machen. Und dann vermutlich den neuen Dateinamen angeben.
    Und evtl. irgendwie die Funktion _WinHttpWriteData benutzen.

    Leider klappt das bei mir nicht.

    Upload...
    [autoit]

    $hSession = _WinHttpOpen($sUserAgent, $WINHTTP_ACCESS_TYPE_DEFAULT_PROXY)
    $hConnect = _WinHttpConnect($hSession, $sServerName, $INTERNET_DEFAULT_HTTPS_PORT)
    $hRequest = _WinHttpOpenRequest($hConnect, "POST", "Testdatei.txt", Default, $sFullServerName, "*/*", $WINHTTP_FLAG_SECURE)
    _WinHttpSetOption($hRequest, $WINHTTP_OPTION_SECURITY_FLAGS, BitOr($SECURITY_FLAG_IGNORE_CERT_CN_INVALID, $SECURITY_FLAG_IGNORE_CERT_DATE_INVALID, $SECURITY_FLAG_IGNORE_UNKNOWN_CA))
    _WinHttpSetOption($hRequest, $WINHTTP_OPTION_USERNAME, $sUsername)
    _WinHttpSetOption($hRequest, $WINHTTP_OPTION_PASSWORD, $sPassword)
    _WinHttpSendRequest($hRequest)
    _WinHttpWriteData($hRequest, "test")

    [/autoit]


    Ich bekomme zwar keine Fehlermeldungen zurück (die @error Auswertung war schon mal eingebaut gewesen :) ), aber es wird auch keine Datei mit dem Inhalt "test" erstellt.

    Vielen Dank für Eure Hilfe!
    Veronesi