Currently I want to redirect specific Post requests using Indy10 TIdHTTPProxyServer. I followed the page
http://embarcadero.newsgroups.archived.at/public.delphi.internet.winsock/200904/0904301714.html
and wrote a simple sample as follow.
oid __fastcall TForm3::MyProxyHTTPBeforeCommand(TIdHTTPProxyServerContext *AContext)
{
if (AContext->Target == "http://example.com/need_redirect_url" ) {
TIdIOHandler* io = AContext->Connection->IOHandler;
io->WriteLn("HTTP/1.0 302 Moved Temporarily");
io->WriteLn("Location: http://exampledomain.com/target_url");
io->WriteLn("Connection: close");
}
}
It works if I press the "http://sample.com/need_redirect_url" into the URL bar of the browser. But return nothing(No matter Post or Get) if it is a XMLHttpRequest which is targeted to the same URL.
I have to admit I am really unfamiliar with how HTTP works. And I also wonder if there is any better way to do what I want.
Although I am using C++Builder XE2. Delphi samples are also appreciated as there are less examples using indy components using C++
Your code is not writing a blank line that terminates the HTTP headers you are sending. You will also need to throw an exception to prevent TIdHTTPProxyServer
from navigating to the client's requested URL after the OnBeforeCommand
event handler exits.
Try this:
void __fastcall TForm3::MyProxyHTTPBeforeCommand(TIdHTTPProxyServerContext *AContext)
{
if (AContext->Target == "http://xxx.com/need_redirect_url" )
{
TIdIOHandler* io = AContext->Connection->IOHandler;
io->WriteLn("HTTP/1.0 302 Moved Temporarily");
io->WriteLn("Location: http://mydomain.com/target_url");
io->WriteLn("Connection: close");
io->WriteLn("");
Abort();
}
}