2010-01-06 85 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

相關問題