Jump to content
Colek

THttpCli - retry request

Recommended Posts

Hello,

 

I am working with a rather unstable API. Sometimes, I'm getting an error 404 or 403 without a reason, but simply retrying the request the second time works and I'm receiving proper data from API.

 

I would like to implement a feature, where if my THttpCli gets in header one of these codes, I will retry the request. But I am unsure on how to retry the request from, for example, OnHeaderEnd.

 

What code should I implement in this procedure to retry the request? Sending a simple GetAsync again doesn't seem to work.

 

Thank you,

Piotr

Share this post


Link to post

You should finish the call chain in OnHeaderEnd and retry the request async-ly. F.ex., by posting a custom message via PostMessage to socket control's handle (and, of course, write handler for it). Also don't forget to limit number of retries

Share this post


Link to post
53 minutes ago, Fr0sT.Brutal said:

You should finish the call chain in OnHeaderEnd and retry the request async-ly. F.ex., by posting a custom message via PostMessage to socket control's handle (and, of course, write handler for it). Also don't forget to limit number of retries

 

Thank you for the very quick reply, appreciated!

 

Is there a chance you could write a code snippet that would show how can I achieve that? I don't really understand how to do this myself. I'm not an experienced programmer.

 

Thank you,

Piotr

Share this post


Link to post

Where ever you check the 404 error send the windows message to start another GetAsync request.  Or use sync methods when you can do a simple loop and break when it works, Get instead of GetAsync it all depends on how your application designed. 

 

Angus

 

Share this post


Link to post

Colek, I would create a new component instead of modifying the ICS one. You can either inherit from THttpCli component or inherit from TComponent and embed a THppCli to do what needs to be done. Either way has pros and cons but both are exactly what object oriented programming is all about. Think about moving the logic behind your application into a component instead of putting it into a TForm.

 

Share this post


Link to post

Thank you for your suggestions and help! 🙂

 

In the end, I did make an inherited class from the THttpCli and I am checking the status codes in OnRequestDone event and handling it there. Everything works as I wanted it to 🙂

 

Thanks again,

Piotr

Share this post


Link to post

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
×