Thread View: borland.public.cppbuilder.internet.socket
11 messages
11 total messages
Started by dzeidzei
Wed, 16 Nov 2005 11:17
What's wrong with this?
Author: dzeidzei
Date: Wed, 16 Nov 2005 11:17
Date: Wed, 16 Nov 2005 11:17
55 lines
2238 bytes
2238 bytes
Hi,
I've tried to wrote a little dll for doing http and https post with indy TIdHTTP.
Here is my code:
AnsiString * HttpsPostSMS(AnsiString *Url, AnsiString *Params)
{
AnsiString *asRet = new AnsiString;
TIdHTTP *IdHTTP = new TIdHTTP(NULL);
IdHTTP->Request->ContentType = "application/x-www-form-urlencoded";
TIdConnectionInterceptOpenSSL *IdConnectionInterceptOpenSSL = new TIdConnectionInterceptOpenSSL(NULL);
IdConnectionInterceptOpenSSL->SSLOptions->Method = sslvSSLv23;
IdConnectionInterceptOpenSSL->SSLOptions->Mode = sslmClient;
IdConnectionInterceptOpenSSL->SSLOptions->VerifyDepth = 0;
IdHTTP->Intercept = IdConnectionInterceptOpenSSL;
IdHTTP->InterceptEnabled = true;
TStringStream * tsResponse = new TStringStream("");
TStringStream * tsParams = new TStringStream(*Params);
try
{
IdHTTP->Post(*Url, tsParams, tsResponse);
tsResponse->Position = 0;
*asRet = tsResponse->DataString.Trim();
}
catch(Exception &e)
{
*asRet = "Error: " + e.Message;
}
delete IdHTTP;
IdHTTP = NULL;
delete IdConnectionInterceptOpenSSL;
IdConnectionInterceptOpenSSL = NULL;
delete tsParams;
tsParams = NULL;
delete tsResponse;
tsResponse = NULL;
return asRet;
}
And when I try to call function above it'll trow 'Intercept value is not valid'?
Can someone tell what i've missed?
Thanks.
- dzeidzei
Re: What's wrong with this?
Author: Remy Lebeau (TeamB)
Date: Wed, 16 Nov 2005 21:45
Date: Wed, 16 Nov 2005 21:45
32 lines
2028 bytes
2028 bytes
"dzeidzei" <dzeidzei@suolakaivos.com> wrote in message
news:437b2354$1@newsgroups.borland.com...
> AnsiString * HttpsPostSMS(AnsiString *Url, AnsiString *Params)
> {
> AnsiString *asRet = new AnsiString;
Why are you dynamically allocating AnsiString everywhere? You should not
be.
> TIdHTTP *IdHTTP = new TIdHTTP(NULL);
You are not protecting that instantiation from unhandled exceptions. If an
exception is thrown, you will have a lot of memory leaks in your code.
> TIdConnectionInterceptOpenSSL *IdConnectionInterceptOpenSSL = new
TIdConnectionInterceptOpenSSL(NULL);
That component does not exist in either Indy 9 or 10. You are using a very
old version of Indy, probably Indy 8. Such versions are no longer
supported. You should seriously consider upgrading to the latest
Development Snapshot of at least Indy 9 (though Indy 10 has more features).
> And when I try to call function above it'll trow 'Intercept value is not
valid'?
Since you are using a very outdated version of Indy, the only way to know
why that error occurs is for you to look at TIdHTTP's source code yourself.
Gambit
Re: What's wrong with this?
Author: dzeidzei
Date: Thu, 17 Nov 2005 04:49
Date: Thu, 17 Nov 2005 04:49
11 lines
1386 bytes
1386 bytes
"Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> wrote: >> TIdHTTP *IdHTTP = new TIdHTTP(NULL); > >You are not protecting that instantiation from unhandled exceptions. If an >exception is thrown, you will have a lot of memory leaks in your code. How to protect It from unhandled exeptions? I quess it's done automatically when i put TIdHTTP component to my form, but can't find out what is the proper way to instantiate it 'manually'. - dzeidzei
Re: What's wrong with this?
Author: Remy Lebeau (TeamB)
Date: Thu, 17 Nov 2005 07:24
Date: Thu, 17 Nov 2005 07:24
89 lines
3601 bytes
3601 bytes
"dzeidzei" <dzeidzei@suolakaivos.com> wrote in message
news:437c19ea$1@newsgroups.borland.com...
> How to protect It from unhandled exeptions?
By not allowing there to be any uncaught exceptions. Use try...catch blocks
to catch them all. Especially since you said you are putting the code into
a DLL. A DLL is not allowed to let exceptions escape into the application.
That is very dangerous. For example:
AnsiString HttpsPostSMS(const AnsiString &Url, const AnsiString &Params)
{
AnsiString asRet;
try
{
TIdHTTP *HTTP = new TIdHTTP(NULL);
try
{
TIdConnectionInterceptOpenSSL *Intercept = new
TIdConnectionInterceptOpenSSL(IdHTTP);
Intercept->SSLOptions->Method = sslvSSLv23;
Intercept->SSLOptions->Mode = sslmClient;
Intercept->SSLOptions->VerifyDepth = 0;
HTTP->Intercept = Intercept;
HTTP->InterceptEnabled = true;
TStringStream *tsParams = new TStringStream(Params);
try
{
HTTP->Request->ContentType =
"application/x-www-form-urlencoded";
asRet = HTTP->Post(Url, tsParams);
}
__finally
{
delete tsParams;
}
}
__finally
{
delete HTTP;
}
}
catch(const Exception &e)
{
asRet = "Error: " + e.Message;
}
return asRet;
}
You can then use the std::auto_ptr class to clean up the code further:
#include <memory>
AnsiString HttpsPostSMS(const AnsiString &Url, const AnsiString &Params)
{
try
{
std::auto_ptr<TIdHTTP> HTTP(new TIdHTTP(NULL));
TIdConnectionInterceptOpenSSL *Intercept = new
TIdConnectionInterceptOpenSSL(HTTP.get());
Intercept->SSLOptions->Method = sslvSSLv23;
Intercept->SSLOptions->Mode = sslmClient;
Intercept->SSLOptions->VerifyDepth = 0;
HTTP->Intercept = Intercept;
HTTP->InterceptEnabled = true;
std::auto_ptr<TStringStream> tsParams(new
TStringStream(Params));
HTTP->Request->ContentType =
"application/x-www-form-urlencoded";
return HTTP->Post(Url, tsParams);
}
catch(const Exception &e)
{
return "Error: " + e.Message;
}
}
Gambit
Re: What's wrong with this?
Author: dzeidzei
Date: Thu, 17 Nov 2005 08:21
Date: Thu, 17 Nov 2005 08:21
2 lines
1034 bytes
1034 bytes
Thanks!
Re: What's wrong with this?
Author: dzeidzei
Date: Fri, 18 Nov 2005 06:39
Date: Fri, 18 Nov 2005 06:39
37 lines
2136 bytes
2136 bytes
Now I've upgraded to Indy 90017 and my function looks as follows:
AnsiString HttpsPostSMS(const AnsiString &Url, const AnsiString &Params)
{
AnsiString asResponse;
try
{
std::auto_ptr<TIdHTTP> IdHTTP(new TIdHTTP(NULL));
TIdConnectionIntercept *IdConnectionIntercept = new TIdConnectionIntercept(IdHTTP.get());
TIdSSLIOHandlerSocket *IdSSLIOHandlerSocket = new TIdSSLIOHandlerSocket(IdHTTP.get()) ;
TStringStream * tsParams = new TStringStream(Params);
try
{
IdSSLIOHandlerSocket->SSLOptions->Method = sslvSSLv23;
IdSSLIOHandlerSocket->SSLOptions->Mode = sslmClient;
IdSSLIOHandlerSocket->SSLOptions->VerifyDepth = 0;
IdHTTP->Request->ContentType = "application/x-www-form-urlencoded";
IdHTTP->Intercept = IdConnectionIntercept;
IdHTTP->IOHandler = IdSSLIOHandlerSocket;
asResponse = IdHTTP->Post(Url, tsParams);
return (asResponse.Trim());
}
__finally
{
delete tsParams;
delete IdConnectionIntercept;
delete IdSSLIOHandlerSocket;
}
}
catch(const Exception &e)
{
return ("Error: " + e.Message);
}
}
Re: What's wrong with this?
Author: dzeidzei
Date: Fri, 18 Nov 2005 06:41
Date: Fri, 18 Nov 2005 06:41
41 lines
2311 bytes
2311 bytes
Now I've upgraded to Indy 90017 and my function looks as follows:
AnsiString HttpsPostSMS(const AnsiString &Url, const AnsiString &Params)
{
AnsiString asResponse;
try
{
std::auto_ptr<TIdHTTP> IdHTTP(new TIdHTTP(NULL));
TIdConnectionIntercept *IdConnectionIntercept = new TIdConnectionIntercept(IdHTTP.get());
TIdSSLIOHandlerSocket *IdSSLIOHandlerSocket = new TIdSSLIOHandlerSocket(IdHTTP.get()) ;
TStringStream * tsParams = new TStringStream(Params);
try
{
IdSSLIOHandlerSocket->SSLOptions->Method = sslvSSLv23;
IdSSLIOHandlerSocket->SSLOptions->Mode = sslmClient;
IdSSLIOHandlerSocket->SSLOptions->VerifyDepth = 0;
IdHTTP->Request->ContentType = "application/x-www-form-urlencoded";
IdHTTP->Intercept = IdConnectionIntercept;
IdHTTP->IOHandler = IdSSLIOHandlerSocket;
asResponse = IdHTTP->Post(Url, tsParams);
return (asResponse.Trim());
}
__finally
{
delete tsParams;
delete IdConnectionIntercept;
delete IdSSLIOHandlerSocket;
}
}
catch(const Exception &e)
{
return ("Error: " + e.Message);
}
}
And when I tried to call it, I'll get 'IOHandler value is not valid' error. It seems that problem persists...
-dzeidzei
Re: What's wrong with this?
Author: dzeidzei
Date: Fri, 18 Nov 2005 08:57
Date: Fri, 18 Nov 2005 08:57
58 lines
2511 bytes
2511 bytes
Thanks!
But when I paste Your code to my function, I still get 'IOHandler value is not valid' when calling???
"Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> wrote:
>
>"dzeidzei" <dzeidzei@suolakaivos.com> wrote in message
>news:437d851c$1@newsgroups.borland.com...
>
>> Now I've upgraded to Indy 90017
>
>The latest snapshot build is 9.0.50.
>
>> and my function looks as follows:
>
>Get rid of the Intercept. It is no longer needed. The IOHandler will
>handle everything.
>
>> delete IdConnectionIntercept;
>> delete IdSSLIOHandlerSocket;
>
>Get rid of those. They are not needed since the TIdHTTP is the owner for
>them.
>
>The code can be as follows:
>
> #include <memory>
>
> AnsiString HttpsPostSMS(const AnsiString &Url, const AnsiString &Params)
> {
> try
> {
> std::auto_ptr<TIdHTTP> HTTP(new TIdHTTP(NULL));
> TIdSSLIOHandlerSocket *SSL = new
>TIdSSLIOHandlerSocket(HTTP.get()) ;
>
> SSL->SSLOptions->Method = sslvSSLv23;
> SSL->SSLOptions->Mode = sslmClient;
> SSL->SSLOptions->VerifyDepth = 0;
> HTTP->IOHandler = SSL;
>
> std::auto_ptr<TStringStream> tsParams(new
>TStringStream(Params));
> HTTP->Request->ContentType =
>"application/x-www-form-urlencoded";
>
> return HTTP->Post(Url, tsParams.get()).Trim();
> }
> catch(const Exception &e)
> {
> return ("Error: " + e.Message);
> }
> }
>
>
>Gambit
>
>
Re: What's wrong with this?
Author: dzeidzei
Date: Fri, 18 Nov 2005 09:11
Date: Fri, 18 Nov 2005 09:11
22 lines
1860 bytes
1860 bytes
This seems quite interesting. And when I put those components to a form it's working just fine... "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> wrote: > >"dzeidzei" <dzeidzei@suolakaivos.com> wrote in message >news:437d85b3$1@newsgroups.borland.com... > >> my function looks as follows: ><snip> >> And when I tried to call it, I'll get 'IOHandler value is >> not valid' error. It seems that problem persists... > >Given the code you have shown, that error is not possible. The *only* >condition that produces that error is when the IOHandler property is not >pointing to a TIdSSLIOHandlerSocket when requesting an HTTPS Url. That is >clearly not the case in your code. > > >Gambit > >
Re: What's wrong with this?
Author: Remy Lebeau (TeamB)
Date: Fri, 18 Nov 2005 09:39
Date: Fri, 18 Nov 2005 09:39
51 lines
2310 bytes
2310 bytes
"dzeidzei" <dzeidzei@suolakaivos.com> wrote in message
news:437d851c$1@newsgroups.borland.com...
> Now I've upgraded to Indy 90017
The latest snapshot build is 9.0.50.
> and my function looks as follows:
Get rid of the Intercept. It is no longer needed. The IOHandler will
handle everything.
> delete IdConnectionIntercept;
> delete IdSSLIOHandlerSocket;
Get rid of those. They are not needed since the TIdHTTP is the owner for
them.
The code can be as follows:
#include <memory>
AnsiString HttpsPostSMS(const AnsiString &Url, const AnsiString &Params)
{
try
{
std::auto_ptr<TIdHTTP> HTTP(new TIdHTTP(NULL));
TIdSSLIOHandlerSocket *SSL = new
TIdSSLIOHandlerSocket(HTTP.get()) ;
SSL->SSLOptions->Method = sslvSSLv23;
SSL->SSLOptions->Mode = sslmClient;
SSL->SSLOptions->VerifyDepth = 0;
HTTP->IOHandler = SSL;
std::auto_ptr<TStringStream> tsParams(new
TStringStream(Params));
HTTP->Request->ContentType =
"application/x-www-form-urlencoded";
return HTTP->Post(Url, tsParams.get()).Trim();
}
catch(const Exception &e)
{
return ("Error: " + e.Message);
}
}
Gambit
Re: What's wrong with this?
Author: Remy Lebeau (TeamB)
Date: Fri, 18 Nov 2005 09:42
Date: Fri, 18 Nov 2005 09:42
16 lines
1516 bytes
1516 bytes
"dzeidzei" <dzeidzei@suolakaivos.com> wrote in message news:437d85b3$1@newsgroups.borland.com... > my function looks as follows: <snip> > And when I tried to call it, I'll get 'IOHandler value is > not valid' error. It seems that problem persists... Given the code you have shown, that error is not possible. The *only* condition that produces that error is when the IOHandler property is not pointing to a TIdSSLIOHandlerSocket when requesting an HTTPS Url. That is clearly not the case in your code. Gambit
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads