天天看點

一直苦惱于找不到擷取系統預設的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;

注:此方法為擷取系統預設的UserAgent的方法,而不一定是系統預設IE浏覽器的UserAgent,如需擷取IE浏覽器的UserAgent,可将Webbrowser對象改為IWebBrowser2或IShellWindows等接口即可