代码之家  ›  专栏  ›  技术社区  ›  devstopfix

Delphi应用程序如何检测Windows PC的网络代理设置?

  •  10
  • devstopfix  · 技术社区  · 16 年前

    我有一个Delphi应用程序,它使用 Indy components . 应用程序的大多数用户都有直接的Internet连接,但有些用户在本地网络的代理服务器后面。我不想要求用户在 Internet Options / Connections / LAN Settings dialog

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

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

    我可以通过Delphi-7应用程序的一些系统调用获得这些信息吗?

    多谢!

    4 回复  |  直到 15 年前
        1
  •  13
  •   Kornel Kisielewicz    16 年前

    通过WiAPI WinHttpGetIEProxyConfigForCurrentUser . 你一定很喜欢她那长长的winapi名字。

    操作编辑后:您可以从注册表中读取,它将位于此处:

     [ HKEY_CURRENT_USER/Software/Microsoft/Windows/CurrentVersion/Internet Settings ]
    
        2
  •  3
  •   devstopfix    8 年前

    Delphi代码 Kornel Kisielewicz answer :

    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;
    
        3
  •  3
  •   Conor Boyd    15 年前

    这是我使用的另一种方法,不需要直接访问注册表。这在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;
    
        4
  •  1
  •   Dave Swersky    16 年前

    您必须从浏览器获取代理设置,根据使用的浏览器,代理设置可能位于多个不同的位置。

    你可以考虑调查一下 Web Proxy Autodiscovery Protocol 自动检测网络上的代理设置。