Delphi CEF4Delphi Chromium1 设置User-Agent

方法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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值