Jump to content

How to get HttpRequest 's Return value


Recommended Posts

in msdn :https://msdn.microsoft.com/en-us/library/windows/desktop/aa384045(v=vs.85).aspx

it says:

msdn.jpg

 

I want to how how can i get the Return value (S_OK  or  error value )

here is my codes as follow:

Local $post_data = '123'
        Local $post_url = 'http://127.0.0.1/test.php'
    
        Local $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1")
        $oHTTP.Open("POST", $post_url, True)
    
    Local $Return_Value = $oHTTP.Send($post_data)
    
        $oHTTP.WaitForResponse(-1)
        Local $res = $oHTTP.responsetext

    
    MsgBox(0,'$Return_Value',$Return_Value);IT shows nothing ;why  ?  how can i get S_OK or error value ?

 

Link to comment
Share on other sites

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

I think you are referring to the status property.  In your example, it would be "$oHTTP.Status".  If you want the text, it would be "$oHTTP.StatusText".

If you actually are looking to see if there was an error, then you could capture the OnError event and use the parameters that are passed to it.  Or you could implement a COM error handler to handle the error.

 

Edited by TheXman
Link to comment
Share on other sites

21 minutes ago, TheXman said:

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

I think you are referring to the status property.  In your example, it would be "$oHTTP.Status".  If you want the text, it would be "$oHTTP.StatusText".

 

how ever, as msdn says:

Status property:

The results of this property are valid only after the Send method has successfully completed. 

 

SO ,  i still can't get the return value if the Send method has been failed

Link to comment
Share on other sites

I'm not quite sure what you are trying to accomplish but sometimes a working example is worth a 1000 words.  You can play with the example by forcing errors and see what you get.  You can also display values to see what they return.

 

The example below just does a HTTPS GET to Google.  If you change the URL to something that doesn't exist, you can see how I trap errors.

 

http_example()


;==========================================================================
;
;==========================================================================
Func http_example()

    Local $oHttp       = Null, _
          $oCommErr    = Null


    ;Register COM Error Handler
    $oCommErr = ObjEvent("AutoIt.Error", com_error_handler)
    If @error Then
        ConsoleWrite("Unable to register COM error handler" & @CRLF)
        Return SetError(-1, 0, False)
    EndIf


    ;Create a HTTP COM object
    $oHttp = ObjCreate("winhttp.winhttprequest.5.1")
    If @error Then
        ConsoleWrite("Unable to create HTTP object" & @CRLF)
        Return SetError(-1, 0, False)
    EndIf

    With $oHttp
        ;Open GET request and send the request synchronous
        .Open("GET", "https://www.google.com")
        If @error Then
            ConsoleWrite(StringFormat("ERROR: (0x%X) %s", $oCommErr.RetCode, $oCommErr.Description) & @CRLF)
            Return SetError(-1, 0, False)
        EndIf
        Sleep(500)

        ;Send request
        .Send()
        If @error Then
            ConsoleWrite(StringFormat("ERROR: (0x%X) %s", $oCommErr.RetCode, $oCommErr.Description) & @CRLF)
            Return SetError(-1, 0, False)
        EndIf

        If .Status <> 200 Then
            ConsoleWrite(StringFormat("ERROR: HTTP Status Code = (%s) %s", .Status, .StatusText) & @CRLF)
            Return SetError(-1, 0, False)
        EndIf
    EndWith ;$oHTTP


    ;All was good
    ConsoleWrite("All is good" & @CRLF)
    Return

EndFunc


;==========================================================================
;
;==========================================================================
Func com_error_handler($oError)
#forceref $oError
;~  ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF)
;~  ConsoleWrite(@TAB & "Error Number........... " & "0x" & Hex($oError.number) & @CRLF)
;~  ConsoleWrite(@TAB & "Error WinDescription... " & $oError.windescription)
;~  ConsoleWrite(@TAB & "Error Description...... " & $oError.description & @CRLF)
;~  ConsoleWrite(@TAB & "Error ScriptLine....... " & $oError.scriptline & @CRLF)
;~  ConsoleWrite(@TAB & "Error RetCode.......... " & "0x" & Hex($oError.retcode) & @CRLF)

    Return ; Return without doing anything so @error can be checked

EndFunc

 

Edited by TheXman
Removed the includes from the example - not needed
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

×
×
  • Create New...