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

    • sivaramanm
      WinHTTP Send with Complex Send Parameters
      By sivaramanm
      Have this following input parameter(Complex list of inputs) that need to be SEND as part of HTTP POST Request. 
      Have been searching forums to find an AutoIT equivalent to enapsulate the following data, but unsuccessful.
      Below given Working Code is failing with 400-Bad Request for obvious reason of incomplete input - unable to send ip_list and other array type parameters
      Questions:
      1. How to encapsulate the below given data for $oHttp.Send()
      2. How to enapsulate special data types like
       null - aligned_device_tempate parameter
      White sapced values - () and
      empty array? - hostname or device_groups
       
      Input Parameter in JSON format to be converted & sent in AutoIT compatible
      {       "name": "SnmpSIM",       "description": "EM7 device created by BDD test case",       "credentials":"{{feature.credential.body.result_set[*].URI}}",        "organization": "/api/organization/0",       "aligned_device_template": null,       "aligned_collector": "{{feature.appliance_id[0].id[0]}}{{feature.appliance_id[0].id[1]}}",       "discover_non_snmp": "1",       "scan_ports": [         "21",         "22",         "23",         "25",         "80"       ],       "ip_lists": [         {         "start_ip": "{{feature.json.ip}}",         "end_ip": "{{feature.json.ip}}"         }       ],       "dhcp_enabled": "0",       "duplicate_protection": "1",       "model_device": "1",       "log_all": "1",       "scan_all_ips": null,       "port_scan_timeout": null,       "initial_scan_level": null,       "scan_throttle": null,       "interface_inventory_timeout": "600000",       "max_interface_inventory_count": "10000",       "bypass_interface_inventory": "0",       "hostnames": [],       "device_groups": []     }  
      Working Code
      ; The data to be sent $sPD = 'name=SnmpSIM&description=EM7 device created by BDD test case&credentials=37&organization=/api/organization/0&aligned_device_template=null&aligned_collector=1&discover_non_snmp=1&dhcp_enabled=0&duplicate_protection=1&model_device=1&log_all=1&scan_all_ips= null&port_scan_timeout= null&initial_scan_level= null&scan_throttle= null&interface_inventory_timeout=600000&max_interface_inventory_count=10000&bypass_interface_inventory=0&hostnames=&device_groups=&scan_ports=21' ; Creating the object $oHTTP = ObjCreate("winhttp.winhttprequest.5.1") $oHTTP.Open("POST", "http://10.2.4.18/api/discovery_session", False) $oHTTP.SetCredentials("username","password",0) $oHTTP.SetRequestHeader("Content-Type", "application/em7-resource-uri") ; Performing the Request $oHTTP.Send($sPD) ; Download the body response if any, and get the server status response code. $oReceived = $oHTTP.ResponseText $oStatusCode = $oHTTP.Status If $oStatusCode <> 200 then  MsgBox(4096, "Response code", $oStatusCode) EndIf ; Saves the body response regardless of the Response code  $file = FileOpen("Received.html", 2) ; The value of 2 overwrites the file if it already exists  FileWrite($file, $oReceived)  FileClose($file)  

       SAMPLE VBA CODE for Reference
       Dim Items As New Collection  Dim Item As Dictionary  Dim Id As Long  For Id = 1 To 2      Set Item = New Dictionary      Item("iditem") = Id      Item("amount") = 1      Items.Add Item  Next Id  Request.AddBodyParameter "id", 5633  Request.AddBodyParameter "items", Items $oDictionary = ObjCreate("Scripting.Dictionary") $oDictionary.ADD("start_ip", "10.20.7.31") $oDictionary.ADD("end_ip", "10.20.7.33")  
    • parrishjason
      Load Page In IE and check HTTP Status
      By parrishjason
      I want an AutoIT script to be able to open a given page, but also be able to check the HTTP status of the page and print out the status code in a message box. Looking through some of the documentation, I've found how to open a page in Internet Explorer and keep it as an object by doing this:
      $IE = _IECreate($google_url)
      ^^^ but how would I find the HTTP status 200 from here?
    • AutID
      _WinHttpSimpleSSLRequest return non expected result
      By AutID
      I am trying to get Spotify User's playlists via http requests using WinHTTP.au3.
      Now according to their site, application's requests authorization should look something like this.
      #include "WinHttp.au3" Local $hOpen = _WinHttpOpen('Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)') Local $sURL = "https://accounts.spotify.com/authorize/?" Local $ClientId = "26d287105e31491889f3cd293d85bfea" Local $ResponseType = "code" Local $sRedirectURI = "http://localhost:8000" Local $aState = "XSS" Local $aURL = $sURL & "client_id=" & $ClientId & "&response_type=" & $ResponseType & "&redirect_uri=" & $sRedirectURI & "&state=" & $aState & "&scope=playlist-read-private user-read-private user-read-email user-library-read user-follow-read user-read-birthdate" Local $hConnect = _WinHttpConnect($hOpen, $aURL) Local $sData = _WinHttpSimpleSSLRequest($hConnect) _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hOpen) ConsoleWrite($sData & @CRLF) This would work on a browser however I am not sure http requests support these kind of request.
      Normally on a browser at this point the user is asked to authorize the access by submitting his credentials in case they are not saved. He is then redirected to the Redirect uri and granted access.
       
      So my questions are simple. Before loosing too much time on this for nothing, is it possible to achieve what I'm trying to? If yes, what should my next step be?