EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Stopping TElHTTPSClient connection while executing a GET

Also by EldoS: Callback File System
Create virtual file systems and disks, expose and manage remote data as if they were files on the local disk.
#24142
Posted: 03/18/2013 11:47:40
by Tim Koscielski (Standard support level)
Joined: 03/17/2013
Posts: 6

With my code, I start a GET of a large resource, but the user then requests to cancel the download in mid stream. What is the best process to stop this connection without raising an Exception? The download could be done using HTTP or HTTPS.

Today I use the following to prematurely close the link, but it raises the excepption with the socket.

Code
ElHTTPSClient1.Close(True);
#24145
Posted: 03/18/2013 11:51:04
by Eugene Mayevski (EldoS Corp.)

That is fine - you need to be ready for any exceptions that might happen, and if you initiated closing, you can use some variable which will serve as an indicator of whether you need to display the exception to the user or not.


Sincerely yours
Eugene Mayevski
#24146
Posted: 03/18/2013 11:51:07
by Vsevolod Ievgiienko (EldoS Corp.)

Thank you for contacting us.

You should use TElHTTPSClient.OnProgress event handler to do this.
#24150
Posted: 03/18/2013 14:21:41
by Tim Koscielski (Standard support level)
Joined: 03/17/2013
Posts: 6

Ok, I am able to catch the exception in the global Application.OnException event, but I can't cleanly close the socket such that it won't raise an Exception. (I know its not normal to abort a socket while receiving data, but I'm trying to handle a very low bandwidth environment and looking f on how best to handle this.)

The error that keeps getting raised is
EElSocketError: Wrong socket state 0 in CanReceive (error code is 96258)

This error happens when using TElHTTPSClient.Close() or TElHTTPSClient.CancelRequest.

Why wouldn't this error instead bubble up through the OnError event of the object?

I was thinking about trying to implement this is a self contained unit that can use a thread to retrieve a file using HTTP the unit would be used in a console app or a windows app and provide appropriate Exception handling in the unit.

Can you offer any thoughts on handling the exception at a unit or object level rather than at an application level? Am I overlooking something that I could use to accomplish this?

Thanks.
#24151
Posted: 03/18/2013 14:27:19
by Eugene Mayevski (EldoS Corp.)

Quote
Tim Koscielski wrote:
This error happens when using TElHTTPSClient.Close() or TElHTTPSClient.CancelRequest.


That is (almost) fine - you need to wrap the call with try/except statement and ignore the error. If the error bothers you in IDE, you need to add the exception class to the list of exception classes to ignore in IDE options.

Quote
Tim Koscielski wrote:
Why wouldn't this error instead bubble up through the OnError event of the object?


OnError is a notification method for SSL-level errors which don't influence execution flow (well, they do influence it, but not in the way exception does).

Quote
Tim Koscielski wrote:
Can you offer any thoughts on handling the exception at a unit or object level rather than at an application level? Am I overlooking something that I could use to accomplish this?


Any modern programming language has a try/except (Pascal, VB.NET), try/catch (C++, C#, PHP, Java etc) language construct. Please refer to language documentation for details.


Sincerely yours
Eugene Mayevski
#24152
Posted: 03/18/2013 16:20:29
by Tim Koscielski (Standard support level)
Joined: 03/17/2013
Posts: 6

Eugene,

I had tried the simple Try Except End; block around both the "Cancel Request" and "Close(True)" for the object. It has looked basically as shown below.

Code
try
  ElHTTPSClient1.CancelRequest;
except
  ShowMessage('Exception raised: ' + E.Classname);
end;


In all of the various tests, the exception handler around the CancelRequest does not catch the exception, the Application.OnException event does. It almost feels like another Thread is raising the exception and the "CancelRequest" function does not complete stop the process looking for data on the incoming port?

Either way, the above code has also be tried using "Close(True)" and still I get the same result, exception raised in the Application.OnException event.

I have also tried the following....

  • Instead of closing the connection on a button click, I set a boolean flag.
  • In the OnData event, I checked for the flag and close connection on a loop through.
  • Exception still not caught locally, but still hits Application.OnException

I also tried the same process by putting the disconnect in the OnProgress with the same results.

Quote
That is (almost) fine - you need to wrap the call with try/except statement and ignore the error.


Right now I think I have tried to use this as described above with the standard try/except but it has not worked as assumed. Does this help, would my sample project be of some assistance at this time?

Thanks.
#24153
Posted: 03/19/2013 00:30:07
by Eugene Mayevski (EldoS Corp.)

If you are closing the request from the event handler, you need to wrap not the call to Close, but the call to Get.


Sincerely yours
Eugene Mayevski
Also by EldoS: Solid File System
A virtual file system that offers a feature-rich storage for application documents and data with built-in compression and encryption.

Reply

Statistics

Topic viewed 1041 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!