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等接口即可

原文地址:https://www.cnblogs.com/spider518/p/1921304.html