方法一:
使用SHDocVw, MSHtml单元提供的一些方法利用浏览器的特性来获取。

uses SHDocVw, MSHtml;

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

方法二:
其实很简单就是从注册表里面读取。不过该方法读取出来的并不完整。

uses Registry;

function GetDefaultUserAgent: string;
var
  reg: TRegistry;
begin
  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_CURRENT_USER;
    if Reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\Internet Settings\', FALSE)
      then Result := Reg.ReadString('User Agent');
  finally
    Reg.CloseKey;
    Reg.Free;
  end;
end;

注:其实也可以从 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings 下面读取 User Agent 的值,从 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\5.0\User Agent\Post Platform 下读取 .NET CLR 2.0.50727 类似如此的项名称,然后拼接在一起就可以构成一个IE浏览器的 UserAgent。

方法三:
利用UrlMon里面提供的方法来获取浏览器的User Agent,不过该方法有时候会失误,比如我的浏览器是IE8.0,而结果却是IE7.0。

uses UrlMon;

function GetIEUserAgent: String;
var
  IeUserAgent: array[0 .. MAX_PATH] of Char;
  dwSizeOut: DWord;
begin
  Result := '';
  try
    dwSizeOut := 0;
    ZeroMemory(@IeUserAgent, MAX_PATH * SizeOf(Char));
    UrlMkGetSessionOption(URLMON_OPTION_USERAGENT, @IeUserAgent, MAX_PATH, dwSizeOut, 0);
    Result := StrPas(IeUserAgent);
  except
    on E: Exception do
    begin
      ShowMessage(E.Message);
    end;
  end;
end;

http://www.lsworks.net/article/42.html

Delphi下获取IE的UserAgent的方法的更多相关文章

  1. Windows下获取本机IP地址方法介绍

    Windows下获取本机IP地址方法介绍 if((hostinfo = gethostbyname(name)) != NULL) { #if 1 ; printf("IP COUNT: % ...

  2. Delphi在Android下实现BroadcastReceiver功能(举例在Delphi下获取USB外设拔插消息)

    在Android里,用java通过实现BroadcastReceiver接口,就可以获得Intent消息.可是Delphi程序不能直接实现JBroadcastReceiver,如何能够实现类似Java ...

  3. 在Windows及Linux下获取毫秒级运行时间的方法

    在Windows下获取毫秒级运行时间的方法 头文件:<Windows.h> 函数原型: /*获取时钟频率,保存在结构LARGE_INTEGER中***/ WINBASEAPI BOOL W ...

  4. cocos2d-x在win32和iOS、android下获取当前系统时间的方法

    最近在游戏里要显示当前系统时间的功能,网上一搜很多写着获取的方法,大都是如下 struct cc_timeval now; CCTime::gettimeofdayCocos2d(&now, ...

  5. Android下获取FPS的几种方法

    FPS(Frames Per Second)是关乎Android用户体验最为重要的指标之一,而在VR中更是如此.为了评估VR系统.VR SDK及Unity应用的性能,通常会实时获取FPS并将其显示出来 ...

  6. Delphi实现获取磁盘空间大小的方法

    unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ...

  7. Winform中修改WebBrowser控件User-Agent的方法(已经测试成功)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  8. Delphi实现获取句柄并发送消息的方法(FindWindow、FindWindowEx、EnumChildWindows、SendMessage)

    Delphi实现获取句柄并发送消息的方法 本文以实例形式详细说明了Delphi获取句柄并发送消息的方法,具体用法说明如下: 查找另外一个窗口的句柄: handle := FindWindow(nil, ...

  9. windows下获取IP地址的两种方法

    windows下获取IP地址的两种方法: 一种可以获取IPv4和IPv6,但是需要WSAStartup: 一种只能取到IPv4,但是不需要WSAStartup: 如下: 方法一:(可以获取IPv4和I ...

随机推荐

  1. Holding Bin-Laden Captive!(hdoj1085)代码并未完全看懂

    We all know that Bin-Laden is a notorious terrorist, and he has disappeared for a long time. But rec ...

  2. Android小记之--android:listSelector

    使用ListView和GridView时,经常使用android:listSelector来使item被选中时的状态.但如果不配合android:drawSelectorOnTop来使用可能达不到想要 ...

  3. autofac使用笔记

    在之前的项目中用来解耦的使用的轻型IOC框架是unity,它的使用也是很方便的提供在之前的文章的也提到过它的使用方式,但是使用久了之后发现了它的不足之处就是需要配置xml文件来对应的接口和实现的关系. ...

  4. Nginx 变量漫谈(一)

    Nginx 的配置文件使用的就是一门微型的编程语言,许多真实世界里的 Nginx 配置文件其实就是一个一个的小程序.当然,是不是“图灵完全的”暂且不论,至少据我观察,它在设计上受 Perl 和 Bou ...

  5. Learn LIBSVM---a practical Guide to SVM classification

    想学习一下SVM,所以找到了LIBSVM--A Library for Support Vector Machines,首先阅读了一下网站提供的A practical guide to SVM cla ...

  6. 载入OpenSSL的动态库——学会使用tryToLoadOpenSslWin32Library和QPair

    Libraries name of openssl? The "library" portion of OpenSSL consists of two libraries. On ...

  7. Android中ListView无法点击

    Android中ListView无法点击 转自:http://xqjay19910131-yahoo-cn.iteye.com/blog/1319502   问题描述: ListView中Item加入 ...

  8. LInux 下挂在Windows共享文件夹

    挂载WIndow共享文件夹  //192.168.0.103/software mount -t smbfs -o username=administrator,password=“de123”  / ...

  9. sql的基本查询语句

    --------------------------------------------基本常用查询-------------------------------------- 自己简单练习做了个表. ...

  10. C / C++算法学习笔记(8)-SHELL排序

    原始地址:C / C++算法学习笔记(8)-SHELL排序 基本思想 先取一个小于n的整数d1作为第一个增量(gap),把文件的全部记录分成d1个组.所有距离为dl的倍数的记录放在同一个组中.先在各组 ...