Jump to content

WinHTTP functions


trancexx
 Share

Recommended Posts

I could help you with the rest but I don't want to take that "Made it Ma! Top of the world!" feeling away from you.

I feel I've put all that I can into this.  If you want to help me out then I won't feel like a total loser.  If you want to help me then I will gladly accept your help.  Will you help with the rest?  I have nothing to pay you except for kindness.

Link to comment
Share on other sites

I feel I've put all that I can into this.  If you want to help me out then I won't feel like a total loser.  If you want to help me then I will gladly accept your help.  Will you help with the rest?  I have nothing to pay you except for kindness.

I don't get that last sentence. What does it mean?

Just add errors checks and missing function:

Func _imgur_uploadur(Const $image_file)
    Local $hFile = FileOpen($image_file, 16)
    Local $bData = FileRead($hFile)
    FileClose($hFile)

    Local Const $sClientID = "3fda9b4a8bc150c" ; your client-id

    Local Const $hOpen = _WinHttpOpen()
    _WinHttpSetTimeouts($hOpen, 10000, 60000, 90000, 90000) ; see, you can do it here too

    Local Const $hConnect = _WinHttpConnect($hOpen, "https://api.imgur.com")

    Local $sReceivedData = _WinHttpSimpleSSLRequest($hConnect, "POST", "/3/upload.xml", Default, "image=" & Base64Encode($bData), "Content-Type: application/x-www-form-urlencoded" & @CRLF & "Authorization: Client-ID " & $sClientID)

    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hOpen)

    Return $sReceivedData
EndFunc

That's the same thing as that VB example that you showed as far as HTTP goes.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

It means if you want to help me then I can't pay you in money.  All I have is kindness.  Do you accept kindness rain checks?  XD

But seriously, thank you. 

Two good days in a row for the internet.

 

Link to comment
Share on other sites

Function _WinHttpSimpleSendRequest in function _WinHttpSimpleSendSSLRequest throws an error 3.  I don't have nor could I figure out the dll.  Any hints on what that means?

Edited by jaberwacky
Link to comment
Share on other sites

 

It means if you want to help me then I can't pay you in money.  All I have is kindness.  Do you accept kindness rain checks?  XD

But seriously, thank you. 

Two good days in a row for the internet.

 

I suspected it'd be something like that. My English level made me have doubts though. If someone says: "I have nothing to pay you except for kindness." I read that as if they owe me nothing for anything I've done except for being kind, which is kinda opposite of what you said it really means. Silly me, right?

I have nothing to pay you with except ... - would make me have no doubts.

Edited by trancexx

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Why? Base64 is Base64.

Func Base64Encode($vInput)
    Local Const $CRYPT_STRING_BASE64 = 0x00000001

    $vInput = Binary($vInput)
    Local $tInput = DllStructCreate("byte[" & BinaryLen($vInput) & "]")
    DllStructSetData($tInput, 1, $vInput)

    Local $aCall = DllCall("Crypt32.dll", "bool", "CryptBinaryToStringA", _
            "struct*", $tInput, _
            "dword", DllStructGetSize($tInput), _
            "dword", $CRYPT_STRING_BASE64, _
            "ptr", 0, _
            "dword*", 0)
    If @error Or Not $aCall[0] Then Return SetError(1, 0, "") ; error calculating the length of the needed buffer

    Local $tOut = DllStructCreate("char[" & $aCall[5] & "]")

    $aCall = DllCall("Crypt32.dll", "int", "CryptBinaryToStringA", _
            "struct*", $tInput, _
            "dword", DllStructGetSize($tInput), _
            "dword", $CRYPT_STRING_BASE64, _
            "struct*", $tOut, _
            "dword*", DllStructGetSize($tOut))
    If @error Or Not $aCall[0] Then Return SetError(2, 0, ""); error encoding

    Return DllStructGetData($tOut, 1)
EndFunc

 

Edited by trancexx

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Every file.  I think it connects to the server because this is the message I get:

<?xml version="1.0" encoding="utf-8"?>
<data type="array" success="0" status="400"><error>Image format not supported, or image is corrupt.</error><request>/3/upload.xml</request><method>POST</method></data>

 

Link to comment
Share on other sites

No, scratch that.  I must have reverted to using your other function with the _WinHttpSimpleFormFill.  Which would be great but I don't see a way to get progress updates.

Link to comment
Share on other sites

No, scratch that.  I must have reverted to using your other function with the _WinHttpSimpleFormFill.  Which would be great but I don't see a way to get progress updates.

What progress updates?
How does base64 method offers ability to do that?

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

I meant, well, I was talking about two different things.

So, the error message that I posted earlier is still there.

 

The progress updates, I meant that using the WinHttpSimpleFormFill method doesn't allow me to get progress updates from the imgur server.  Other than that the function uploads just fine!

Link to comment
Share on other sites

But you never get progress updates from the server. You've lost me now.

If you want detailed report of the whole process there is option to use callback function. Like this maybe:

#include "WinHttp.au3"

Const $sClientID = "555555555555555" ; your client-id
Const $sAddress = "https://api.imgur.com/3/upload.xml" ; the address of the target (https or http, makes no difference - handled automatically)
Const $sForm = _
        '<form action="' & $sAddress & '" method="post" enctype="multipart/form-data">' & _
        ' <input type="file" name="image"/>' & _ ;
        '</form>'

$sFileToUpload = FileOpenDialog("Open", "", "Images (*.jpg;*.gif;*.png;*.bmp)")

; Initialize and get session handle
$hOpen = _WinHttpOpen()

Const $hWINHTTP_STATUS_CALLBACK = DllCallbackRegister(__WINHTTP_STATUS_CALLBACK, "none", "handle;dword_ptr;dword;ptr;dword")

; Associate callback function with session
_WinHttpSetStatusCallback($hOpen, $hWINHTTP_STATUS_CALLBACK)

$hConnect = $sForm ; will pass form as string so this is for coding correctness because $hConnect goes in byref

; Fill form
$sReturned = _WinHttpSimpleFormFill($hConnect, $hOpen, _
        Default, _
        "name:image", $sFileToUpload, _
        "Authorization: Client-ID " & $sClientID)

If @error Then
    MsgBox(4096, "Error", "Error number = " & @error)
Else
    ConsoleWrite(@CRLF & $sReturned & @CRLF)
EndIf



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

DllCallbackFree($hWINHTTP_STATUS_CALLBACK)


; Define callback function
Func __WINHTTP_STATUS_CALLBACK($hInternet, $iContext, $iInternetStatus, $pStatusInformation, $iStatusInformationLength)
    #forceref $hInternet, $iContext, $pStatusInformation, $iStatusInformationLength

    ConsoleWrite("--> ")
    ; Interpret the status
    Local $sStatus

    Switch $iInternetStatus

        Case $WINHTTP_CALLBACK_STATUS_CLOSING_CONNECTION
            $sStatus = "Closing the connection to the server"
        Case $WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER
            $sStatus = "Successfully connected to the server."
        Case $WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER
            $sStatus = "Connecting to the server."
        Case $WINHTTP_CALLBACK_STATUS_CONNECTION_CLOSED
            $sStatus = "Successfully closed the connection to the server."
        Case $WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE
            $sStatus = "Data is available to be retrieved with WinHttpReadData."
        Case $WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
            $sStatus = "An HINTERNET handle has been created."
        Case $WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING
            $sStatus = "Handle has been terminated."
        Case $WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE
            $sStatus = "The response header has been received and is available with WinHttpQueryHeaders."
        Case $WINHTTP_CALLBACK_STATUS_INTERMEDIATE_RESPONSE
            $sStatus = "Received an intermediate (100 level) status code message from the server."
        Case $WINHTTP_CALLBACK_STATUS_NAME_RESOLVED
            $sStatus = "Successfully found the IP address of the server."
        Case $WINHTTP_CALLBACK_STATUS_READ_COMPLETE
            $sStatus = "Data was successfully read from the server."
        Case $WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE
            $sStatus = "Waiting for the server to respond to a request."
        Case $WINHTTP_CALLBACK_STATUS_REDIRECT
            $sStatus = "An HTTP request is about to automatically redirect the request."
        Case $WINHTTP_CALLBACK_STATUS_REQUEST_ERROR
            $sStatus = "An error occurred while sending an HTTP request."
        Case $WINHTTP_CALLBACK_STATUS_REQUEST_SENT
            $sStatus = "Successfully sent the information request to the server."
        Case $WINHTTP_CALLBACK_STATUS_RESOLVING_NAME
            $sStatus = "Looking up the IP address of a server name."
        Case $WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED
            $sStatus = "Successfully received a response from the server."
        Case $WINHTTP_CALLBACK_STATUS_SECURE_FAILURE
            $sStatus = "One or more errors were encountered while retrieving a Secure Sockets Layer (SSL) certificate from the server."
        Case $WINHTTP_CALLBACK_STATUS_SENDING_REQUEST
            $sStatus = "Sending the information request to the server."
        Case $WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE
            $sStatus = "The request completed successfully."
        Case $WINHTTP_CALLBACK_STATUS_WRITE_COMPLETE
            $sStatus = "Data was successfully written to the server."
    EndSwitch

    ; Print it
    ConsoleWrite($sStatus & @CRLF)
EndFunc

 

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Thanks for that info.

I think I will just stick with your _WinHttpSimpleFormFill example.  You know I'd do this on my own if I could.  I hate to be a bother.

 

Thank you for what you've provided.  I can't thank you enough!

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...