Jump to content

Problem with Winhttp readystate value


Recommended Posts

Hi

I have the following code which does an async http post:

$oHTTP[$i] = ObjCreate("WinHttp.WinHttpRequest.5.1")

    $oHTTP.Open("POST", $sURL, TRUE)
    If (@error) Then Return SetError(1, 0, 0)

    $oHTTP.SetRequestHeader ("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0")
    $oHTTP.Send($sData)
    If (@error) Then Return SetError(2, 0, 0)

    ;...some other processing...

    HttpWait($HTTP)

When I call the function httpwait() to wait for the response and see what it is I don't get any value for readystate.

status however comes back quickly with a 200 but the request isn't fully complete as part of its header content is missing.

If I change the original post to synchronous (by changing the open parameter to FALSE: $oHTTP.Open("POST", $sURL, FALSE)) then it all works fine and returns the full header content.

Something is not set for asynchronous processing but I'm not sure what.

Func HttpWait($oHTTP)
    Local $i = 0

    Do
        Sleep(25)
        $i+= 1
    Until $oHttp.read == 4 And $oHTTP.status == 200 Or $i > 100

    ConsoleWrite("readystate "&$oHTTP.ReadyState & " status " & $oHTTP.Status & @CRLF)
EndFunc

Any thoughts what I'm doing wrong?

Edited by delstone123
Link to comment
Share on other sites

Forum rules dictate to wait for a 24-hour delay before bumping your thread.

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

It appears you are mixing object property names between:

WinHttpRequest.5.1 --and-- InternetExplorer.Application.

There is no such property as "Read" or "ReadyState"

in the WinHttpRequest.5.1 object.

Those are in the InternetExplorer.Application object.

Here's the list of Methods and Properties for the WinHttpRequest.5.1 object.

http://msdn.microsoft.com/en-us/library/windows/desktop/aa384106(v=vs.85).aspx

Here's something to play with...

Func _WinHttpRequest($sURL, $sData = '')
    Local $oHTTP = ObjCreate('WinHttp.WinHttpRequest.5.1')

    $oHTTP.Open('HEAD', $sURL, True)
    If @error Then Return SetError(1, 0, 0)

    $oHTTP.SetRequestHeader('User-Agent', 'http_requester/0.1')

    $oHTTP.Send($sData)
    If @error Then Return SetError(2, 0, 0)

    ;$oHTTP.WaitForResponse(5); <-- seconds

    ; -- Or --

    For $i = 1 To 100
        If $oHTTP.status == 200 Then ExitLoop
        Sleep(25)
    Next

    MsgBox(0, 'Loops: ' & $i & '  -  Status: ' & $oHTTP.Status, $oHTTP.GetAllResponseHeaders)
EndFunc

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

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