nhocquan

[WinHTTP 5.1 Object] request reached timeout

3 posts in this topic

#1 ·  Posted (edited)

Hi everyone,

I have a script that call this function again & again normaly

Func CallAPI($amount, $target, $condition, $API_key)
    $oHTTP.Open("POST","https://HOST?api_key="&$API_key)
    $oHTTP.SetRequestHeader('content-type','application/json')
    $oHTTP.Send('{"amount":'&$amount&',"target":'&$target&',"condition":"'&$condition&'"}')
    $Response = $oHTTP.ResponseText
    Return $Response
EndFunc

but sometime (may be after 105 request, 203 request, 1000 request, ... n request) this request error and make my script stop 

$oHTTP.Send('{"amount":'&$amount&',"target":'&$target&',"condition":"'&$condition&'"}')
$oHTTP.^ ERROR


I think sever response slow that make this request reached default timeout.

I try to set time out with hope if reached timeout this request will return error or something else and my script keep run continue

$oHTTP.SetTimeouts(30000,60000,30000,30000)
Spoiler

HTTP Timeouts With AutoIt

To easily change the timeouts, we can simply change all of them at the same time, in one line of code.


$oHTTP.SetTimeouts(30000,60000,30000,30000)

The timeouts that you set with the above line of code are as follows:

ResolveTimeout Maximum time allowed to use when resolving a host name, such as brugbart.com, to an ip address.
ConnectTimeout Maximum time allowed to spend when connecting to a server, if a server does not respond within this time, the connection is dropped.
SendTimeout Timeout for individual packets. Larger packets are normally broken up into multiple smaller packets.
ReceiveTimeout Timeout for incoming packets. Again, larger packets are normally broken into multiple smaller ones.

but it when request reached timeout, my script error, exit and return the same message above.

Anyone have any idea too fix this disadvantage, or when sever response slow it will skip this request and return error code and countinue code without exit?

Thanks everyone T.T

Edited by nhocquan

Share this post


Link to post
Share on other sites



The example from the AutoIt documentation  (ObjEvent) shows how to:

44 minutes ago, nhocquan said:

return error code and countinue code without exit

 

1 person likes this

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

6 hours ago, genius257 said:

The example from the AutoIt documentation  (ObjEvent) shows how to:

 

Thank you very much :*

Edited by nhocquan

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

  • Similar Content

    • CT83
      Need help Website Navigating with HTTPRequests and WinHttp
      By CT83
      (Sorry if this topic is in the wrong section, please suggest a better place if so.)
      Hi! a newbie here, since a few weeks i have got the basic hang of Pixelsearch, Controlclick, Imagesearch functions and now would like to proceed to Automating Internet navigation.
      In this case i am trying to develop a Script which will keep a watch on amazon page and notify me when the price of a commodity drops. I can program the latter part but i still am in search of a better way to , navigate and basically watch webpages with out actually having them on the screen
      Therefore, I need some guidance with HttpRequest, and Winhttp functions, can anyone please refer me to tutorials, links and example scripts for a complete noobbie? Some detailed Video Tutorials, and Guides would just be icing on the cake.
      Sorry, if this sounds too naive. Hoping for positive replies
       
       
    • ntprohd
      how to login to google with winhttp in AutoIt
      By ntprohd
      how to login to google with winhttp in AutoIt
    • RyukShini
      retry request with new proxy WINHTTP
      By RyukShini
      #include "WinHttp.au3" #include <array.au3> #include <file.au3> #include <WinAPI.au3> #include <string.au3> #include <WindowsConstants.au3> #include <FileConstants.au3> #include <ListviewConstants.au3> #include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> ; Learning HTTP Requests with WinHTTP ; Global $idOP, $usr, $list, $idEXIT, $call HotKeySet("{ESC}", "Terminate") Local $hGUI = GUICreate("Learning", 750, 500, 223, 202, $WS_BORDER) Local $idEXIT = GUICtrlCreateButton("Exit", 660, 440, 75, Default) Local $idCon = GUICtrlCreateButton("Start", 10, 10, 725, Default) Local $siteLab = GUICtrlCreateLabel("Select Test", 20, 48 ,Default, 35) GUICtrlSetFont (-1,9, 800); bold Local $IG = GUICtrlCreateCheckbox("test", 75, 45) Local $realm = GUICtrlCreateCheckbox("test1", 75, 65) Local $site3 = GUICtrlCreateCheckbox("test2", 200, 45) Local $site4 = GUICtrlCreateCheckbox("test3", 200, 65) $list = GUICtrlCreateListView( " PROXY | NAME1 | NAME2 | STATUS ", 20, 100, 710, 300, $LVS_NOSORTHEADER+$LVS_SINGLESEL) GUISetState(@SW_SHOW, $hGUI) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE, $idEXIT Exit Case $IG If _IsChecked($IG) Then $call = "test" EndIf Case $idCon Call($call) EndSwitch WEnd Func test() $url = "www.w3schools.com" $sPage = "/tags/demo_form.asp" Local $proxies Local $names _FileReadToArray("proxies.txt", $proxies) ; read the list of names to array _FileReadToArray("Okay.txt", $names) ; read the list of names to array For $i = 1 To UBound($proxies) - 1 $Read = $names[$i] $Datastring = ('') $newreadamount = _StringBetween($read,$Datastring, ':') $newreadamount[0] = StringReplace($newreadamount[0], ":", "") $name1 = $newreadamount[0] $Datastring2 = (':') $newreadamount2 = _StringBetween($read,$Datastring2, '') $newreadamount2[0] = StringReplace($newreadamount2[0], ":", "") $name2 = $newreadamount2[0] $sAdditionalData = "name1="&$name1&"&name2="&$name2 MsgBox(4096, "Test", $proxies[$i] & " - " & $name1&":"&$name2,1) ; Initialize and get session handle $hOpen = _WinHttpOpen("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0", $WINHTTP_ACCESS_TYPE_NAMED_PROXY, $proxies[$i]) _WinHttpSetTimeouts($hOpen, 15, 15, 15, 15) ; Get connection handle $hConnect = _WinHttpConnect($hOpen, $url) ; Make a request $hRequest = _WinHttpOpenRequest($hConnect, "POST", $sPage) ; Send it. Specify additional data to send too. This is required by the Google API: _WinHttpSendRequest($hRequest, "Content-Type: application/x-www-form-urlencoded", $sAdditionalData) ; Wait for the response _WinHttpReceiveResponse($hRequest) ; See what's returned Dim $sReturned If _WinHttpQueryDataAvailable($hRequest) Then ; if there is data Do $sReturned &= _WinHttpReadData($hRequest) Until @error EndIf ; Close handles _WinHttpCloseHandle($hRequest) _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hOpen) ; See what's returned MsgBox(4096, "Returned", $sReturned, 1) If StringInStr($sReturned,'Input was received as:') Then GUICtrlCreateListViewItem($proxies[$i] & "|"&$name1&"|"&$name2&"|Success", $list) Sleep(500) ContinueLoop ElseIf $sReturned = "" Then FileWrite("nottested.txt",$name1&":"&$name2 & @CRLF) GUICtrlCreateListViewItem($proxies[$i] & "|"&$name1&"|"&$name2&"|Bad Proxy", $list) Sleep(500) ContinueLoop EndIf Next EndFunc Func Terminate() Exit 0 EndFunc Func _IsChecked($idControlID) Return BitAND(GUICtrlRead($idControlID), $GUI_CHECKED) = $GUI_CHECKED EndFunc ;==>_IsChecked This is my code and it runs just fine.
      My problem is that if I receive Bad Proxy I need it to test the request again with the same array input $name1 / $name2 until I receive
      'Input was received as:'
      So basically
       
      ElseIf $sReturned = "" Then FileWrite("nottested.txt",$name1&":"&$name2 & @CRLF) GUICtrlCreateListViewItem($proxies[$i] & "|"&$name1&"|"&$name2&"|Bad Proxy", $list) Sleep(500) TRY AGAIN WITH NEW PROXY AND SAME CREDENTIALS EndIf Is this possible and if so do you have either some example code and/or some helpfile I can read.

      Thanks in advance!
    • jandings
      WinHTTP Get (to a REST API): Authentication problem
      By jandings
      Hello there,
      I'm trying to query a REST API of a webservice, SHOPWARE to be precise. The API is very well documented (https://developers.shopware.com/developers-guide/rest-api/#using-the-rest-api-in-your-own-a) but only using PHP. I tried some stuff but could not make it work.
      It should be very simple, because the following simply works in a Chrome-Browser:
      https://USERNAME:PASSPHRASE@www.SHOPWAREDOMAIN.com/api/orders/300
      queries me for username and passphrase and then dumps order number 300.
      Now I try to do the same in AutoIt (based on the wonderful work of many contributors here):
      #include-once #include "..\INCLUDE\winhttp.au3" #include "..\INCLUDE\OO_JSON.au3" ;Proxy Config Global $oJSON = _OO_JSON_Init() Global $obj = "" Global $sUserName = "USERNAME" Global $sPassword = "PASSPHRASE" Global $sDomain = "www.SHOPWAREDOMAIN.com/api/orders/300" Global $hOpen = _WinHttpOpen("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) JOC/0.1") ; without ie proxy settings $hConnect = _WinHttpConnect($hOpen, "https://" & $sDomain) ConsoleWrite ($hConnect & "#") $sReturned = _WinHttpSimpleSSLRequest($hConnect, "GET", $sUserName & ":" & $sPassword & "@" & $sDomain, Default ) ;$sReturned = _WinHttpSimpleSSLRequest($hConnect, "GET", $sDomain, Default, $sUserName & ":" & $sPassword) ;$sReturned = _WinHttpSimpleSSLRequest($hConnect, "GET", $sDomain, Default, , $sUserName & ":" & $sPassword ) _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hOpen) ; See what's returned ConsoleWrite ($sReturned & "#" & @error) Whatever version I use (I tried the commented ones and many others), I always get a reply from Shopware:
      {"success":false,"message":"Invalid or missing auth"}
      At least this comes from Shopware. But there must be some format of the credentials that I don't know or understand.
      Any help?