-2

Using Indy's TIdHTTP component, I want to do a POST operation without waiting for a response from the server. The normal function I use is:

IdHTTP1->Post(sURL, reqStream, resStream);

But I want to skip the response to save time so Post() exits faster. How do I do that?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Omar
  • 11
  • I don't care about the response, it does not matter if the post succeed or not – Omar Aug 30 '18 at 20:26
  • 1
    I have no idea what you're talking about. What is `IdHTTP1`? What do you mean by "skip the response"? – melpomene Aug 30 '18 at 20:27
  • I just want to to a post in c++ builder without getting a response. i know it strange but i need it like that. – Omar Aug 30 '18 at 20:29
  • 1
    @Omar I still remember my times as beginner when it was difficult for me to know which of the things I used belong to the language standard and which were third party, so I can relate. That said, whatever `IdHTTP1` is, it's not part of C++. It might be written in C++, it might ship with some C++ IDE (you mentioned something called C++ Builder), but not everyone who is adept in C++ knows every third party tool for C++. For us to be able to help you, and for those who know the tools you are using to see your question, please describe what tools you are using and add useful tags to your question. – Max Vollmer Aug 30 '18 at 20:43
  • @Omar I've added tags for the IDE and third party solutions your question seems to be related to. This should make your question visible to people who can help in this area. – Max Vollmer Aug 30 '18 at 20:49
  • ok, you are right, what i use is a tool which call TIdHTTP, and with that tool i do a post useing a function like that: IdHTTP->Post(URL, requset, response); and this function waits for response like 200 millesecond, i dont want to wait this time, i just want to post and dont care if the post success or not. if you or any one else knows another tool to post but without waiting for the response please tell me. that what i mean. – Omar Aug 30 '18 at 20:52
  • Being as far removed from web development as I am, I ended up going down quite a bit of a Google rabbithole looking up what `IdHTTP1` was (and then C++Builder, then Delphi, then Indy). So I learned something new today! – hegel5000 Aug 30 '18 at 20:53
  • 1
    If it doesn't matter whether the post succeeds or not, then the fastest option is to not post at all. – Caleb Aug 30 '18 at 20:55
  • 1
    @Caleb obviously the OP wants to post data to the server, he just doesn't want to wait for the response. The fastest option would be to not use `TIdHTTP` at all. Simply drop down to the TCP layer directly. Create a socket, write a few basic HTTP request headers to it, write the post data to it, and then disconnect it. – Remy Lebeau Aug 30 '18 at 21:01

1 Answers1

4

HTTP is a request/response protocol. TIdHTTP is designed to follow that protocol, and that means its request methods always read a response in full before exiting.

That being said, you have two options to accomplish what you want:

  • let TIdHTTP read the response normally, but simply not save it anywhere. You can use a response TStream that discards any bytes written to it (such as TIdEventStream with no OnWrite event handler), or if you are using an up-to-date version of Indy, you can simply set the response stream to NULL.

    IdHTTP1->Post(sURL, reqStream, (TStream*)NULL);
    

    Obviously, this still has the overhead of reading the full response from the socket, at least.

  • use the TIdHTTP::OnHeadersAvailable event to check the current Response status code. When a final (non-redirect, non-authorization) status code is received, abort processing of the response by throwing an exception (such as by calling Sysutils::Abort()), which will bypass reading the response body. Make sure to close the underlying socket after TIdHTTP::Post() exits, if TIdHTTP doesn't do it automatically for you. Or, you can close the socket instead of throwing your own exception, and let TIdHTTP throw its own exception when it can't read the response body from the socket. The end effect is the same either way.

I have added a new feature request to Indy's issue tracker for you, to address this situation better in a future version of Indy:

#230 Add hoNoReadResponseBody flag to TIdHTTP.HTTPOptions property

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770