2010-01-06 76 views
9

我有一个Delphi应用程序,使用Indy components与Internet上的Web服务器进行通信。该应用程序的大多数用户都有直接的Internet连接,但有些位于本地网络的代理服务器之后。我不希望有要求用户查找自己的代理服务器在Internet Options/Connections/LAN Settings dialogDelphi应用程序如何检测Windows PC的网络代理设置?

alt text http://toybase.files.wordpress.com/2008/11/ie-proxy-settings.png

因为坦率地说,大多数人都不会知道也不关心这个设置是什么。

我可以通过Delphi-7应用程序的某些系统调用获取此信息吗?

非常感谢!

回答

13

Via WinAPI - WinHttpGetIEProxyConfigForCurrentUser。你必须爱MS的长WINAPI名字^ _ ^。

OP编辑后:您可以从注册表中读取,AFAIR将设在这里:

[ HKEY_CURRENT_USER/Software/Microsoft/Windows/CurrentVersion/Internet Settings ] 
+7

+1,但如果我没有记错,那是只有IE,虽然。 Firefox和其他浏览器维护自己的代理设置。 – 2010-01-06 15:12:05

+0

这是真的,但我认为如果OP想要通过系统调用获得它,那么他就是这么做的。 – 2010-01-06 15:28:52

+1

谢谢!现在我知道要搜索什么,我在这里找到了一些代码:http://coding.derkeiler.com/Archive/Delphi/borland.public.delphi.nativeapi/2004-01/0205.html – devstopfix 2010-01-12 20:41:18

1

你必须让代理从浏览器设置,这可能是根据几个不同的地点正在使用的浏览器。

您可能会考虑查看Web Proxy Autodiscovery Protocol,它会自动检测网络上的代理设置。

+0

用户肯定会使用IE :)但感谢链接! – devstopfix 2010-01-12 21:11:24

2

Kornel Kisielewiczanswer的Delphi代码:

uses Registry, Windows; 

function detectIEProxyServer() : string; 
begin 
    with TRegistry.Create do 
    try 
     RootKey := HKEY_CURRENT_USER; 
     if OpenKey('\Software\Microsoft\Windows\CurrentVersion\Internet Settings', False) then begin 
      Result := ReadString('ProxyServer'); 
      CloseKey; 
     end 
     else 
      Result := ''; 
    finally 
     Free; 
    end; 
end; 
+0

注意:TRegistry.ReadString():“如果注册表项包含字符串以外的内容,则会引发异常。” – devstopfix 2010-01-12 21:21:14

3

这里的另一种方法,我用的,它并不需要直接注册表访问。这在D2007下工作,但我不明白为什么它不能在D7下工作。

uses 
    WinInet, 
    SysUtils; 

function UseIEProxyInfo(var ProxyHost: String; var ProxyPort: Integer): Boolean; 
var 
    ProxyInfo: PInternetProxyInfo; 
    Len: LongWord; 
    ProxyDetails: String; 
    s2: String; 
    i1: Integer; 

    procedure RemoveProtocol(var str: string); 
    var 
    i1 : integer; 
    begin 
    i1 := PosText('://', str); 
    if i1 > 0 then 
     Delete(str, 1, i1 + 2); 
    i1 := PosText('http=', str); 
    if i1 > 0 then begin 
     Delete(str, 1, i1 + 4); 
     str := SubStr(str, 1, ' '); 
    end; 
    end; 

begin 
    Result := False; 

    Len := 4096; 
    GetMem(ProxyInfo, Len); 
    try 
    if InternetQueryOption(nil, INTERNET_OPTION_PROXY, ProxyInfo, Len) then 
    begin 
     if ProxyInfo^.dwAccessType = INTERNET_OPEN_TYPE_PROXY then 
     begin 
     Result := True; 
     ProxyDetails := ProxyInfo^.lpszProxy; 

     RemoveProtocol(ProxyDetails); 
     s2 := SubStr(ProxyDetails, 2, ':'); 
     if s2 <> '' then 
     begin 
      try 
      i1 := StrToInt(s2); 
      except 
      i1 := -1; 
      end; 

      if i1 <> -1 then 
      begin 
      ProxyHost := SubStr(ProxyDetails, 1, ':'); 
      ProxyPort := i1; 
      end; 
     end; 
     end; 
    end; 
    finally 
    FreeMem(ProxyInfo); 
    end; 
end; 
+0

这将是很好的知道SubStr函数的定义,因为它在这里不是很明显。 – mj2008 2012-07-31 12:37:21

+1

@ mj2008:它是对来自madExcept异常处理库的madStrings.pas单元中的函数的引用。你可以在这里找到一些在线帮助:http://help.madshi.net/StringSub.htm – 2012-07-31 20:50:36

相关问题