不幸的是,您的自定義頭文件被清除並在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'中。 –
奇妙的是,我不明白它是如何工作的,但它的工作原理!非常感謝。 –