While working on a project which uses the TIdHTTP component, I realized I needed to eliminate certain HTTP headers from being sent.
After examining the logic flow in Indy's TIdHTTP component, I realized that all I needed to do to manipulate HTTP headers is to create a TIdHttpRequest descendant, and override the SetHeaders method.
Then, before making a call to any of the TIdHTTP.Get methods, assign an instance of the TIdHttpRequest descendant to the TIdHTTP.Request property, like so:
IdHTTP1.Request.Free;
IdHTTP1
.
Request
Free;
Request := TIdHTTPRequestReplacement
Create(IdHTTP1);
Here's what the SetHeaders method looks like:
procedure
TIdHTTPRequestReplacement
SetHeaders;
var
I:
Integer
;
LHeader:
string
SL: TStringList;
begin
inherited
SL := TStringList
Create;
try
// Clear any headers that is not User-Agent, Host, etc...
for
I :=
0
to
RawHeaders
Count-
1
do
LHeader := RawHeaders
Names[ I ];
if
(LHeader<>
'User-Agent'
)
and
'Host'
'xxx'
'yyy'
then
SL
Add(LHeader);
end
// Pretend we're some other browser
Values[
] :=
'custom user-agent'
LHeader
in
Values[LHeader] :=
''
finally