Jump to content

WinHTTP functions


trancexx
 Share

Recommended Posts

I found that _WinHttpOpenRequest cause error (DllCall Failed) many times when i try to run this func more than 3 times so what should i do ? Exactly, when i test this au3, it success and no error, But after many hours, i run au3 again, it seem very bad.

#include <winhttp.au3>
cngoogle()
cngoogle()
cngoogle()

Func cngoogle()
Global $hOpen = _WinHttpOpen("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.9) Gecko/20100315 Firefox/2.0.0.14;MEGAUPLOAD 1.0")
If @error Then
    MsgBox(48, "Error", "Error initializing the usage of WinHTTP functions.")
    Exit 1
EndIf
; Get connection handle
Global $hConnect = _WinHttpConnect($hOpen, "google.com")
If @error Then
    MsgBox(48, "Error", "Error getting connection handle." & @CRLF & "Error number is " & @error)
Else
    ConsoleWrite("+ Connection handle $hConnect = " & $hConnect & @CRLF)
    MsgBox(64, "Yes!", "Handle is get! $hConnect = " & $hConnect)
EndIf


Global $hRequest = _WinHttpOpenRequest($hConnect, _
        "GET", _
        "/index.html", _
        Default, _
        "http://google.com/index.html", _
        "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
_WinHttpAddRequestHeaders($hRequest, "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.9) Gecko/20100315 Firefox/2.0.0.14;MEGAUPLOAD 1.0")
_WinHttpSendRequest($hRequest, _
        "Content-Type: application/x-www-form-urlencoded")
If @error Then
    MsgBox(48, "Error", "OPENREQ FAILED")
EndIf

; Wait for the response
_WinHttpReceiveResponse($hRequest)
Global $sHeader = _WinHttpQueryHeaders($hRequest)

msgbox(0,"",$sHeader)
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)
EndFunc
Edited by Idc
Link to comment
Share on other sites

hey guys, i am struggling with this code

i need to establish a cookie based connection to where i would like to set the cookie before i get the source so it is valid source

could someone please see what i am doing wrong and help me out here thanks in advance

#include "WinHttp.au3"
_Source()
Func _Source($In = "http://www.filesonic.com/file/897615481/Bibb...Final.part1.rar")

    Local $aUrl = _WinHttpCrackUrl($In)
    ; Post data:

    ; Initialize and get session handle
    Global $hOpen = _WinHttpOpen("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1) Gecko/20090624 Firefox/3.5")

    ; Get connection handle
    Global $hConnect = _WinHttpConnect($hOpen, $aUrl[2])
    ; Make a request
    Global $hRequest = _WinHttpOpenRequest($hConnect)

    ; Add header fields to the request
    _WinHttpAddRequestHeaders($hRequest, "Accept-Language: en-us,en;q=0.5")
    _WinHttpAddRequestHeaders($hRequest, "Content-Type: application/x-www-form-urlencoded")
    _WinHttpAddRequestHeaders($hRequest, "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7")
    _WinHttpAddRequestHeaders($hRequest, "Keep-Alive: 300")
    _WinHttpAddRequestHeaders($hRequest, "Connection: keep-alive")
    _WinHttpAddRequestHeaders($hRequest, "Cookie: PHPSESSID=SOMEVAKYEHERE;domain=.filesonic.com;path=/;expires=")

    $hRequest = _WinHttpOpenRequest($hConnect, -1, $aUrl[6] & $aUrl[7])
    _WinHttpSendRequest($hRequest)
    _WinHttpReceiveResponse($hRequest)
    Local $Ret = _WinHttpReadData($hRequest)

    _WinHttpCloseHandle($hRequest)
    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hOpen)

    Return ($Ret)
EndFunc   ;==>_Source
Link to comment
Share on other sites

  • 4 weeks later...

What's about WinHTTP in asyncronous mode?

I have this PHP on my server with a While cycle that lasts about 20'':

contentLoop.php:

<?php

session_id("lfun3t84nbtkep2gkk5plr2nn0");
session_start();

$_SESSION["timeOld"] =  getdate(); // time when contentLoop is launched

while ($_SESSION["loop"] == '1') {

    $_SESSION["count"] += 1;
    echo "im=".$_SESSION["count"] . "\n";

 
   $_SESSION["time"] =  getdate(); // I check to see how much time passed

    if ($_SESSION["time"]['seconds'] - $_SESSION["timeOld"]["seconds"] > 20  || $_SESSION["time"]['seconds'] - $_SESSION["timeOld"]['seconds'] < -40 ) {
        .......
        $_SESSION["loop"] = '0';
  }
   sleep(1);

} // end while

$_SESSION["exitloop"] = $_SESSION["time"]['seconds'] - $_SESSION["timeOld"]["seconds"];
echo "exit loop from contentLoop" . $_SESSION["exitloop"]. "\n"

I have another autoit script that calls another php every 5sec.

If I call the contentLoop.php syncronous both GET or POST, the other php blocks for 20 sec.

I think that if I call it asyncronous the first php script should continue his work without blocking, isn't it?

So I made this syncronous request:

#include <WinHTTP.au3>

$url = "/public/Files/PHP/contentLoop.php"

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

Global $hOpen = _WinHttpOpen()

_WinHttpSetStatusCallback($hOpen, $hWINHTTP_STATUS_CALLBACK)

Global $hConnect = _WinHttpConnect($hOpen, "www.myserver")


Global $hRequest = _WinHttpSimpleSendRequest($hConnect,"POST", $url,Default,$JSONdata,default)
Global $sRead = _WinHttpSimpleReadData($hRequest)
ConsoleWrite($sRead & @CRLF)


_WinHttpReceiveResponse($hRequest)
Global $sHeader
If _WinHttpQueryDataAvailable($hRequest) Then $sHeader = _WinHttpQueryHeaders($hRequest) ; ...get full header

 _WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

ConsoleWrite(@CRLF & $sHeader & @CRLF)

Sleep(2000)


DllCallbackFree($hWINHTTP_STATUS_CALLBACK)

; Define callback function
Func __WINHTTP_STATUS_CALLBACK($hInternet, $iContext, $iInternetStatus, $pStatusInformation, $iStatusInformationLength)
    #forceref $hInternet, $iContext, $pStatusInformation, $iStatusInformationLength
    ConsoleWrite("!->Current status of the connection: " & $iInternetStatus & " " & @TAB & " ")
    ; 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

and it works well with the headers and the echo of the script. But it blocks the other php script and for example the echo are sent all at the end of the 20 sec. I would like an output echo every loop of the while cycle.

So I tried the async way, changing this row:

global $hOpen = _WinHttpOpen()

into:

global $hOpen = _WinHttpOpen(Default,Default,Default,Default,$WINHTTP_FLAG_ASYNC)

This gives me the error: "An error occured while sending an HTTP request"

So how can I make an async call with WinHTTP?

Edited by frank10
Link to comment
Share on other sites

What's about WinHTTP in asyncronous mode?

I have this PHP on my server with a While cycle that lasts about 20'':

contentLoop.php:

<?php

session_id("lfun3t84nbtkep2gkk5plr2nn0");
session_start();

$_SESSION["timeOld"] =  getdate(); // time when contentLoop is launched

while ($_SESSION["loop"] == '1') {

    $_SESSION["count"] += 1;
    echo "im=".$_SESSION["count"] . "\n";

 
   $_SESSION["time"] =  getdate(); // I check to see how much time passed

    if ($_SESSION["time"]['seconds'] - $_SESSION["timeOld"]["seconds"] > 20  || $_SESSION["time"]['seconds'] - $_SESSION["timeOld"]['seconds'] < -40 ) {
        .......
        $_SESSION["loop"] = '0';
  }
   sleep(1);

} // end while

$_SESSION["exitloop"] = $_SESSION["time"]['seconds'] - $_SESSION["timeOld"]["seconds"];
echo "exit loop from contentLoop" . $_SESSION["exitloop"]. "\n"

I have another autoit script that calls another php every 5sec.

If I call the contentLoop.php syncronous both GET or POST, the other php blocks for 20 sec.

I think that if I call it asyncronous the first php script should continue his work without blocking, isn't it?

So I made this syncronous request:

#include <WinHTTP.au3>

$url = "/public/Files/PHP/contentLoop.php"

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

Global $hOpen = _WinHttpOpen()

_WinHttpSetStatusCallback($hOpen, $hWINHTTP_STATUS_CALLBACK)

Global $hConnect = _WinHttpConnect($hOpen, "www.myserver")


Global $hRequest = _WinHttpSimpleSendRequest($hConnect,"POST", $url,Default,$JSONdata,default)
Global $sRead = _WinHttpSimpleReadData($hRequest)
ConsoleWrite($sRead & @CRLF)


_WinHttpReceiveResponse($hRequest)
Global $sHeader
If _WinHttpQueryDataAvailable($hRequest) Then $sHeader = _WinHttpQueryHeaders($hRequest) ; ...get full header

 _WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

ConsoleWrite(@CRLF & $sHeader & @CRLF)

Sleep(2000)


DllCallbackFree($hWINHTTP_STATUS_CALLBACK)

; Define callback function
Func __WINHTTP_STATUS_CALLBACK($hInternet, $iContext, $iInternetStatus, $pStatusInformation, $iStatusInformationLength)
    #forceref $hInternet, $iContext, $pStatusInformation, $iStatusInformationLength
    ConsoleWrite("!->Current status of the connection: " & $iInternetStatus & " " & @TAB & " ")
    ; 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

and it works well with the headers and the echo of the script. But it blocks the other php script and for example the echo are sent all at the end of the 20 sec. I would like an output echo every loop of the while cycle.

So I tried the async way, changing this row:

global $hOpen = _WinHttpOpen()

into:

global $hOpen = _WinHttpOpen(Default,Default,Default,Default,$WINHTTP_FLAG_ASYNC)

This gives me the error: "An error occured while sending an HTTP request"

So how can I make an async call with WinHTTP?

You are doing it asynchronously.

Thing is you have to use callback for the job and not just to print status. What I'm saying is that your code should be inside __WINHTTP_STATUS_CALLBACK as response to specific InternetStatus value.

Do you comprehend?

Link to comment
Share on other sites

Do you know what WTF truck is?

--------.__
           |::WTF?::|[_I___,
           |_.-.____I__.-~;|
...BRUUMM...`(_)--------(_)"
-------------------------------------------

Use your head.

:huh2:
Link to comment
Share on other sites

You are doing it asynchronously.

Thing is you have to use callback for the job and not just to print status. What I'm saying is that your code should be inside __WINHTTP_STATUS_CALLBACK as response to specific InternetStatus value.

Do you comprehend?

I tried to insert some code into the callback with no luck...

These are the answers from a correct normal call, like I posted in the example before:

!->Current status of the connection: 1024      An HINTERNET handle has been created. 1024  -  1(Open & Connect)
!->Current status of the connection: 1024      An HINTERNET handle has been created. 1024  -  1(Open & Connect)
!->Current status of the connection: 1      Looking up the IP address of a server name.  1  -  2
!->Current status of the connection: 2      Successfully found the IP address of the server.  2  -   3
!->Current status of the connection: 4      Connecting to the server.   4  -   4
!->Current status of the connection: 8      Successfully connected to the server.   8   -  5
!->Current status of the connection: 16      Sending the information request to the server.   16   -   6
!->Current status of the connection: 32      Successfully sent the information request to the server.  32  -  7
!->Current status of the connection: 64      Waiting for the server to respond to a request.   64  - 8
!->Current status of the connection: 128      Successfully received a response from the server.   128   -   9
!->Current status of the connection: 64      Waiting for the server to respond to a request.   64  - 8
!->Current status of the connection: 128      Successfully received a response from the server.   128   -   9
!->Current status of the connection: 256      Closing the connection to the server   256  -   10
!->Current status of the connection: 512      Successfully closed the connection to the server.   512   -  11 

--------- Here goes my server's echo...

---------  Then it arrives the headers...:
HTTP/1.1 200 OK
Connection: close
Date: Fri, 03 Jun 2011 14:58:10 GMT
Content-Type: text/html
Server: Microsoft-IIS/6.0
MicrosoftOfficeWebServer: 5.0_Pub
X-Powered-By: ASP.NET

!->Current status of the connection: 2048      This handle value has been terminated.   2048   -  12
!->Current status of the connection: 2048      This handle value has been terminated.   2048   -  12
!->Current status of the connection: 2048      This handle value has been terminated.   2048   -  12

So I tried to insert some code into the callback func like this:

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

Global $hOpen = _WinHttpOpen()
_WinHttpSetStatusCallback($hOpen, $hWINHTTP_STATUS_CALLBACK)

Global $hConnect = _WinHttpConnect($hOpen, "www.deveritate.net")
Global $hRequest = _WinHttpSimpleSendRequest($hConnect,"POST", $url,Default,$JSONdata,default)

Exit

; Define callback function
Func __WINHTTP_STATUS_CALLBACK($hInternet, $iContext, $iInternetStatus, $pStatusInformation, $iStatusInformationLength)
    #forceref $hInternet, $iContext, $pStatusInformation, $iStatusInformationLength
    ConsoleWrite("!->Current status of the connection: " & $iInternetStatus & " " & @TAB & " ")
    ; Interpret the status
    Local $sStatus
    Switch $iInternetStatus
        Case $WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
            $sStatus = "An HINTERNET handle has been created. 1024  -  1°(both Open & Connect)"

        Case $WINHTTP_CALLBACK_STATUS_RESOLVING_NAME
            $sStatus = "Looking up the IP address of a server name.  1  -  2°"
        Case $WINHTTP_CALLBACK_STATUS_NAME_RESOLVED
            $sStatus = "Successfully found the IP address of the server.  2  -   3°"
        Case $WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER
            $sStatus = "Connecting to the server.   4  -   4°"
        Case $WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER
            $sStatus = "Successfully connected to the server.   8   -  5°"
        Case $WINHTTP_CALLBACK_STATUS_SENDING_REQUEST
            $sStatus = "Sending the information request to the server.   16   -   6°"
;~          Global $hRequest = _WinHttpSimpleSendRequest($hConnect,"POST", $url,Default,$JSONdata,default)

        Case $WINHTTP_CALLBACK_STATUS_REQUEST_SENT
            $sStatus = "Successfully sent the information request to the server.  32  -  7°"
            
        Case $WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE
            $sStatus = "Waiting for the server to respond to a request.   64  - 8°"
            
        Case $WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED
            $sStatus = "Successfully received a response from the server.   128   -   9°"
            
        Case $WINHTTP_CALLBACK_STATUS_CLOSING_CONNECTION
            $sStatus = "Closing the connection to the server   256  -   10°"
                _WinHttpCloseHandle($hRequest)
                _WinHttpCloseHandle($hConnect)
                _WinHttpCloseHandle($hOpen)
        Case $WINHTTP_CALLBACK_STATUS_CONNECTION_CLOSED
            $sStatus = "Successfully closed the connection to the server.   512   -  11°"


        Case $WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE
            $sStatus = "The response header has been received and is available with WinHttpQueryHeaders."
            Global $sHeader
            If _WinHttpQueryDataAvailable($hRequest) Then $sHeader =        _WinHttpQueryHeaders($hRequest) ; ...get full header
            ConsoleWrite(@CRLF & $sHeader & @CRLF)

        Case $WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE
            $sStatus = "Data is available to be retrieved with WinHttpReadData."
            Global $sRead = _WinHttpSimpleReadData($hRequest)
            ConsoleWrite($sRead & @CRLF)  ; prints echos


        Case $WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING
            $sStatus = "This handle value has been terminated.  2048   -  12°"
            
        Case $WINHTTP_CALLBACK_STATUS_INTERMEDIATE_RESPONSE
            $sStatus = "Received an intermediate (100 level) status code message from the server."
        Case $WINHTTP_CALLBACK_STATUS_READ_COMPLETE
            $sStatus = "Data was successfully read from the server."
        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_SECURE_FAILURE
            $sStatus = "One or more errors were encountered while retrieving a Secure Sockets Layer (SSL) certificate from 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

it gives me all the first lines up to :

!->Current status of the connection: 128 Successfully received a response from the server. 128 - 9°

with no echos nor headers, not close handles.

I tried to move Connect and SimpleRequest into callback too, but it doesn't start the connection.

Anyway, if I change

global $hOpen = _WinHttpOpen(Default,Default,Default,Default,$WINHTTP_FLAG_ASYNC)

that it should be the async way, It blocks at the very first lines...:

!->Current status of the connection: 1024 An HINTERNET handle has been created. 1024 - 1°(Open & Connect)

Do you have some code examples of async callback func or how would you change my code?

Link to comment
Share on other sites

I meant like this:

#include "WinHttp.au3"

Opt("MustDeclareVars", 1)


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

Global $hOpen = _WinHttpOpen(Default, Default, Default, Default, $WINHTTP_FLAG_ASYNC)

_WinHttpSetStatusCallback($hOpen, $hWINHTTP_STATUS_CALLBACK)

Global $hConnect = _WinHttpConnect($hOpen, "google.com")
Global $hRequest = _WinHttpOpenRequest($hConnect)

; Send it
_WinHttpSendRequest($hRequest)
ConsoleWrite("----------------------------------" & @CRLF)

; Wait for the response
_WinHttpReceiveResponse($hRequest)
ConsoleWrite("----------------------------------" & @CRLF)

MsgBox(64 + 262144, "Wait...", "...for the results if they are not shown already.")


_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("!->Current status of the connection: " & $iInternetStatus & " " & @TAB & " ")
    ; 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."

            ConsoleWrite($sStatus & @CRLF)
            ; This!
            ConsoleWrite(_WinHttpQueryHeaders($hInternet) & @CRLF)
            Return

        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

That helps?

Link to comment
Share on other sites

I'm gonna add function for asynchronous reading. It will make things much easier while in this mode.

... with just a touch inside _WinHttpReadData.

I just did something similar. Links are in the first post. The Help file and online version have example of usage for this new function too.

Current version of WinHttp UDF is 1.6.2.3.

Edited by trancexx
Link to comment
Share on other sites

I discovered what was the problem with my previous code not connecting in async:

the func

_WinHttpSimpleSendRequest($hConnect)

doesn't work with

_WinHttpOpen(Default, Default, Default, Default, $WINHTTP_FLAG_ASYNC)

but only with

Global $hOpen = _WinHttpOpen()

Maybe a bug ?

Anyway, using _WinHTTPSendRequest works.

But, I have some problems with your script:

It doesn't show the headers, because I need to make a pause between

_WinHttpSendRequest($hRequest)

Sleep(100)  

_WinHttpReceiveResponse($hRequest)

That way the headers show, without the little pause it shows nothing.

BUT if I put a slightly long pause such Sleep(500), it gives me:

If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
If @error Or Not ^ ERROR

and crash the program.

I added also:

ConsoleWrite( _WinHttpReadData($hRequest) & @CRLF)

in several positions in the callback but I don't receive the echos from the server.

Where should I put it?

EDIT:

I will look at the new func in first post...

Edited by frank10
Link to comment
Share on other sites

_WinHttpSimpleSendRequest calls _WinHttpReceiveResponse to wait for a response. If you use async-mode, you don't want to do that. Since async requires more advanced knowledge I think it is not necessary to support it in the simplified functions, just use the normal functions then :huh2:

Edited by ProgAndy

*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

Thank you trancexx, now the new code works very well! Very good!

The only thing is that in a php while cycle lasting 20sec, I echo something at every loop, but the results are printed only at the end of the 20 sec.

I think this is a problem with php...

Is it possible to achieve the echo every loop instead of all at the end?

Link to comment
Share on other sites

Since async requires more advanced knowledge I think it is not necessary to support it in the simplified functions, just use the normal functions then :huh2:

Yes, you are right, but if one doesn't know... Maybe a little info in the help, saying not to use with async.

Link to comment
Share on other sites

Yes, you are right, but if one doesn't know... Maybe a little info in the help, saying not to use with async.

That could be right.

Btw: Using asynchronous WinHttp is not recommended in AutoIt using DllCallbackRegister. The callback has to be threadsafe and must handle beeing called while its still running. AutoIt's implementation does not support that and as a result, crashes are likely to occur (I executed the script about 20 times and got 10 crashes, running stable and beta in x86 and x64 mode)

*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

That could be right.

Btw: Using asynchronous WinHttp is not recommended in AutoIt using DllCallbackRegister. The callback has to be threadsafe and must handle beeing called while its still running. AutoIt's implementation does not support that and as a result, crashes are likely to occur (I executed the script about 20 times and got 10 crashes, running stable and beta in x86 and x64 mode)

What script? The one from here or the one from the help file?

This one from here is bad on releasing.

Link to comment
Share on other sites

What script? The one from here or the one from the help file?

This one from here is bad on releasing.

Oh, forgot to mention it. I used the script from the helpfile with a stripped __WINHTTP_STATUS_CALLBACK. (Only set $sStaus, no function calls)

*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

Oh, forgot to mention it. I used the script from the helpfile with a stripped __WINHTTP_STATUS_CALLBACK. (Only set $sStaus, no function calls)

Can you give me the exact code? I can't make it crash here (win 7 x64) no matter what I do.

What system are you running the script on?

Edited by trancexx
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...