Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cancel Large File Uploading in TIdHTTP
#1
Hi,
What is the correct way to cancel file uploads using TIdHTTP.Post I believe FTP has Abort function but not HTTP.

I have the http request in side a thread syncing with UI normally But when I try to call Disconnect function in the middle of the upload I get this error: "Socket Error #10038 socket operation on non-socket". 
When I use the request in a thread as i said the error does not show up. But when I use in the main thread with Antifreeze component on form the error shows up. How can I close the connection without causing any errors on client or server (As I develop both).

I also tried IOHandler.CloseGracefully but also caused a message to be shown that connection is closed gracefully.

Is there a silent way to do this?

Thanks in advance.
Reply
#2
(12-21-2020, 05:42 AM)Ahmed Sayed Wrote: What is the correct way to cancel file uploads using TIdHTTP.Post

The only way to cancel a transfer is to close the socket connection. You can do that directly, or you can raise an exception in the context of the transfer, such as in the OnWork... events, and let TIdHTTP close the socket on failure.

(12-21-2020, 05:42 AM)Ahmed Sayed Wrote: I believe FTP has Abort function but not HTTP.

Because the FTP protocol has an explicit ABOR command to cancel the current transfer. The HTTP protocol has no such command.

(12-21-2020, 05:42 AM)Ahmed Sayed Wrote: I have the http request in side a thread syncing with UI normally But when I try to call Disconnect function in the middle of the upload I get this error: "Socket Error #10038 socket operation on non-socket".

That is to be expected, since Disconnect() will actually close the socket handle. So if you are doing that across thread boundaries, then you are basically ripping the socket out from underneath TIdHTTP. A slightly less destructive way might be to use your platform’s shutdown() function on the TIdHTTP.Socket.Binding.Handle. That will stop the further transmission of data without closing the socket itself. That won’t stop any error from occurring, you will just get a different error, though.

(12-21-2020, 05:42 AM)Ahmed Sayed Wrote: How can I close the connection without causing any errors on client or server (As I develop both).

In short, you can’t. But on the client side, you can simply discard the error since you know you are ending the request intentionally. On the server side, the request is incomplete, which is a real error, so all you can do at that point is close the connection since the state of the request is unknown. TIdHTTPServer will take care of that for you.

(12-21-2020, 05:42 AM)Ahmed Sayed Wrote: I also tried IOHandler.CloseGracefully but also caused a message to be shown that connection is closed gracefully.

CloseGracefully() is not meant for this purpose.

(12-21-2020, 05:42 AM)Ahmed Sayed Wrote: Is there a silent way to do this?

Not a silent way, no.

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)