Jump to content

WinHTTP functions


trancexx
 Share

Recommended Posts

Could any other conclusion be derived?

I have no intention of helping if you have no intention of listening. It gets boring then.

Right...um, I've been tweaking the code I posted earlier, trying to figure out why it can't access "127.0.0.1". I've got nothing that works, but I noticed that no other AutoIt function seems to be able to access it either... Edited by motionman95
Link to comment
Share on other sites

I modified _WinHTTPSetOption(Ex), so it returns an error if called with an unsupported option. As a result _WinHTTPGetOptionType was removed. Using StringLen for WSTR ist valid.

I meant StringLen($sType).

I'll send you new WinHTTP.au3 for you to review it (whether you like it or not :blink: ).

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

I meant StringLen($sType).

I'll send you new WinHTTP.au3 for you to review it (whether you like it or not :blink: ).

Ohh ^^ I think I just went blind.

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Whenever i try to download an image i get like double the size of the image and is not really writing the image right.

I checked my http traffic through a tool and I am receiving my the correct data I just need to know how to save it to a image.

Func _GETRequest($domain, $file)
    Local $hOpen, $hConnect, $hRequest
    $hOpen = _WinHttpOpen()
    $hConnect = _WinHttpConnect($hOpen, $domain)
    $hRequest = _WinHttpOpenRequest($hConnect, "GET", $file)
    _WinHttpAddRequestHeaders($hRequest, "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6")
    _WinHttpAddRequestHeaders($hRequest, "Content-Type: application/x-www-form-urlencoded")
    _WinHttpAddRequestHeaders($hRequest, "Connection: Keep-Alive")

    _WinHttpSendRequest($hRequest, $WINHTTP_NO_ADDITIONAL_HEADERS)
    _WinHttpReceiveResponse($hRequest)

 Local $aReturn[2]= ['', '']
    If _WinHttpQueryDataAvailable($hRequest) Then
        Do

            $aReturn[1] &= _WinHttpReadData($hRequest,2)
            
        Until 0=@extended

    EndIf
    $aReturn[0] = _WinHttpQueryHeaders($hRequest)
    _WinHttpCloseHandle($hRequest)
    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hOpen)

    Return $aReturn ; Array: [0]: headers, [1]: received data.

EndFunc

$n = _GETRequest("www.scheduletwitterposts.com","/images/money_tree.jpg")
$ffi = FileOpen("c:\images\test2.jpg",10)
FileWrite($ffi,$n[1])
FileClose($ffi)
Link to comment
Share on other sites

If you want to download binary, you have to init $aReturn[1] with Binary('') instead of ''.

PS: there should be a new version with some new functions to simplify requests soon.

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

  • 2 weeks later...

Since you guys are about to release a new version soon,

I was wondering if it wouldn't speed up things slightly if you wouldn't

call SetError without actually setting @error. Like this: Return SetError(0, 0, 1)

Why not simply 'Return 1'?

Also, why not using SetExtended instead of things like SetError(0, $a_iCall[4], StringLeft(DllStructGetData($tBuffer, 1), $a_iCall[4]))?

However, thanks for a great script and for all your support.

I learned quite a bit reading this complete thread again.

EDIT: One other thing I just noticed: DllOpen("winhttp.dll") in the top of WinHTTP.au3 is totally useless, isn't it?

Is it there to remind us users that we could open it in the beginning of a section where winhttp functions are used,

and then using the handle (therefore having to change all the functions manually) but having to close the handle manually, or why is it there?

I see that it makes sense to use the filename in every dllcall from every function for scripts where you throughout the script re-use winhttp functions, but in case there's only one section where let's say some website sources are gathered and later processed, it's not a good practice to re-open it over and over, right?

Edited by Mojo
You can fool some of the people all of the time, and all of the people some of the time, but you can not fool all of the people all of the time. Abraham Lincoln - http://www.ae911truth.org/ - http://www.freedocumentaries.org/
Link to comment
Share on other sites

Since you guys are about to release a new version soon,

I was wondering if it wouldn't speed up things slightly if you wouldn't

call SetError without actually setting @error. Like this: Return SetError(0, 0, 1)

Why not simply 'Return 1'?

Also, why not using SetExtended instead of things like SetError(0, $a_iCall[4], StringLeft(DllStructGetData($tBuffer, 1), $a_iCall[4]))?

However, thanks for a great script and for all your support.

I learned quite a bit reading this complete thread again.

No it wouldn't.

But you are right, and we are. New version uses your syntax.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

EDIT: One other thing I just noticed: DllOpen("winhttp.dll") in the top of WinHTTP.au3 is totally useless, isn't it?

Is it there to remind us users that we could open it in the beginning of a section where winhttp functions are used,

and then using the handle (therefore having to change all the functions manually) but having to close the handle manually, or why is it there?

I see that it makes sense to use the filename in every dllcall from every function for scripts where you throughout the script re-use winhttp functions, but in case there's only one section where let's say some website sources are gathered and later processed, it's not a good practice to re-open it over and over, right?

I's not useless. DllOpen("winhttp.dll") means reference count for winhttp.dll will never reach 0. This is very important when working with this dll. Unloading that module prematurely could lead to unexpected behavior. This way other calls by string will only temporarily increase ref count by 1. Dll is not unloaded.

Calling by string (dll name) instead by pseudo-handle is/was done to avoid adding global variable from within UDF, since AutoIt had bug with releasing dlls in some special cases. Declaring it as Global Constant and with bug fixed, new version uses pseudo-handle.

Just wait a little bit more. It's almost finished, just polishing it now.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

I's not useless. DllOpen("winhttp.dll") means reference count for winhttp.dll will never reach 0. This is very important when working with this dll. Unloading that module prematurely could lead to unexpected behavior. This way other calls by string will only temporarily increase ref count by 1. Dll is not unloaded.

Calling by string (dll name) instead by pseudo-handle is/was done to avoid adding global variable from within UDF, since AutoIt had bug with releasing dlls in some special cases. Declaring it as Global Constant and with bug fixed, new version uses pseudo-handle.

Just wait a little bit more. It's almost finished, just polishing it now.

Hi trancexx,

thanks for the explanations. I knew there had to be a reason you put it there.

I'm looking forward to the new version.

Have a nice day

You can fool some of the people all of the time, and all of the people some of the time, but you can not fool all of the people all of the time. Abraham Lincoln - http://www.ae911truth.org/ - http://www.freedocumentaries.org/
Link to comment
Share on other sites

does anyone have any example of how I can use this to simply upload a file from the computer to my server? I have made a php script that can accept files via an http post by reading from $_FILES array. Now I just need to get the file to upload to my script from autoit and this thread is the closest I can find to do that without using a browser object. I'm really hoping for a somewhat simple solution.

Link to comment
Share on other sites

Are there any script breaking changes from your last version? And thanks for keeping this updated, I find it quite useful! The new site and help file are very helpful as well.

There shouldn't be. I would have mentioned it if I was aware of some. You have non-working scripts after the update?

Only thing that I know that could cause any problems is one changed constant (from empty string to 0) and only because I haven't find needed to test it additionally.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Filling forms is almost the most important part of web manipulation. It was always done through IE because there it's easy to do something like this:

;...
$oDocument = $oIE.document
$oForm = $odocument.GetElementsByName("FormName")
$oInput = $oForm.GetElementsByName("InputName")
$oInput.value = "Something"
$oSubmit = $odocument.GetElementsByName("SearchOrWhatever")
$oSubmit.click
;...

Here's an example how to fill form with WinHttp.au3:

#include "WinHttp.au3"

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

;    1. Open w3schools forms page (http://www.w3schools.com/html/html_forms.asp)
;    2. Fill form on that page with these values/conditins:
;         - form is to be identifide by its name, Name is "input0"
;         - set "OMG!!!" data to input box. Locate input box by its name. Name is "user"
;         - gather data

; Get connection handle
Global $hConnect = _WinHttpConnect($hOpen, "w3schools.com")

; Fill form on this page
Global $sRead = _WinHttpSimpleFormFill($hConnect, "html/html_forms.asp", "name:input0", "name:user", "OMG!!!")

; Close connection handle
_WinHttpCloseHandle($hConnect)
; Close session handle now that's no longer needed
_WinHttpCloseHandle($hOpen)


If $sRead Then
    MsgBox(64 + 262144, "Done!", "Will open returned page in your default browser now." & @CRLF & _
            "You should see 'OMG!!!' somewhere on that page.")
    Global $sFileHTM = @ScriptDir & "\Form.htm"
    Global $hFileHTM = FileOpen($sFileHTM, 2)
    FileWrite($hFileHTM, $sRead)
    FileClose($hFileHTM)
    ShellExecuteWait($sFileHTM)
EndIff

Forms are accessible thru Id, name or index.

Form fields are accessible by their Id or name.

You can use it to login, read your web mail, post to favorite forum, whatever. All that IE-free.

This is new function together with all other Simple functions. They can be used to simplify all tasks done with this UDF.

Bw, last version is 1.6.1.5.

Edited by trancexx

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

It is possible. Example as attachment ;)

@trancexx: You should set @error -1 in _WinHTTPReadData when return is empty (like FileRead)

If Not $a_iCall[4] Then Return SetError(-1,0,"")

Also, sometimes it is required to send binary Data (e.g. if you send images like in attached example)

when i try to run this i get this error

C:\Documents and Settings\Erik\My Documents\Downloads\POST_WinHTTP.au3(216,49) : ERROR: syntax error

Local $szExt = StringLower(StringRegExpReplace(,

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Documents and Settings\Erik\My Documents\Downloads\POST_WinHTTP.au3 - 1 error(s), 0 warning(s)

Whats wrong with the code , am using v3.3.6.1

Link to comment
Share on other sites

when i try to run this i get this error

C:\Documents and Settings\Erik\My Documents\Downloads\POST_WinHTTP.au3(216,49) : ERROR: syntax error

Local $szExt = StringLower(StringRegExpReplace(,

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Documents and Settings\Erik\My Documents\Downloads\POST_WinHTTP.au3 - 1 error(s), 0 warning(s)

Whats wrong with the code , am using v3.3.6.1

You have syntax error in your script. Line 219, character 49 (counted from the left). That's trivial, regardless of the cause for the issue. Why do you ask that kind of questions?

There was some changes since the times the script you are using was posted. What is it you want to do?

It can probably be done differently (better maybe) now.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Here's an example how to upload file using WinHttp.au3. I've added event monitoring to (obvious follows) monitor events.

#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6

#include "WinHttp.au3"

Opt("MustDeclareVars", 1)

;=====================================================================
; Page and Form info
; http://www.cs.tut.fi/~jkorpela/forms/file.html
Global Const $sAddress = "www.cs.tut.fi"
Global Const $sPage = "~jkorpela/forms/file.html"
Global Const $sFormId = "index:0" ;<- meaning first form on the page
;=====================================================================

; Register Callback function to monitor events
Global $hWINHTTP_STATUS_CALLBACK = DllCallbackRegister("__WINHTTP_STATUS_CALLBACK", "none", "handle;dword_ptr;dword;ptr;dword")

; Initialize and get session handle (impersonating Chrome browser)
Global $hOpen = _WinHttpOpen("Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.127 Safari/533.4")

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

; Get connection handle
Global $hConnect = _WinHttpConnect($hOpen, $sAddress)
Global $sHTM = _WinHttpSimpleFormFill($hConnect, $sPage, $sFormId, _
        "name:datafile", @ScriptFullPath, _
        "txt", "Private dancer.")

ConsoleWrite(">Server response:" & @CRLF & $sHTM & @CRLF)

_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)



; 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 = "This handle value 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   ;==>__WINHTTP_STATUS_CALLBACK

You need WinHttp.au3 1.6.1.6. for that.

♡♡♡

.

eMyvnE

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...