Jump to content
Sign in to follow this  
fenhanxue

How to get HttpRequest 's Return value

Recommended Posts

fenhanxue

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 ?

 

Share this post


Link to post
Share on other sites
TheXman

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

Share this post


Link to post
Share on other sites
fenhanxue
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

Share this post


Link to post
Share on other sites
TheXman

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

Share this post


Link to post
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
Sign in to follow this  

  • Similar Content

    • Xenobiologist
      By Xenobiologist
      Hello,
      I am using Loxone as the central "technology" for controlling my SmartHome. Now, I'm curious whether it is possible to use the webservice to get/set information in the Loxone miniserver.
      Anyone tried that already, or anyone who has got an idea what code to use?
      Thanks!
      Describtion of the webservice can be found here: https://www.loxone.com/enen/kb/web-services/
      Getting the mac address is working. The result looks the way it is described on the website.
      <?xml version="1.0" encoding="utf-8"?>
      <LL control="dev/cfg/mac" value="59:9F:99:90:89:C9" Code="200"/>
      All other stuff like getting the version - is not working.
      ;https://www.loxone.com/enen/kb/web-services/ #include <Crypt.au3> _Crypt_Startup() ; To optimize performance start the crypt library. Global Const $HTTP_STATUS_OK = 200 Global $user = 'xxx' Global $password = 'xxx' Global $ip = '192.168.178.77' Global $port = '50666' ;~ Global $MD5 = HttpGet('http://' & $user & ':' & $password & '@' & $ip & ':' & $port & '/dev/cfg/mac', "password=" & _Crypt_HashData($password, $CALG_MD5)) Global $MD5 = HttpGet('http://' & $user & ':' & $password & '@' & $ip & ':' & $port & '/dev/cfg/version', "password=" & _Crypt_HashData($password, $CALG_MD5)) ;~ Global $MD5 = HttpGet('http://' & $user & ':' & $password & '@' & $ip & ':' & $port & '/dev/cfg/version') ;, "password=WeWantThisAsMd5") ;~ Global $MD5 = HttpGet('/dev/cfg/gateway') ;, "password=WeWantThisAsMd5") ConsoleWrite($MD5 & @CRLF) Func HttpPost($sURL, $sData = "") Local $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1") $oHTTP.Open("POST", $sURL, False) If (@error) Then Return SetError(1, 0, 0) $oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded") $oHTTP.Send($sData) If (@error) Then Return SetError(2, 0, 0) If ($oHTTP.Status <> $HTTP_STATUS_OK) Then Return SetError(3, 0, 0) Return SetError(0, 0, $oHTTP.ResponseText) EndFunc ;==>HttpPost Func HttpGet($sURL, $sData = "") Local $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1") ;~ $oHTTP.Open("GET", $sURL & "?" & $sData, False) $oHTTP.Open("GET", $sURL & "?" & $sData, False) If (@error) Then Return SetError(1, 0, 0) $oHTTP.Send() If (@error) Then Return SetError(2, 0, 0) If ($oHTTP.Status <> $HTTP_STATUS_OK) Then Return SetError(3, 0, 0) Return SetError(0, 0, $oHTTP.ResponseText) EndFunc ;==>HttpGet So long,
      Mega
    • olmanRvr
      By olmanRvr
      I have a compiled script :-timeStampSecs.exe which returns timestamp in seconds. I call it in FileWrite to write the time stamp in a text file.But it writes some other values instead of the timestamp. Please help.
      thanks
      olmar
      The script is below:-
      $kNum=run("timeStampSecs.exe","",1); also tried with flags 2,4,8,10 $hFile=FileOpen("timeStamp.txt",2) FileWrite($hFile,"the key is: "&$kNum) FileClose("$hFile") ;Code of thetimeStampSecs.exe:- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; #include <Date.au3> Func secFromEpoch() Local $ts=_DateDiff( "s","1970/01/01 00:00:00",_NowCalc()) $ts=String($ts) ConsoleWrite("timeStamp is: "&$ts&@CRLF) Return $ts EndFunc secFromEpoch()  
    • caramen
      By caramen
      Hello guy got some trouble in this easy script
       
      Case $BoutonWhrite1 $Case1 = MsgBox (4,"Are you sure ?" ,"Reg key gonna be changed." ) While 1 If $Case1 = "No" Then ConsoleWrite(">Case -1 Started" & @CRLF) ExitLoop EndIf $InputType = InputBox ("Value Type ?", 'Type of key to write: "REG_SZ", "REG_MULTI_SZ", "REG_EXPAND_SZ", "REG_DWORD", "REG_QWORD", or "REG_BINARY".' ) ;~ RegWrite (""&Reg1,""&$RegName1,""&$InputType, ""&$RegValue1) ExitLoop WEnd I tryed
      If $case1 = 1
      If $case1 = -1
      If $case1 = "No"
       
      What is the returned value by the msg box YES or NO ?
       
      The script is going wrong about the final purpose (Whrite a Registry key if the Script user unswer YES ) But dont take attention i am gonna corect after get the returned value xD it was for testing. 
    • bigbangnet
      By bigbangnet
      I got this code which copies some files in my network to pcs with open office. Really useful if you got lots of pc's.

      How would I "display" a message that the file was copied successfully ? A bit lost since I use a case here. I know i could call the filecopy in a variable but since I verify if the os is windows 7, xp or 2k thats where I get lost a bit.

      like this but its not enough since i don't have my os verification going on :

      $variable = FileCopy ( $CompletePath & "mission" & $NextMis & ".mis", $CompletePath & "currentmis.mis",1 ) If $variable = 1 then ;Do something Else ;Do something else Endif

      This is my current code

      Local $os = @OSVersion If Not IsDeclared("iMsgBoxAnswer") Then Local $iMsgBoxAnswer $iMsgBoxAnswer = MsgBox(4, "Vérification de la version de OpenOffice", "Est-ce que la version 3.3 ou supérieur de openOffice est installé ?") Select Case $iMsgBoxAnswer = 6 ;Yes If $os = "WIN_7" Then FileCopy("192.168.1.126clientsappsScriptregistrymodifications.xcu", @AppDataDir & "OpenOffice.org3user", 1) Else If $os = "WIN_XP" Then FileCopy("192.168.1.126clientsappsScriptregistrymodifications.xcu", @AppDataDir & "OpenOffice.org3user", 1) Else If $os = "WIN_2000" Then MsgBox(1, "OpenOffice 3.3", "Ne pas installer ou utiliser Openoffice sur Windows 2000") EndIf EndIf EndIf Case $iMsgBoxAnswer = 7 ;No Exit EndSelect
    • MattX
      By MattX
      Just a quickie, I need some ideas in regards to returning a value once Chrome has finished loading a page. I know there is no Chrome UDF [ shame ] I will be launching the pages using the quick and dirty method of calling chrome with a web page after it:

      chrome www.webpage.com

      There is the WinWaitActive function but was wondering if anyone has managed to code a better method or do you think this is best way to approach it ?

      FYI the majority of the time the web page opening will be from the local LAN. Thanking you in advance.
×