不幸的是,您的自定义标头被清除并忽略TDSRestRequest.GetHTTP
(并TDSRestRequest
隐藏在Datasnap.DSClientRest
单元的实现中)。试试这个解决方法:
uses
Datasnap.DSHTTP, IdHTTPHeaderInfo;
const
SUserAgent = 'MyUserAgent';
type
TDSHTTPEx = class(TDSHTTP)
constructor Create(AOwner: TComponent; const AIPImplementationID: string); override;
end;
TDSHTTPSEx = class(TDSHTTPS)
constructor Create(const AIPImplementationID: string); override;
end;
{ TDSHTTPEx }
constructor TDSHTTPEx.Create(AOwner: TComponent; const AIPImplementationID: string);
begin
inherited Create(AOwner, AIPImplementationID);
with Request.GetObject as TIdRequestHeaderInfo do
UserAgent := SUserAgent;
end;
{ TDSHTTPSEx }
constructor TDSHTTPSEx.Create(const AIPImplementationID: string);
begin
inherited Create(AIPImplementationID);
with Request.GetObject as TIdRequestHeaderInfo do
UserAgent := SUserAgent;
end;
initialization
TDSHTTP.UnregisterProtocol('http');
TDSHTTP.RegisterProtocol('http', TDSHTTPEx);
TDSHTTP.UnregisterProtocol('https');
TDSHTTPS.RegisterProtocol('https', TDSHTTPSEx);
finalization
TDSHTTP.UnregisterProtocol('http');
TDSHTTP.UnregisterProtocol('https');
end.