working with IdHTTPProxyServer1

243 views Asked by At

I want to write a simple proxy with TIdHTTPProxyServer and this is my code :

void __fastcall TForm1::FormCreate(TObject *Sender)
{
    IdHTTPProxyServer1->DefaultPort = 8090;
    IdHTTPProxyServer1->Active = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
    int Index = 0;
    TList * cList;
    {
        cList = IdHTTPProxyServer1->Contexts->LockList();
        try
        {
            for ( int stop = cList->Count - 1, Index = 0; Index <= stop; Index++)
            {
                TIdContext * t;
                t = static_cast <TIdContext*>(cList[Index]);
                t->Connection->Disconnect();
            }
        }
        __finally
        {
            IdHTTPProxyServer1->Contexts->UnlockList();
        }
        IdHTTPProxyServer1->Active = false;
    }
}

I have two Question :

  1. How can I cast a TList to a TIdContext?

  2. This code doesn't change my IP and I want to change my IP. How can I change my IP with TIdHTTPProxyServer ?

1

There are 1 answers

3
Remy Lebeau On BEST ANSWER

How can I cast a TList to a TIdContext?

You don't, as they are not related classes. The real problem is that you are not accessing the TList items correctly to begin with. You need to use the TList::Items[] property instead, either directly:

t = static_cast<TIdContext*>(cList->Items[Index]);

Or indirectly, by dereferencing the TList pointer so that TList::operator[] (which uses the Items[] property internally) can be invoked:

t = static_cast<TIdContext*>((*cList)[Index]);

Either way, this code is completely unnecessary, as Indy will automatically close all active clients for you when the server is deactivated/destroyed:

void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
    IdHTTPProxyServer1->Active = false;
}

This code doesn't change my IP and I want to change my IP. How can I change my IP with TIdHTTPProxyServer ?

You cannot change the local machine's IP with TIdHTTPProxyServer, that is not it is meant for. In fact, there is nothing in Indy to change the local machine's IP, that is outside of Indy's scope. You have to use platform-specific APIs for that instead. What are you trying to accomplish exactly? Please post a separate question about this topic, and explain your requirements for it.