Error on data reading from the connection: An existing connection was forcibly closed by the remote host
Posted: Sun 19 Apr 2020 16:41
In this topic (viewtopic.php?f=27&t=40513) I was exploring the SecureBridge HTTP component possibilities. After implementing TScHttpWebRequest in my project I receive again an exception. The exception occurs when TScHttpWebRequest is called several times in a short period of time. Below you find the exception message.
The exception message is
A dirty solution is to catch the exception and recall the request. In my opinion, code below is a dirty method. It is a dirty method because I need to search for the error message as a String instead of a specific Exception class like HttpException. My solution below could end up in an endless loop. (I understand that it is possible to count the recalls and do something after the maximum count is reached).
I am wondering if there is another method for handling the error message. Can you please inform me what to do? Thank you in advance for your support.
The exception message is
Code: Select all
Project Project1.exe raised exception class Exception with message 'Error on data reading from the connection:
An existing connection was forcibly closed by the remote host'.
Code: Select all
void __fastcall TMyProject::Get(String AUrl, TStringStream *&AResponseContent)
{
try
{
TScHttpWebRequest *Request = new TScHttpWebRequest(AUrl);
try
{
Request->UserAgent = this->UserAgent;
Request->Method = rmGET;
Request->ConnectionGroupName = "my_group_name";
TScHttpWebResponse *Response = Request->GetResponse();
try
{
Response->ReadToStream(AResponseContent);
}
__finally
{
Response->Free();
}
}
__finally
{
Request->Free();
}
}
catch (HttpException &exception)
{
// nothing is fetched here
}
catch (Exception &exception)
{
if(exception.Message.Pos("An existing connection was forcibly closed by the remote host") > 1)
{
this->Get(AUrl, AResponseContent);
}
}
}
// ---------------------------------------------------------------------------