TidHTTP and apple store

This is the forum for general discussion.

Moderator: 2ffat

TidHTTP and apple store

Postby Lena » Sat Mar 11, 2017 7:17 am

We can't use TidHTTP in the app to publish in the store apple because they require the support ipV6.
On this forum: http://fire-monkey.ru I read that the solution is the use of T(Net)HTTPClient
Help translate this code in C++:
Code: Select all
function CheckInet: boolean;
var
  aResp: IHTTPResponse;
  aHTTP: THTTPClient;
begin
  Result := false;
  aHTTP := THTTPClient.Create;
  try
    try
      aResp := aHTTP.Head('http://google.com');
      Result := aResp.StatusCode < 400;
    except
      Result := false;
    end;
  finally
    FreeAndNil(aHTTP);
  end;
end;

Sorry in the wrong thread of the forum wrote.
Lena
BCBJ Master
BCBJ Master
 
Posts: 452
Joined: Sun Feb 06, 2011 1:28 pm

Re: TidHTTP and apple store

Postby rlebeau » Sun Mar 12, 2017 2:22 am

Lena wrote:We can't use TidHTTP in the app to publish in the store apple because they require the support ipV6.


Indy supports IPv6 in general, and has for several years. Most Indy TCP-based components have an IPVersion property available.

TIdHTTP can make IPv6-based requests, however, there is a trick to it - you must wrap hostnames in square brackets, and only on IPv6 networks, eg:

Code: Select all
IdHTTP1->Get("http://[google.com]/");


The brackets signal to TIdHTTP that it should use an IPv6 socket and resolve the hostname to an IP using IPv6 instead of IPv4. The brackets are also required for IPv6 literal addresses, eg:

Code: Select all
IdHTTP1->Get("http://[2001:4860:b002::68]/");


This is not ideal, and will not work on an IPv4 network, only an IPv6 network. But it is all that TIdHTTP supports right now. There is currently some 3rd party effort to make Indy a little smarter in regards to connecting to a hostname when it could resolve to either an IPv4 or IPv6 address. This logic is not integrated into the main Indy release yet.

Lena wrote:On this forum: http://fire-monkey.ru I read that the solution is the use of T(Net)HTTPClient


That is one workaround, yes. But then you are not using Indy anymore, so you would have to re-write any existing TIdHTTP code to use Embarcadero's component instead, which does not have the same interface as TIdHTTP.

Lena wrote:Help translate this code in C++:


Code: Select all
bool __fastcall CheckInet()
{
    bool result = false;
    THTTPClient *aHTTP = new THTTPClient;
    try
    {
        try
        {
            _di_IHTTPResponse aResp = aHTTP->Head("http://google.com");
            result = (aResp->StatusCode < 400);
        }
        catch (const Exception &)
        {
            result = false;
        }
    }
    __finally
    {
        delete aHTTP;
    }
    return result;
}
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1399
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: TidHTTP and apple store

Postby Lena » Sun Mar 12, 2017 8:20 am

Thank you very much!

Please tell me how to fix this?
THTTPClient *aHTTP = new THTTPClient;
[bccaarm Error] UnitINI.cpp(30): allocating an object of abstract class type 'System::Net::Httpclient::THTTPClient'


Maybe so?
Code: Select all
bool __fastcall CheckInet()
{
   bool result = false;
//how use std::unique_ptr and THTTPClient?
   THTTPClient *aHTTP = System::Net::Httpclient::THTTPClient::Create();
   try
   {
      try
      {
         _di_IHTTPResponse aResp = aHTTP->Head("http://google.com");
         result = (aResp->StatusCode < 400);
      }
      catch (System::Sysutils::Exception &E)//<-- because const Exception & reference to 'Exception' is ambiguous
      {
         result = false;
      }
   }
   __finally
   {
      delete aHTTP;
   }
   return result;
}



P.S
Can I use this code to check connection with TidHTTP in Internet projects iOS and Android?
Code: Select all
void __fastcall TForm1::Image1Click(TObject *Sender)
{

 try
   {
    IdHTTP1->Get("http://[google.com]/");
   }
   catch(System::Sysutils::Exception &E)
      {
       ShowMessage(L"No Internet connection.");
       return;
      }

 FormWEB = new TFormWEB(this);
 FormWEB->WebBrowser1->Navigate(L"http://bcbj.org/forums");
 FormWEB->Show();
}
Lena
BCBJ Master
BCBJ Master
 
Posts: 452
Joined: Sun Feb 06, 2011 1:28 pm

Re: TidHTTP and apple store

Postby rlebeau » Sun Mar 12, 2017 9:25 am

Lena wrote:Please tell me how to fix this?
THTTPClient *aHTTP = new THTTPClient;
[bccaarm Error] UnitINI.cpp(30): allocating an object of abstract class type 'System::Net::Httpclient::THTTPClient'



That means THTTPClient has an abstract method in it (the compiler should be telling you exactly which one) and thus cannot be instantiated directly, you must use a derived class instead.

I didn't realize that THTTPClient.Create() is just a class method and not an actual constructor (most constructors in Delphi are named Create). So the C++ way to instantiate THTTPClient is:

Code: Select all
THTTPClient *aHTTP = THTTPClient::Create();


Otherwise, use TNetHTTPClient instead:

Code: Select all
TNetHTTPClient *aHTTP = new TNetHTTPClient(nullptr);



Lena wrote:Can I use this code to check connection with TidHTTP in Internet projects iOS and Android?


Technically yes, but again, use brackets only on an IPv6 enabled network. You still need to support IPv4 networks as well. Also, there is no point in using Get() if you are going to ignore the data received. Use Head() instead.

You could try something more like this:

Code: Select all
void __fastcall TForm1::Image1Click(TObject *Sender)
{
    try
    {
        IdHTTP1->Head("http://[google.com]/");
    }
    catch(const System::Sysutils::Exception &)
    {
        try
        {
            IdHTTP1->Head("http://google.com/");
        }
        catch(const System::Sysutils::Exception &)
        {
            ShowMessage(L"No Internet connection.");
            return;
        }
    }

    FormWEB = new TFormWEB(this);
    FormWEB->WebBrowser1->Navigate(L"http://bcbj.org/forums");
    FormWEB->Show();
}


But really, it is redundant to check the connection manually since the web browser is going to do that anyway, so just let the browser handle it. It should report any errors to you. Also, it is very important that you NOT perform blocking operations (like TIdHTTP) on the main UI thread at all on iOS and Android. They must be in a worker thread instead.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1399
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: TidHTTP and apple store

Postby Lena » Mon Mar 13, 2017 12:29 am

Thank you very much!
Lena
BCBJ Master
BCBJ Master
 
Posts: 452
Joined: Sun Feb 06, 2011 1:28 pm


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron