Jump to content

GG2023

Members
  • Content Count

    3
  • Joined

  • Last visited

Everything posted by GG2023

  1. GG2023

    Using TIdHTTP in a thread

    Hello, I am using TIdHTTP with SSL IO to send/receive REST API requests. At some point I get Read timed out and at this point the thread seems to be stuck in-limbo. I tried to recreate the HTTP object, but seems like that does not help - how do I reset the component after Read timed out exception? I am using simple Get and Post methods. I have keep-alive option set, now I am wondering if it would help to remove it... Thanks a lot. Gabi
  2. GG2023

    Using TIdHTTP in a thread

    Dear Remy, I appreciate your input, I have found the issue in my code. Regarding OnStatusInfoEx this is pretty strange, I had to add this piece to several other RESP providers in order for the code to work. Most likely I am still missing something here. Have a good weekend and thank you.
  3. GG2023

    Using TIdHTTP in a thread

    The thread itself looks like this while not Terminated do begin //do something with TXXX object that affects X, Y, Z vTerminate := X - Y < Z; if vTerminate then begin PostLog(0, 'Thread is about to be terminated.'); UpdateThread; self.Terminate; end else begin PostAlive; Sleep(FSystemConfig.ThreadSleep) end end //while not terminated Now the TXXX creates and uses TIdHTTP like below procedure TXXX.CreateHTTP; begin FHTTP := TIdHTTP.Create(NIL); FHTTP.Request.Connection := 'keep-alive'; FHTTP.IOHandler := CreateSSLIOHandler; FHTTP.ConnectTimeout := 10000; FHTTP.ReadTimeout := 30000; end; // ----------- function TXXX.CreateSSLIOHandler: TIdSSLIOHandlerSocketOpenSSL; begin result := TIdSSLIOHandlerSocketOpenSSL.Create; result.SSLOptions.Method := sslvTLSv1_2; result.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2]; result.SSLOptions.Mode := sslmClient; result.SSLOptions.VerifyMode := []; result.SSLOptions.VerifyDepth := 0; result.OnStatusInfoEx := self.OnStatusInfoEx; end; procedure TXXX.OnStatusInfoEx(ASender: TObject; const AsslSocket: PSSL; const AWhere, Aret: TIdC_INT; const AType, AMsg: String); begin SSL_set_tlsext_host_name(AsslSocket, FHTTP.Request.Host) end; And the actual HTTP processing is the following vJsonToPost := {....some json goes here} PostLog(0, vJsonToPost); vJsonToSend := TStringStream.Create(vJsonToPost); try FHTTP.Request.ContentType := 'application/json'; FHTTP.Request.ContentEncoding := 'utf-8'; while TRUE do begin try vId := ''; vResult := FHTTP.Post(vUrl, vJsonToSend); if vResult > '' then begin ///working with vResult end; except on E: Exception do begin PostLog(1, 'Exception in SubmitIOCBuyOrder: ' + E.Message); //FGotException := True; PostLog(100, FHTTP.Response.RawHeaders.Text); //if Read time out //if FHTTP.Response.RawHeaders.Text = '' then if E.Message = 'Read timed out.' then RecreateHTTP; end end; if vClientOrderId > '' then if CheckIfOrderIsThere(vClientOrderId) then begin PostLog(0, 'New order is going to be posted with status: ' + upperCase(vStatus)); PostOrder; break end; end //while TRUE finally vJsonToSend.Free end; CheckIfOrderIsThere does FHTTP.Get, if that matters.
×