Search code examples
c++builderindy10

HTTPS web addresses with Indy IdHTTP in C++Builder


I am using the code below in C++Builder XE4 VCL 32bit. I am using the Indy components, version 10.6.0.497.

I have been using IdHTTP->Get() with HTTP addresses that have now changed to HTTPS. I believe I need to create a TIdSSLIOHandlerSocketOpenSSL component and add it to TIdHTTP as its IOHandler.

When I try to do this, the code below gives the error:

E2451 Undefined symbol 'TIdSSLIOHandlerSocketOpenSSL'

The error is on the code, std::auto_ptr<TIdSSLIOHandlerSocketOpenSSL>.

I am not sure why TIdSSLIOHandlerSocketOpenSSL is undefined, because I have Indy installed and can use TIdSSLIOHandlerSocketOpenSSL as a traditional component from the component palette.

Can anyone show me how I can set this code up to use HTTPS addresses?

std::auto_ptr<TIdSSLIOHandlerSocketOpenSSL> Local_IOHandler( new TIdSSLIOHandlerSocketOpenSSL( NULL ) );
//error: E2451 Undefined symbol 'TIdSSLIOHandlerSocketOpenSSL'
//error: E2299 Cannot generate template specialization from 'std::auto_ptr<_Ty>'


std::auto_ptr<TIdHTTP> Local_IdHTTP( new TIdHTTP( NULL ) );
Local_IdHTTP->Name="MyLocalHTTP";
Local_IdHTTP->HandleRedirects=true;
Local_IdHTTP->IOHandler=Local_IOHandler;

TStringStream *jsonToSend = new TStringStream;

UnicodeString GetURL = "https://chartapi.finance.yahoo.com/instrument/1.0/CLZ17.NYM/chartdata;type=quote;range=1d/csv/";

jsonToSend->Clear();
try
{
    Local_IdHTTP->Get(GetURL, jsonToSend);
}
catch (const Exception &E)
{
    ShowMessage( E.Message );
    //error: IOHandler value is not valid
}

Solution

  • When I try to do this the code below gives the error E2451 Undefined symbol 'TIdSSLIOHandlerSocketOpenSSL'

    Add #include <IdSSLOpenSSL.hpp> to your code.

    I am not sure why 'TIdSSLIOHandlerSocketOpenSSL' is Undefined because I have Indy installed and can use 'TIdSSLIOHandlerSocketOpenSSL' as a traditional component from the compoenent pallet?

    Dropping a component onto your Form at design-time auto-generates any necessary #include statements for you. TIdSSLIOHandlerSocketOpenSSL is no different.

    That being said, once you get that fixed, you cannot assign a std::auto_ptr itself to the IOHandler. You need to use its get() method to get the object pointer:

    Local_IdHTTP->IOHandler = Local_IOHandler.get();
    

    And you should consider using std::auto_ptr for your TStringStream as well:

    std::auto_ptr<TStringStream> json( new TStringStream );
    Local_IdHTTP->Get(GetURL, json.get());
    // use json as needed...
    

    Though in this situation, I would suggest using the overloaded version of TIdHTTP::Get() that returns a String instead, there is no benefit to using a TStringStream:

    String json = Local_IdHTTP->Get(GetURL);
    // use json as needed...