不幸的是,您的自定义头文件被清除并在TDSRestRequest.GetHTTP
中被忽略(并且在执行Datasnap.DSClientRest
单元中隐藏TDSRestRequest
)。 尝试此变通办法:
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.
嗨,非常感谢您的答复。请原谅我,但您能否确认您发布的代码是放置在Datasnap.DSClientRest单元中,还是只能作为我的项目中的新单元添加? –
嗨,对不起,我还不够清楚。把代码放在你自己的任何单元中。我专门编写了这些代码,以避免修改原始Datasnap源代码。否则,你可以简单地将它修复在'TDSRestRequest.GetHTTP'中。 –
奇妙的是,我不明白它是如何工作的,但它的工作原理!非常感谢。 –