TidHTTP and response timeout [Edit]

Delphi 2010 Prof.
Indy 10.5.5
========

Hello,

I have been looking for this in google and in the indyInDepth book, but I have not
found anything useful (or I could understand).

I have to get an image from a web server (http) and I am using a
function I found on some place.

Ok, in summary, I use this:


{code}    try
      idHttp.Get(URL, imgStream);
    finally
      idHttp.Free;
    end;
{code}

but if idHttp client has no answer at all, then there is no timeout or the
timeout parameter:
 {code} idHttp.ReadTimeout {code}
doesn't run as I am expected.

How can I set a real timeout to the idHttp client ?

Thanks.

PS: I have send the same question in Atozed Newsgroup.

Edited by: Terry Yapt on Oct 22, 2010 6:22 AM  ( Delphi and Indy versions added. )
0
Terry
10/22/2010 1:23:23 PM
embarcadero.delphi.winsock 1874 articles. 2 followers. Follow

18 Replies
9126 Views

Similar Articles

[PageSpeed] 39

> {quote:title=Terry Yapt wrote:}{quote}
> Delphi 2010 Prof.
> Indy 10.5.5
> ========
> 
> Hello,
> 
> I have been looking for this in google and in the indyInDepth book, but I have not
> found anything useful (or I could understand).
> 
> I have to get an image from a web server (http) and I am using a
> function I found on some place.
> 
> Ok, in summary, I use this:
> 
> 
> {code}    try
>       idHttp.Get(URL, imgStream);
>     finally
>       idHttp.Free;
>     end;
> {code}
> 
> but if idHttp client has no answer at all, then there is no timeout or the
> timeout parameter:
>  {code} idHttp.ReadTimeout {code}
> doesn't run as I am expected.
> 
> How can I set a real timeout to the idHttp client ?
> 
> Thanks.
> 
> PS: I have send the same question in Atozed Newsgroup.
> 
> Edited by: Terry Yapt on Oct 22, 2010 6:22 AM  ( Delphi and Indy versions added. )

Please try:
idHTTP.IOHandler.ReadTimeout
0
Hafedh
10/22/2010 3:42:47 PM
> {quote:title=Hafedh TRIMECHE wrote:}{quote}
> > {quote:title=Terry Yapt wrote:}{quote}
> > Delphi 2010 Prof.
> > Indy 10.5.5
> > ========
> > 
> > Hello,
> > 
> > I have been looking for this in google and in the indyInDepth book, but I have not
> > found anything useful (or I could understand).
> > 
> > I have to get an image from a web server (http) and I am using a
> > function I found on some place.
> > 
> > Ok, in summary, I use this:
> > 
> > 
> > {code}    try
> >       idHttp.Get(URL, imgStream);
> >     finally
> >       idHttp.Free;
> >     end;
> > {code}
> > 
> > but if idHttp client has no answer at all, then there is no timeout or the
> > timeout parameter:
> >  {code} idHttp.ReadTimeout {code}
> > doesn't run as I am expected.
> > 
> > How can I set a real timeout to the idHttp client ?
> > 
> > Thanks.
> > 
> > PS: I have send the same question in Atozed Newsgroup.
> > 
> > Edited by: Terry Yapt on Oct 22, 2010 6:22 AM  ( Delphi and Indy versions added. )
> 
> Please try:
> idHTTP.IOHandler.ReadTimeout

Where could I put the idHTTP.IOHandler.ReadTimeOut inside this code ?

{code}
function DownloadJPGToBitmap(const URL: string; ABitmap: TBitmap): Boolean;
var
  idHttp   :  TIdHTTP;
  imgStream: TMemoryStream;
  jpgImage : TJPEGImage;
begin
  result := false;
  imgStream := TMemoryStream.Create;
  try
    idHttp := TIdHTTP.Create(nil);
    idHttp.ConnectTimeout := 3000;
    try
      idHttp.Get(URL, imgStream);
    finally
      idHttp.Free;
    end;
    imgStream.Position := 0;
    jpgImage := TJPEGImage.Create;
    try
      jpgImage.LoadFromStream(imgStream);
      ABitmap.Assign(jpgImage);
      result := True;
    finally
      jpgImage.Free;
    end;
  finally
    imgStream.Free;
  end;
end;
{code}
0
Terry
10/22/2010 3:53:10 PM
<Terry Yapt> wrote in message news:299566@forums.embarcadero.com...

> if idHttp client has no answer at all, then there is no timeout or the
> timeout parameter:
> {code} idHttp.ReadTimeout {code}
> doesn't run as I am expected.

Works fine for me.

> How can I set a real timeout to the idHttp client ?

The ReadTimeout property is the only option available.

-- 
Remy Lebeau (TeamB)
0
Remy
10/22/2010 5:10:45 PM
<Hafedh TRIMECHE> wrote in message news:299603@forums.embarcadero.com...

> Please try:
> idHTTP.IOHandler.ReadTimeout

That is not necessary.  The ReadTimeout property that TIdHTTP inherits from 
TIdTCPClientCustom delegates to the IOHandler's ReadTimeout property 
internally:

{code:delphi}
function TIdTCPClientCustom.GetReadTimeout: Integer;
begin
  if IOHandler <> nil then begin
    Result := IOHandler.ReadTimeout;
  end else begin
    Result := FReadTimeout;
  end;
end;

procedure TIdTCPClientCustom.SetReadTimeout(const AValue: Integer);
begin
  FReadTimeout := AValue;
  if IOHandler <> nil then begin
    IOHandler.ReadTimeout := AValue;
  end;
end;
{code}

-- 
Remy Lebeau (TeamB)
0
Remy
10/22/2010 5:10:45 PM
<Terry Yapt> wrote in message news:299604@forums.embarcadero.com...

> Where could I put the idHTTP.IOHandler.ReadTimeOut inside this code ?

Where you set the ConnectTimeout, eg:

{code:delphi}
function DownloadJPGToBitmap(const URL: string; ABitmap: TBitmap): Boolean;
var
  idHttp   :  TIdHTTP;
  imgStream: TMemoryStream;
  jpgImage : TJPEGImage;
begin
  Result := False;
  try
    imgStream := TMemoryStream.Create;
    try
      idHttp := TIdHTTP.Create(nil);
      try
        idHttp.ConnectTimeout := 3000;
        idHttp.ReadTimeout := ...; // <-- no need to access the IOHandler 
directly
        idHttp.Get(URL, imgStream);
      finally
        idHttp.Free;
      end;
      imgStream.Position := 0;
      jpgImage := TJPEGImage.Create;
      try
        jpgImage.LoadFromStream(imgStream);
        ABitmap.Assign(jpgImage);
        Result := True;
      finally
        jpgImage.Free;
      end;
    finally
      imgStream.Free;
    end;
  except
  end;
end;
{code}

-- 
Remy Lebeau (TeamB)
1
Remy
10/22/2010 5:17:17 PM
> {quote:title=Remy Lebeau (TeamB) wrote:}{quote}
> <Terry Yapt> wrote in message news:299566@forums.embarcadero.com...
> 
> > if idHttp client has no answer at all, then there is no timeout or the
> > timeout parameter:
> > {code} idHttp.ReadTimeout {code}
> > doesn't run as I am expected.
> 
> Works fine for me.

It runs fine for you with Get Method ?
{code}
  try
    idHttp := TIdHTTP.Create(nil);
    idHttp.ConnectTimeout := 3000; // 3 seconds
    idHttp.ReadTimeout    := 2000;  // 2 seconds
    try
       idHttp.Get(URL, imgStream);  // <<-------  This ¿?¿?¿
    finally
      idHttp.Free;   // Never get into this...
    end;
{code}

Thanks Remy...
0
Terry
10/22/2010 5:19:31 PM
My problem is another than I was thinking....   :-(

In my tests I was trying several WebCam's and I got into this one:
http://202.213.157.230/nphMotionJpeg?Resolution=640x480&Quality=Standard

But this is a "continuous jpeg image" (load in your explorer to know what I say), then I suppose it NEVER stops sending information.  So idHttp component never goes into the next line.

Ok, I must to change my question:
How can I stop my idHttp.Get (raise exception) based in size or time ?

Thanks.
0
Terry
10/22/2010 5:24:21 PM
<Terry Yapt> wrote in message news:299631@forums.embarcadero.com...

> It runs fine for you with Get Method ?

Yes.

-- 
Remy Lebeau (TeamB)
0
Remy
10/22/2010 7:51:24 PM
<Terry Yapt> wrote in message news:299634@forums.embarcadero.com...

> In my tests I was trying several WebCam's and I got into this one:
> http://202.213.157.230/nphMotionJpeg?Resolution=640x480&Quality=Standard
>
> But this is a "continuous jpeg image" (load in your explorer to know what 
> I say)

When I load that URL in IE8, it tries to download a file of unknown format, 
not display a "continuous" image.  The webserver is sending the data as a 
"multipart/x-mixed-replace" Content-Type, which IE does not support.

> I suppose it NEVER stops sending information.  So idHttp component never
> goes into the next line.

That is correct.  The webserver is not sending either a "Content-Length: 
...." or "Transfer-Encoding: chunked" header, so TIdHTTP has no choice but to 
wait until the server closes the connection to signal the end of the data. 
The "multipart/x-mixed-replace" type is designed to not close the connection 
until the end of the data stream has been reached (for a streaming video 
file), however a live stream (like a webcam) will never end, so 
TIdHTTP.Get() will read indefinately (unless it times out if ReadTimeout is 
set) until the connection is closed.

> How can I stop my idHttp.Get (raise exception) based in size or time ?

You can either Disconnect() it from a separate thread when needed, or you 
can implements a custom TStream class that raises an exception in its 
overriden Write() method.

To handle "multipart/x-mixed-replace" responses with TIdHTTP, you are better 
off using the TStream approach.  That way, you can manually detect when each 
MIME boundary arrives and then process each data block as needed while Get() 
is still running.  Maybe in a future version of Indy, TIdHTTP will implement 
native support for "multipart/x-mixed-replace" responses.

Otherwise, you will have to switch your code to use TIdTCPClient directly, 
and then implement the HTTP processing manually.

-- 
Remy Lebeau (TeamB)
0
Remy
10/22/2010 7:51:25 PM
> {quote:title=Remy Lebeau (TeamB) wrote:}{quote}
> <Terry Yapt> wrote in message news:299631@forums.embarcadero.com...
> 
> > It runs fine for you with Get Method ?
> 
> Yes.

Thanks Remy...
0
Terry
10/22/2010 7:56:26 PM
> {quote:title=Remy Lebeau (TeamB) wrote:}{quote}
> <Terry Yapt> wrote in message news:299634@forums.embarcadero.com...
> 
> > In my tests I was trying several WebCam's and I got into this one:
> > http://202.213.157.230/nphMotionJpeg?Resolution=640x480&Quality=Standard
> >
> > But this is a "continuous jpeg image" (load in your explorer to know what 
> > I say)
> 
> When I load that URL in IE8, it tries to download a file of unknown format, 
> not display a "continuous" image.  The webserver is sending the data as a 
> "multipart/x-mixed-replace" Content-Type, which IE does not support.

You are right, IE8 tries to download a file.  Firefox runs fine and show me an office or something similar.  It is a "public" webcam I got searching on google.

> 
> > I suppose it NEVER stops sending information.  So idHttp component never
> > goes into the next line.
> 
> That is correct.  The webserver is not sending either a "Content-Length: 
> ..." or "Transfer-Encoding: chunked" header, so TIdHTTP has no choice but to 
> wait until the server closes the connection to signal the end of the data. 
> The "multipart/x-mixed-replace" type is designed to not close the connection 
> until the end of the data stream has been reached (for a streaming video 
> file), however a live stream (like a webcam) will never end, so 
> TIdHTTP.Get() will read indefinately (unless it times out if ReadTimeout is 
> set) until the connection is closed.

Thanks for your complete explanation.

> 
> > How can I stop my idHttp.Get (raise exception) based in size or time ?
> 
> You can either Disconnect() it from a separate thread when needed, or you 
> can implements a custom TStream class that raises an exception in its 
> overriden Write() method.
> 
> To handle "multipart/x-mixed-replace" responses with TIdHTTP, you are better 
> off using the TStream approach.  That way, you can manually detect when each 
> MIME boundary arrives and then process each data block as needed while Get() 
> is still running.  Maybe in a future version of Indy, TIdHTTP will implement 
> native support for "multipart/x-mixed-replace" responses.
> 
> Otherwise, you will have to switch your code to use TIdTCPClient directly, 
> and then implement the HTTP processing manually.

I think I will try the TStream approach but now it is not so important because my concern was a good server without an answer not a bad server configuration.  :-\

Thanks for your help Remy and also for the answers from the other people.

Best regards...
0
Terry
10/22/2010 8:01:10 PM
<Terry Yapt> wrote in message news:299699@forums.embarcadero.com...

> You are right, IE8 tries to download a file.  Firefox runs fine
> and show me an office or something similar.  It is a "public"
> webcam I got searching on google.

Most third-party browsers support it, even on smartphones, etc.  Microsoft 
never has.

-- 
Remy Lebeau (TeamB)
0
Remy
10/22/2010 8:12:31 PM
Hi,

Could you tell me how to catch the IDHttp timeout Exception... and which exception in your example above... ?

I need it to make more try if the first connexion fails.

{code}
    try
        IdHTTP1.Post(url, stl, ts);
    except
     ...
   end;
{code}



Cédric
0
cedric
7/28/2011 8:22:44 PM
<cedric joubert> wrote in message news:382222@forums.embarcadero.com...

> Could you tell me how to catch the IDHttp timeout Exception...

What kind of timeout are you referring to?  Connect timeout? Read timeout? 
Indy raises EIdConnectTimeout (but can also raise EIdConnectException or 
EIdSocketError) if a connection times out, and EIdReadTimeout if a read 
times out.

-- 
Remy Lebeau (TeamB)
0
Remy
7/28/2011 9:16:34 PM
hum... i don't know which one... but thoses catched when then TIdHttp seems to reach a very slow site.
Even the query can return with success, i would need to abort in the next 3 sec for example...

How would you implement this kind of behavious please ?

Cédric
0
cedric
7/29/2011 8:17:28 PM
<cedric joubert> wrote in message news:382481@forums.embarcadero.com...

> hum... i don't know which one... but thoses catched when then
> TIdHttp seems to reach a very slow site.  Even the query can
> return with success, i would need to abort in the next 3 sec for
> example...

Have you tried simply catching all exceptions and then see which one(s) are 
actually being raised in your scenario?  Have you tried setting TIdHTTP' 
ConnectTimeout and ReadTimeout properties yet?  Note that in the case of 
ConnectTimeout, 3 seconds is a bit on the short side.  The OS could take 
longer than that just setting up the connection (and the thread that Indy 
uses internaly to implement the timeout).

-- 
Remy Lebeau (TeamB)
0
Remy
7/29/2011 8:58:40 PM
Thanks Remy,

In fact, i'm probably missing something in my code, because i've never seen any exception raised on my appli.
or maybe i'm just lucky with all the sites which i connect.

The only 2 exceptions i can simulate is these ones :
-  (disabling my lan connection before connecting)
Socket Error (11004)

- Connect to a site i've found with google  by chance (i do not know it) that do not respond
http://www.313fad.com
Socket Error (Host not found - 11001)

Maybe do you know any good benchmark site i can connect to test differents scenarios (to test slow connection but site alive)... ?

Oups...(i'm re-editing this messageI ;-)  )  I'm realising... that I did not configure the ReadTimeout property... Si i have a to test it before... (and I come back)

Cédric

Edited by: cedric joubert on Jul 30, 2011 1:03 AM
0
cedric
7/30/2011 8:05:58 AM
Ok, finally, i've succeed to raise the ReadTimeout ... slowing down my internet connection... and over and overloading my PC...

but i'm still interested by a clever solution ;-)  if you have any idea... or know some kind of site for testing/simulate slow connections...

Cédric
0
cedric
7/30/2011 9:53:24 AM
Reply:

Web resources about - TidHTTP and response timeout [Edit] - embarcadero.delphi.winsock

America's Response Monument - Wikipedia, the free encyclopedia
dedicated to the United States Special Forces and commemorates the servicemen and women of America’s Special Operations response to 9/11, including ...

WA fires: DFES defends response times to devastating Yarloop fires
Department of Fire and Emergency Services commissioner Wayne Gregson has defended his department's response times to the South West fires after ...

Antibodies to our own proteins may predict adverse vaccine responses
(credit: Art Writ ) Let's get something straight from the start: vaccines are good. Let's be completely clear about that. But sometimes some ...

GOOD NEWS: Apple Buys Technology That Uses Your Phone’s Camera to Gauge Emotional Response to Ads
... better understand consumer reactions to engagements. Given the iPhone’s camera, the possibilities seem endless: • Gauge the emotional response ...

Five possible responses to the Google-Motorola merger
When big acquisitions are announced, there's likely an aftershock that reverberates through an industry. CNET takes a look at some possible scenarios ...

Giuliani Says Trump’s Response To Hillary Calling Him A ‘Sexist’ Was ‘Politically Brilliant’
Giuliani Says Trump’s Response To Hillary Calling Him A ‘Sexist’ Was ‘Politically Brilliant’

Abbreviated pundit roundup: 'A national crisis like this demands a national response'
... Barack Obama , who held a live town hall on CNN on reducing gun violence yesterday: A national crisis like this demands a national response. ...

Catcher cuts capex for 2016 in response to client orders
Catcher Technology is planning to cut its capex for 2016, and the amount will be much lower than those budgeted in the past two years in response ...

Amy Schumer Has the Perfect Response to Talk About Her New Boyfriend: This is Apparently My 'Biggest ...
Amy Schumer Has the Perfect Response to Talk About Her New Boyfriend: This is Apparently My 'Biggest Accomplishment'

Consumers Dissatisfied With Interactive Voice Response
Often, respondents reported being frustrated by interactive voice responses (IVRs) that replicated information already available on the website. ...

Resources last updated: 1/11/2016 1:47:45 PM