Delphi下获取系统默认的UserAgent的方法


本文整理自网络,侵删。

 /// <summary>   
/// 获取系统默认的UserAgent   
/// uses SHDocVw, MSHTML;   
/// </summary>   
function GetUserAgent: string;   
var  
  Doc: IHTMLDocument2;   
  win: IHTMLWindow2;   
  wb: TWebBrowser;   
begin  
  Result := '';   
  try  
    wb := TWebBrowser.Create(nil);   
    try  
      wb.Navigate('about:blank');   
      while not wb.ReadyState = READYSTATE_COMPLETE do  
        Application.ProcessMessages;   
      Doc := IHTMLDocument2(wb.Document);   
      if not Assigned(Doc) then Exit;   
      win := Doc.parentWindow;   
      if Assigned(win) then  
        Result := win.clientInformation.userAgent;   
    finally  
      wb.Free;   
    end;   
  except  
  end;   
end;  
/// <summary>
/// 获取系统默认的UserAgent
/// uses SHDocVw, MSHTML;
/// </summary>
function GetUserAgent: string;
var
  Doc: IHTMLDocument2;
  win: IHTMLWindow2;
  wb: TWebBrowser;
begin
  Result := '';
  try
    wb := TWebBrowser.Create(nil);
    try
      wb.Navigate('about:blank');
      while not wb.ReadyState = READYSTATE_COMPLETE do
        Application.ProcessMessages;
      Doc := IHTMLDocument2(wb.Document);
      if not Assigned(Doc) then Exit;
      win := Doc.parentWindow;
      if Assigned(win) then
        Result := win.clientInformation.userAgent;
    finally
      wb.Free;
    end;
  except
  end;
end;

注:此方法为获取系统默认的UserAgent的方法,而不一定是系统默认IE浏览器的UserAgent,如需获取IE浏览器的UserAgent,可将Webbrowser对象改为IWebBrowser2或IShellWindows等接口即可

相关阅读 >>

Delphi 串口常用的字符串转换函数

Delphi 连接dbf数据

Delphi 中使用微软全文翻译的小例子

Delphi 如何快速读取非常大的文本文件

delph记录输出日志

Delphi listbox模糊查找文字

Delphi 正则表达式校验手机号

Delphi ioutils单元3查找并搜索文件夹信息

Delphi 利用hook api函数openprocess与terminateprocess来防止任务管理器结束进程

Delphi的ttreeview类使用大全

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...