I am new to learn C ++ Builder. Three days ago, I installed Embarcadero®. C++Builder® 2010. This language is very interesting for me to learn.
In Delphi, I generally write a simple proxy-server using TIdMappedPortTCP of Indy 9 and 10. I usually use its OnExecute and OnOutboundData events to modify data as it passes through the proxy.
Since I'm new in C ++ Builder, so I don't know how to convert my Delphi code to the exactly right C ++ Builder code.
I've tried and tried many ways, including reading several books, one of which is Borland C ++ Builder - The Complete Reference, by Herbert Schildt, as well as to increase knowledge. Unfortunately, in the book was not discussed at all very important things related to my condition. Also, I find references on google, but I've not found.
So, I ventured to ask for your help. I really need it. Please help! Thank you very much.
The following is my Indy 10's Delphi code that I want to write to C ++ Builder.
......
procedure TForm.IdMappedPortTCP1Execute(AContext: TIdContext);
var
Mydata, NetData: string;
begin
if (Pos('HTTP',netstring(AContext)) <> 0) or (Pos('GET',netstring(AContext)) <> 0) then begin
NetData := netstring(AContext);
TIdMappedPortContext(AContext).OutboundClient.IOHandler.Write(AddHeader(netstring(AContext),'Connection: Keep-Alive'));
Sleep(1000);
Mydata := 'GET http://website.com/ HTTP/1.1'+#13#10+'Host: website.com'#13#10;
NetData := Mydata + Netdata;
TIdMappedPortContext(AContext).NetData := netbyte(Netdata);
TIdMappedPortContext(AContext).OutboundClient.IOHandler.Write(netbyte(Mydata + NetData));
end;
end;
......
A literal translation to C++Builder would look like this:
Here is a slightly condensed version:
But either way, this is definitely NOT a reliable way to implement a viable HTTP proxy in Indy. In fact, Indy 10 introduced a specific
TIdHTTPProxyServer
component for that very purpose. You should seriously consider using that instead ofTIdMappedPortTCP
. For example, the above can be done inTIdHTTPProxyServer
like this:Update: the
netstring()
andnetbyte()
functions you linked to have syntax errors, and have unnecessary overhead (there is no need to involve MIME just to convert a String into a byte array and vice versa, Indy has functions specifically for that purpose). Here are the corrected versions:So, you could actually just eliminate the functions altogether: