方法1
procedure TMiniBrowserFrm.Chromium1BeforeResourceLoad(Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame;
const request: ICefRequest; const callback: ICefRequestCallback;
out Result: TCefReturnValue);
//===============设置 User-Agent
var
TempOldMap, TempNewMap : ICefStringMultimap;
i : NativeUInt;
TempReplaced : boolean;
userAgent : string;
begin
Result := RV_CONTINUE;
userAgent := EditUA.Text;
if userAgent <> '' then
try
// We replace the Accept-Encoding HTTP header to request uncompressed resources.
// If the server sends uncompressed resources it should be easier to handle the
// end of the resource reception because we may know its length.
TempNewMap := TCefStringMultimapOwn.Create;
TempOldMap := TCefStringMultimapOwn.Create;
request.GetHeaderMap(TempOldMap);
TempReplaced := False;
i := 0;
while (i < TempOldMap.Size) do
begin
if (CompareText(TempOldMap.Key[i], 'User-Agent') = 0) then
begin
TempNewMap.Append('User-Agent', userAgent);
TempReplaced := True;
end
else
TempNewMap.Append(TempOldMap.Key[i], TempOldMap.Value[i]);
inc(i);
end;
if not(TempReplaced) then TempNewMap.Append('User-Agent', userAgent);
request.SetHeaderMap(TempNewMap);
finally
TempNewMap := nil;
TempOldMap := nil;
end;
//===============设置 User-Agent
Result := RV_CONTINUE;
if Chromium1.IsSameBrowser(browser) and
(frame <> nil) and
frame.IsMain and
frame.IsValid then
InspectRequest(request);
end;
方法2
procedure TMiniBrowserFrm.Chromium1BeforeResourceLoad(Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame;
const request: ICefRequest; const callback: ICefRequestCallback;
out Result: TCefReturnValue);
//===============设置 User-Agent
Var
map: ICefStringMultimap;
begin
map := TCefStringMultimapOwn.Create;
request.GetHeaderMap(map);
map.Append('User-Agent','Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16');
request.SetHeaderMap(map);
callback.Cont(true);
end;
//===============设置 User-Agent
Result := RV_CONTINUE;
if Chromium1.IsSameBrowser(browser) and
(frame <> nil) and
frame.IsMain and
frame.IsValid then
InspectRequest(request);
end;
Delphi CEF4Delphi Chromium1 设置User-Agent http://www.delphitop.com/html/kongjian/5311.html